<?php

namespace App\Controller;

use App\Entity\News;
use App\Entity\Contact;
use App\Form\ContactType;
use App\Service\MailService;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Routing\Attribute\Route;

final class PageController extends AbstractController
{
    private EntityManagerInterface $entityManager;
    private MailService $mailService;

    public function __construct(EntityManagerInterface $entityManager, MailService $mailService)
    {
        $this->entityManager = $entityManager;
        $this->mailService = $mailService;
    }

    #[Route('/', name: 'home')]
    public function index(): Response
    {
        return $this->render('page/home.html.twig', []);
    }

    #[Route('/a-propos', name: 'about')]
    public function about(): Response
    {
        return $this->render('page/about.html.twig', []);
    }

    #[Route('/projets-artistiques', name: 'project')]
    public function project(): Response
    {
        return $this->render('page/project.html.twig', []);
    }

    #[Route('/projets-manuels-et-creatifs', name: 'manualproject')]
    public function manualproject(): Response
    {
        return $this->render('page/manual-project.html.twig', []);
    }

    #[Route('/spectacles', name: 'show')]
    public function show(): Response
    {
        return $this->render('page/show.html.twig', []);
    }

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

        if ($form->isSubmitted() && $form->isValid()) {
            $contact = new Contact();
            $contact->setSendAt(new DateTimeImmutable());
            $data = $form->getData();

            $honeyPot = $form->getData()['mobilePhoneNumber'];
            if ($honeyPot !== NULL) {
                $contact->setIsValid(false);
                $this->addFlash('success', 'Votre message a été envoyé avec succès.');
                $this->entityManager->persist($contact);
                $this->entityManager->flush();
                return $this->redirectToRoute('contact');
            }
            try {
                $this->mailService->sendContactMail($data);
                $contact->setIsValid(true);
                $this->addFlash('success', 'Votre message a été envoyé avec succès.');
            } catch (TransportExceptionInterface $e) {
                $contact->setIsValid(false);
                $this->addFlash('error', 'Une erreur est survenue lors de l’envoi du message.');
            }
            $this->entityManager->persist($contact);
            $this->entityManager->flush();
            return $this->redirectToRoute('contact');
        }

        return $this->render('page/contact.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    #[Route('/mentions-legales', name: 'legalmentions')]
    public function legalmentions(): Response
    {
        return $this->render('page/legal-mentions.html.twig', []);
    }

    #[Route('/politique-de-confidentialite', name: 'confidentialitypolicy')]
    public function confidentialitypolicy(): Response
    {
        return $this->render('page/confidentiality-policy.html.twig', []);
    }

    #[Route('/actualites/{slug}', name: 'actualites')]
    public function actualites(string $slug): Response
    {
        $news = $this->entityManager->getRepository(News::class)->findOneBy(['slug' => $slug]);
        return $this->render('page/news.html.twig', ['news' => $news]);
    }
}
