<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\TransactionalEmailType;
use CpCreation\VitiCore\TransactionalEmail\Model\TransactionalEmail;
use CpCreation\VitiCore\TransactionalEmail\Repository\TransactionalEmailRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_TRANSACTIONAL_EMAIL')"))]
#[Route('/transactional-emails')]
final class TransactionalEmailController extends AbstractController
{
    public function __construct(
        private readonly TransactionalEmailRepository $transactionalEmailRepository,
    ) {
    }

    #[Route('', name: 'admin_transactional_email')]
    public function index(): Response
    {
        return $this->render('@CpCreationVitiCore/admin/transactional_email/index.html.twig', [
            'items' => $this->transactionalEmailRepository->findBy([], ['name' => 'ASC']),
        ]);
    }

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

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var TransactionalEmail $transactionalEmail */
            $transactionalEmail = $form->getData();
            $this->transactionalEmailRepository->save($transactionalEmail);
            $this->addFlash('alert-success', 'L\'email transactionnel '.$transactionalEmail->getName().' a été ajouté.');

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

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

    #[Route('/{id}/edit', name: 'admin_transactional_email_edit')]
    public function edit(TransactionalEmail $transactionalEmail, Request $request): Response
    {
        $form = $this->createForm(TransactionalEmailType::class, $transactionalEmail);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var TransactionalEmail $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTimeImmutable());
            $this->transactionalEmailRepository->save($data);
            $this->addFlash('alert-success', 'L\'email transactionnel '.$data->getName().' a été modifié.');

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

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

    #[Route('/{id}/delete', name: 'admin_transactional_email_delete')]
    public function delete(TransactionalEmail $transactionalEmail): RedirectResponse
    {
        $name = $transactionalEmail->getName();
        $this->transactionalEmailRepository->delete($transactionalEmail);
        $this->addFlash('alert-success', 'L\'email transactionnel '.$name.' a été supprimé.');

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

    #[Route('/{id}/disable', name: 'admin_transactional_email_disable')]
    public function disable(TransactionalEmail $transactionalEmail): RedirectResponse
    {
        $transactionalEmail->setDisabled(true);
        $this->transactionalEmailRepository->save($transactionalEmail);
        $this->addFlash('alert-success', 'L\'email transactionnel '.$transactionalEmail->getName().' a été désactivé.');

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

    #[Route('/{id}/enable', name: 'admin_transactional_email_enable')]
    public function enable(TransactionalEmail $transactionalEmail): RedirectResponse
    {
        $transactionalEmail->setDisabled(false);
        $this->transactionalEmailRepository->save($transactionalEmail);
        $this->addFlash('alert-success', 'L\'email transactionnel '.$transactionalEmail->getName().' a été activé.');

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