<?php

namespace App\Controller\Blog;

use App\Core\Controller\Admin\Crud\CrudController;
use App\Core\Crud\CrudConfiguration;
use App\Core\Crud\Field\ButtonField;
use App\Core\Crud\Field\TextField;
use App\Core\Entity\EntityInterface;
use App\Core\Manager\EntityManager;
use App\Entity\Blog\Category as Entity;
use App\Factory\Blog\CategoryFactory as EntityFactory;
use App\Form\Blog\CategoryType;
use App\Repository\Blog\CategoryRepositoryQuery as RepositoryQuery;
use App\Repository\Blog\PostRepositoryQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/admin/blog/category")
 */
class CategoryAdminController extends CrudController
{
    public function getConfiguration(): CrudConfiguration
    {
        return CrudConfiguration::create()
            ->setPageTitle('index', 'Catégories')
            ->setPageTitle('edit', '{title}')
            ->setPageTitle('new', 'Nouvelle catégorie')
            ->setPageTitle('show', '{title}')

            ->setPageRoute('index', 'admin_blog_category_index')
            ->setPageRoute('new', 'admin_blog_category_new')
            ->setPageRoute('edit', 'admin_blog_category_edit')
            ->setPageRoute('show', 'admin_blog_category_show')
            ->setPageRoute('delete', 'admin_blog_category_delete')
            ->setPageRoute('filter', 'admin_blog_category_filter')
            ->setPageRoute('batch', 'admin_blog_category_batch')

            ->setForm('edit', CategoryType::class, [])
            ->setForm('new', CategoryType::class)

            ->setMaxPerPage('index', 100)

            ->setView('form', 'blog/category_admin/_form.html.twig')

            ->setDefaultSort('index', 'title', 'asc')

            ->setField('index', 'Titre', TextField::class, [
                'property' => 'title',
                'sort' => ['title', '.title'],
            ])
            ->setField('index', 'Articles', ButtonField::class, [
                'property_builder' => function (EntityInterface $entity) {
                    $count = $entity->getPosts()->count();

                    return '<strong>'.$count.'</strong> article'.($count > 1 ? 's' : '');
                },
                'button_attr' => ['class' => 'btn btn-sm btn-light'],
                'raw' => true,
            ])
            ->setField('index', 'Catégorie', TextField::class, [
                'property_builder' => function (EntityInterface $entity) {
                    return $entity->getParentCategory()?->getTitle() ?? '';
                },
            ])
            ->setField('index', 'Status', ButtonField::class, [
                'sort' => ['isActive', '.isActive'],
                'attr' => ['class' => 'miw-100'],
                'property_builder' => function (EntityInterface $entity) {
                    return $entity->getIsActive() ? 'Actif' : 'Non actif';
                },
                'button_attr_builder' => function (EntityInterface $entity) {
                    $class = 'btn btn-sm '.($entity->getIsActive() ? 'btn-success' : 'btn-secondary');

                    return ['class' => $class];
                },
            ])
        ;
    }

    /**
     * @Route("/{page}", name="admin_blog_category_index", requirements={"page": "\d+"})
     */
    public function index(RepositoryQuery $query, Request $request, Session $session, int $page = 1): Response
    {
        return $this->doIndex($page, $query, $request, $session);
    }

    /**
     * @Route("/new", name="admin_blog_category_new")
     */
    public function new(EntityFactory $factory, EntityManager $entityManager, Request $request): Response
    {
        return $this->doNew($factory->create(), $entityManager, $request);
    }

    /**
     * @Route("/edit/{entity}", name="admin_blog_category_edit")
     */
    public function edit(Entity $entity, EntityManager $entityManager, Request $request): Response
    {
        return $this->doEdit($entity, $entityManager, $request);
    }

    /**
     * @Route("/show/{entity}", name="admin_blog_category_show")
     */
    public function show(Entity $entity, PostRepositoryQuery $postQuery): Response
    {
        $posts = $postQuery->create()
            ->addOrderBy('.publishedAt', 'DESC')
            ->addOrderBy('.createdAt', 'DESC')
            ->inCategory($entity)
            ->paginate(1, 10)
        ;

        $this->getConfiguration()->setView('show_entity', 'blog/category_admin/_show.html.twig');
        $this->getConfiguration()->addViewData('show', 'posts', $posts);

        return $this->doShow($entity);
    }

    /**
     * @Route("/filter", name="admin_blog_category_filter", methods={"GET"})
     */
    public function filter(Session $session): Response
    {
        return $this->doFilter($session);
    }

    /**
     * @Route("/delete/{entity}", name="admin_blog_category_delete", methods={"DELETE"})
     */
    public function delete(Entity $entity, EntityManager $entityManager, Request $request): Response
    {
        return $this->doDelete($entity, $entityManager, $request);
    }

    /**
     * @Route("/admin/blog_category/batch/{page}", name="admin_blog_category_batch", methods={"POST"}, requirements={"page":"\d+"})
     */
    public function batch(RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session, int $page = 1): Response
    {
        return $this->doBatch($page, $query, $entityManager, $request, $session);
    }

    public function getSection(): string
    {
        return 'blog_category';
    }
}
