<?php

declare(strict_types=1);

namespace App\Controller\Admin\Configurations\Modepaiements;

use App\Entity\Parametres\Modepaiements\Modepaiements;
use App\Form\Parametres\Modepaiements\ModepaiementsType;
use App\Managers\ManagersInterface;
use App\Repository\Parametres\Modepaiements\ModepaiementsRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security as Security;

#[Route('/admin/modepaiements')]
#[Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_ADMIN')")]
class AdminModepaiementsController extends AbstractController
{
    public function __construct(private ManagersInterface $managers) {}

    #[Route('/', name: "admin.modepaiements.index", methods: ['GET'])]
    /**
     * @param ModepaiementsRepository $modepaiementsRepository
     * @return Response
     */
    public function index(ModepaiementsRepository $modepaiementsRepository): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $modepaiement = new Modepaiements();
        $form = $this->createForm(ModepaiementsType::class, $modepaiement);

        return $this->render('admin/configurations/modepaiements/index.html.twig', [
            'modepaiements' => $modepaiementsRepository->findAll(),
            'form' => $form->createView(),
            'title' => 'Liste des modes de paiement',
            'libelle_ajouter' => 'Ajouter un mode',
            'current_page' => 'modepaiements',
            'current_global' => 'configurations'
        ]);
    }

    #[Route('/new', name: "admin.modepaiements.new", methods: ['GET', 'POST'])]
    /**
     * @param Request $request
     * @return Response
     */
    public function new(Request $request): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $modepaiement = new Modepaiements();
        $form = $this->createForm(ModepaiementsType::class, $modepaiement);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->managers->setEntity($modepaiement, 'new');
            $this->addFlash('success', 'Mode de paiement ajouté avec succès.');
            return $this->redirectToRoute('admin.modepaiements.index');
        }

        return $this->render('admin/configurations/modepaiements/new.html.twig', [
            'modepaiement' => $modepaiement,
            'form' => $form->createView(),
            'title' => 'Nouveau mode de paiement',
            'libelle_liste' => 'Liste des modes de paiement',
            'current_page' => 'modepaiements',
            'current_global' => 'configurations'
        ]);
    }

    #[Route('/{id<\d+>}/edit', name: "admin.modepaiements.edit", methods: ['GET', 'POST'])]
    /**
     * @param Request $request
     * @param Modepaiements $modepaiement
     * @return Response
     */
    public function edit(Request $request, Modepaiements $modepaiement): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        $form = $this->createForm(ModepaiementsType::class, $modepaiement);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->managers->setEntity($modepaiement, 'edit');
            $this->addFlash('success', 'Mode de paiement modifié avec succès.');
            return $this->redirectToRoute('admin.modepaiements.edit', [
                'id' => $modepaiement->getId(),
                '_locale' => $request->get('_locale')
            ]);
        }

        return $this->render('admin/configurations/modepaiements/edit.html.twig', [
            'modepaiement' => $modepaiement,
            'form' => $form->createView(),
            'title' => 'Editer mode de paiement',
            'libelle_ajouter' => 'Nouvelle modes',
            'libelle_liste' => 'Liste des modes de paiement',
            'current_page' => 'modepaiements',
            'current_global' => 'configurations'
        ]);
    }

    #[Route('/{id<\d+>}', name: "admin.modepaiements.delete", methods: ['DELETE'])]
    /**
     * @param Request $request
     * @param Modepaiements $modepaiement
     * @return Response
     */
    public function delete(Request $request, Modepaiements $modepaiement): Response
    {
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
        if ($this->isCsrfTokenValid('delete' . $modepaiement->getId(), $request->request->get('_token'))) {
            $this->managers->setEntity($modepaiement, 'delete');
            $this->addFlash('success', 'Mode de paiement supprimé avec succès.');
        }

        return $this->redirectToRoute('admin.modepaiements.index');
    }
}
