<?php

namespace App\Controller\Dashboard;

use App\Entity\Promotion;
use App\Form\PromotionType;
use App\Logger\AppLogger;
use App\Repository\PromotionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/crm/promotions')]
class PromotionController extends AbstractController
{
    #[Route('/', name: 'app_crm_promotion', methods: ['GET'])]
    public function index(PromotionRepository $promotionRepository, PaginatorInterface $paginator, Request $request, AppLogger $logger): Response
    {
        $logger->record('VIEW', 'Consultation des promotions');

        $pagination = $paginator->paginate(
            $promotionRepository->findBy([], ['dateStart' => 'DESC']),
            $request->query->getInt('page', 1),
            10
        );

        return $this->render('dashboard/promotions.twig', [
            'promotions' => $pagination,
        ]);
    }

    #[Route('/add', name: 'app_crm_promotion_add', methods: ['GET', 'POST'])]
    public function add(Request $request, EntityManagerInterface $entityManager, AppLogger $logger): Response
    {
        $promotion = new Promotion();
        $form = $this->createForm(PromotionType::class, $promotion);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($promotion);
            $entityManager->flush();

            $logger->record('CREATE', "Nouvelle promotion : {$promotion->getName()}");
            $this->addFlash('success', 'Promotion créée avec succès.');

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

        return $this->render('dashboard/promotions/add.twig', [
            'promotion' => $promotion,
            'form' => $form,
            'is_edit' => false,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_crm_promotion_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Promotion $promotion, EntityManagerInterface $entityManager, AppLogger $logger): Response
    {
        $form = $this->createForm(PromotionType::class, $promotion);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->flush();

            $logger->record('UPDATE', "Modification promotion : {$promotion->getName()}");
            $this->addFlash('success', 'Promotion modifiée avec succès.');

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

        return $this->render('dashboard/promotions/add.twig', [
            'promotion' => $promotion,
            'form' => $form,
            'is_edit' => true,
        ]);
    }

    #[Route('/{id}', name: 'app_crm_promotion_delete', methods: ['POST'])]
    public function delete(Request $request, Promotion $promotion, EntityManagerInterface $entityManager, AppLogger $logger): Response
    {
        if ($this->isCsrfTokenValid('delete'.$promotion->getId(), $request->request->get('_token'))) {
            $name = $promotion->getName();
            $entityManager->remove($promotion);
            $entityManager->flush();

            $logger->record('DELETE', "Suppression promotion : $name");
            $this->addFlash('success', 'Promotion supprimée avec succès.');
        }

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