<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ProductTypeType;
use CpCreation\VitiCore\Product\Model\ProductType;
use CpCreation\VitiCore\Product\Repository\ProductTypeRepository;
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('/product-type')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ProductTypeController extends AbstractController
{
    public function __construct(
        private readonly ProductTypeRepository $productTypeRepository,
    ) {}

    #[Route('', name: 'admin_product_type')]
    public function index(): Response
    {
        $productTypes = $this->productTypeRepository->findByPositionOrderAdmin();

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

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

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

            $this->productTypeRepository->save($data);
            $this->addFlash('alert-success', 'Le type '.$data->getName().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_product_type_edit')]
    public function edit(ProductType $type, Request $request): Response
    {
        $form = $this->createForm(ProductTypeType::class, $type);
        $form->handleRequest($request);

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

            $this->productTypeRepository->save($data);
            $this->addFlash('alert-success', 'Le type '.$type->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/moveUp', name: 'admin_product_type_move_up')]
    public function moveUp(ProductType $type): Response
    {
        $type->setPosition($type->getPosition() - 1);
        $this->productTypeRepository->save($type);

        $this->addFlash('alert-success', 'Le type de produit '.$type->getName().' a été remonté.');

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

    #[Route('/{id}/moveDown', name: 'admin_product_type_move_down')]
    public function moveDown(ProductType $type): Response
    {
        $type->setPosition($type->getPosition() + 1);
        $this->productTypeRepository->save($type);

        $this->addFlash('alert-success', 'Le type de produit '.$type->getName().' a été descendu.');

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

    #[Route('/{id}/delete', name: 'admin_product_type_delete')]
    public function delete(ProductType $type): Response
    {
        $this->productTypeRepository->delete($type);
        $this->addFlash('alert-success', 'Le type '.$type->getName().' a été supprimé avec succès.');

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