<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ProductPriceCodeType;
use CpCreation\VitiCore\Product\Model\ProductPriceCode;
use CpCreation\VitiCore\Product\Repository\ProductPriceCodeRepository;
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-code')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_PRICE_CODE')"))]
final class ProductPriceCodeController extends AbstractController
{
    public function __construct(
        private readonly ProductPriceCodeRepository $productPriceCodeRepository,
    ) {}

    #[Route('', name: 'admin_product_price_code')]
    public function index(): Response
    {
        $codes = $this->productPriceCodeRepository->findByGroupOrderAdmin();

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

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

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

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

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

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

    #[Route('/{id}/edit', name: 'admin_product_price_code_edit')]
    public function edit(ProductPriceCode $code, Request $request): Response
    {
        $form = $this->createForm(ProductPriceCodeType::class, $code);
        $form->handleRequest($request);

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

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

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

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

    #[Route('/{id}/delete', name: 'admin_product_price_code_delete')]
    public function delete(ProductPriceCode $code): Response
    {
        $this->productPriceCodeRepository->softDelete($code);
        $this->addFlash('alert-success', 'Le code '.$code->getCode().' a été supprimé avec succès.');

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