<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

use App\Entity\PartyRequest;
use App\Entity\Gamemaster;
use App\Entity\Game;
use App\Entity\Event;
use App\Repository\GamemasterRepository;
use App\Repository\GameRepository;
use App\Repository\PartyRequestRepository;
use App\Form\PartyRequestType;

final class PartyRequestController extends AbstractController
{
    #[Route('/party/request/{id}', name: 'app_party_request', requirements: ['id' => Requirement::UUID_V7], methods: ['GET', 'POST'])]
    public function request(?Event $event, Request $request, GamemasterRepository $gamemasterRepository, GameRepository $gameRepository, EntityManagerInterface $manager, MailerInterface $mailer): Response
    {
        // Pas d'événement trouvé ?
        if (!$event) {
            $this->addFlash('danger', 'Aucun événement avec cet ID !');
            $this->redirectToRoute('app_main');
        }

        // L'événement n'autorise pas les demandes ?
        if (!$event->isEveryoneCanAskForGame()) {
            $this->addFlash('danger', 'Cet événement n\'autorise pas les demandes.');
            $this->redirectToRoute('app_main_booking', ['id' => $event->getId(), 'view' => 'pictures']);
        }

        // Pas d'utilisateur connecté ?
        $user = $this->getUser();
        if (!$user) {
            $this->addFlash('danger', 'Les demandes nécessitent d\'être connecté(e).');
            $this->redirectToRoute('app_main_booking', ['id' => $event->getId(), 'view' => 'pictures']);
        }

        // On génère un formulaire
        $partyRequest = new PartyRequest();
        $partyRequest->setEvent($event);
        $partyRequest->setRequester($user);
        $partyRequest->setDateRequest(new \Datetime('now'));

        $form = $this->createForm(PartyRequestType::class, $partyRequest);

        // Traitement du formulaire
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $gamemasterSelectedId = $request->request->get('party_gamemaster');
            $gamemasterSelected = $gamemasterRepository->findByStrID($gamemasterSelectedId);
            $gameSelectedId = $request->request->get('party_game');
            $gameSelected = $gameRepository->findByStrID($gameSelectedId);
            $partyRequest->setGameChoosen($gameSelected);
            $partyRequest->setGamemasterChoosen($gamemasterSelected);
            $manager->persist($partyRequest);
            $manager->flush();

            // TODO: peut-être envoyer un mail de confirmation ??

            $this->addFlash('success', 'Demande enregistrée, les gestionnaires y répondront prochainement.');
            $referer = $request->headers->get('referer'); 
            return $this->redirect($referer);
        }

        return $this->render('party_request/_modal.request.html.twig', [
            'form' => $form,
            'partyRequest' => $partyRequest,
            'event' => $event,
            'pathController' => 'app_party_request'
        ]);
    }

    #[Route('/party/request/{id}/delete', name: 'app_party_request_delete', requirements: ['id' => Requirement::UUID_V7], methods: ['GET'])]
    public function delete(?PartyRequest $partyRequest, Request $request, EntityManagerInterface $entityManager)
    {
        if (!$partyRequest) {
            $this->addFlash('danger', 'Cette demande n\'existe pas.');
        } else {
            $entityManager->remove($partyRequest);
            $entityManager->flush();
        }

        $referer = $request->headers->get('referer'); 
        return $this->redirect($referer);
    }
}
