<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ProductCategoryType;
use CpCreation\VitiCore\Product\Model\ProductCategory;
use CpCreation\VitiCore\Product\Repository\ProductCategoryRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[Route('/produit_categories')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ProductCategoryController extends AbstractController
{
    public function __construct(
        private readonly ProductCategoryRepository $productCategoryRepository,
    ) {
    }

    #[Route('', name: 'admin_product_category')]
    public function index(): Response
    {
        $products = $this->productCategoryRepository->findBy([], ['position' => 'ASC']);

        return $this->render('@CpCreationVitiCore/admin/product/category/index.html.twig', [
            'items' => $products,
        ]);
    }

    #[Route('/new', name: 'admin_product_category_new')]
    public function new(Request $request): Response
    {
        $form = $this->createForm(ProductCategoryType::class);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var ProductCategory $data */
            $data = $form->getData();

            $this->productCategoryRepository->save($data);
            $this->addFlash('alert-success', 'La catégorie '.$data->getName().' a été ajoutée avec succès');

            return $this->redirectToRoute('admin_product_category');
        }

        return $this->render('@CpCreationVitiCore/admin/product/category/new.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/edit', name: 'admin_product_category_edit')]
    public function edit(ProductCategory $category, Request $request): Response
    {
        $form = $this->createForm(ProductCategoryType::class, $category);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var ProductCategory $data */
            $data = $form->getData();

            $this->productCategoryRepository->save($data);
            $this->addFlash('alert-success', 'La catégorie '.$category->getName().' a été modifiée avec succès');

            return $this->redirectToRoute('admin_product_category');
        }

        return $this->render('@CpCreationVitiCore/admin/product/category/edit.html.twig', [
            'item' => $category,
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/moveUp', name: 'admin_product_category_move_up')]
    public function moveUp(ProductCategory $category): Response
    {
        $category->setPosition($category->getPosition() - 1);
        $this->productCategoryRepository->save($category);

        $this->addFlash('alert-success', 'La catégorie '.$category->getName().' a été remontée.');

        return $this->redirectToRoute('admin_product_category');
    }

    #[Route('/{id}/moveDown', name: 'admin_product_category_move_down')]
    public function moveDown(ProductCategory $category): Response
    {
        $category->setPosition($category->getPosition() + 1);
        $this->productCategoryRepository->save($category);

        $this->addFlash('alert-success', 'La catégorie '.$category->getName().' a été descendue.');

        return $this->redirectToRoute('admin_product_category');
    }

    #[Route('/{id}/delete', name: 'admin_product_category_delete')]
    public function delete(ProductCategory $category): Response
    {
        $this->productCategoryRepository->delete($category);
        $this->addFlash('alert-success', 'La catégorie '.$category->getName().' a été supprimée avec succès.');

        return $this->redirectToRoute('admin_product_category');
    }
}
