<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ProductPriceGroupType;
use CpCreation\VitiCore\Product\Model\ProductPriceGroup;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
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-price-group')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN')"))]
final class ProductPriceGroupController extends AbstractController
{
    public function __construct(
        private readonly ProductPriceGroupRepository $productPriceGroupRepository,
    ) {}

    #[Route('', name: 'admin_product_price_group')]
    public function index(): Response
    {
        $groups = $this->productPriceGroupRepository->findByPositionOrderAdmin();

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

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

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

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

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

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

    #[Route('/{id}/edit', name: 'admin_product_price_group_edit')]
    public function edit(ProductPriceGroup $group, Request $request): Response
    {
        $form = $this->createForm(ProductPriceGroupType::class, $group);
        $form->handleRequest($request);

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

            $this->productPriceGroupRepository->save($data);
            $this->addFlash('alert-success', 'Le groupe de prix '.$group->getName().' a été modifié avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_product_price_group_delete')]
    public function delete(ProductPriceGroup $group): Response
    {
        $this->productPriceGroupRepository->deleteGroup($group);
        $this->addFlash('alert-success', 'Le groupe de prix '.$group->getName().' a été supprimé avec succès.');

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

    #[Route('/{id}/moveUp', name: 'admin_product_price_group_move_up')]
    public function moveUp(ProductPriceGroup $group): Response
    {
        $group->setPosition($group->getPosition() - 1);
        $this->productPriceGroupRepository->save($group);

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

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

    #[Route('/{id}/moveDown', name: 'admin_product_price_group_move_down')]
    public function moveDown(ProductPriceGroup $group): Response
    {
        $group->setPosition($group->getPosition() + 1);
        $this->productPriceGroupRepository->save($group);

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

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