<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ProductDegressiveType;
use CpCreation\VitiCore\Product\Model\ProductDegressive;
use CpCreation\VitiCore\Product\Repository\ProductDegressiveRepository;
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-degressive')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_DEGRESSIVE')"))]
final class ProductDegressiveController extends AbstractController
{
    public function __construct(
        private readonly ProductDegressiveRepository $productDegressiveRepository,
    ) {}

    #[Route('', name: 'admin_product_degressive')]
    public function index(): Response
    {
        $discounts = $this->productDegressiveRepository->findOrder();

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

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

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

            $this->productDegressiveRepository->save($data);
            $this->addFlash('alert-success', 'La dégressivité a été ajoutée avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_product_degressive_edit')]
    public function edit(ProductDegressive $degressive, Request $request): Response
    {
        $form = $this->createForm(ProductDegressiveType::class, $degressive);
        $form->handleRequest($request);

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

            $this->productDegressiveRepository->save($data);
            $this->addFlash('alert-success', 'La dégressivité a été modifiée avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_product_degressive_delete')]
    public function delete(ProductDegressive $degressive): Response
    {
        $this->productDegressiveRepository->delete($degressive);
        $this->addFlash('alert-success', 'La dégressivité a été supprimée avec succès.');

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