<?php

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

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\NewsletterType;
use CpCreation\VitiCore\Newsletter\Model\Newsletter;
use CpCreation\VitiCore\Newsletter\Repository\NewsletterRepository;
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('/newsletter')]
#[IsGranted(new Expression("is_granted('ROLE_ADMIN') or is_granted('ROLE_NEWSLETTER')"))]
final class NewsletterController extends AbstractController
{
    public function __construct(
        private readonly NewsletterRepository $newsletterRepository,
    ) {}

    #[Route('', name: 'admin_newsletter')]
    public function index(): Response
    {
        $newsletters = $this->newsletterRepository->findBy([], ['createdAt' => 'DESC']);

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

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

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

            $this->newsletterRepository->save($data);
            $this->addFlash('alert-success', 'La newsletter '.$data->getTitle().' a été ajoutée avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_newsletter_edit')]
    public function edit(Newsletter $newsletter, Request $request): Response
    {
        $form = $this->createForm(NewsletterType::class, $newsletter);
        $form->handleRequest($request);

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

            // Si tu veux forcer un update Doctrine :
            $data->setUpdatedAt(new \DateTimeImmutable());

            $this->newsletterRepository->save($data);
            $this->addFlash('alert-success', 'La newsletter '.$newsletter->getTitle().' a été modifiée avec succès');

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

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

    #[Route('/{id}/delete', name: 'admin_newsletter_delete')]
    public function delete(Newsletter $newsletter): Response
    {
        $this->newsletterRepository->delete($newsletter);
        $this->addFlash('alert-success', 'La newsletter '.$newsletter->getTitle().' a été supprimée avec succès.');

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