<?php

namespace App\Controller;

use App\Dto\Contact\ContactType;
use App\Dto\Contact\DtoContact;
use App\Dto\Join\JoinType;
use App\Entity\Account;
use App\Entity\AccountResetPasswordRequest;
use App\Entity\Join;
use App\Form\RequestPasswordConfirmType;
use App\Form\RequestPasswordRequestType;
use App\Service\Mailer\Mailer;
use App\Service\Pdf\Candidat;
use App\Service\ResetPassword\Event\ResetPasswordConfirmEvent;
use App\Service\ResetPassword\Event\ResetPasswordEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;

class JoinController extends AbstractController
{
    #[Route(path: '/join/confirmed', name: 'app_recruit_confirmed', options: ['sitemap' => false], methods: ['GET','POST'])]
    public function joinConfirmed(Request $request,EntityManagerInterface $entityManager,Mailer $mailer,KernelInterface $kernel): Response
    {
        return $this->render('join/confirmed.twig', [
            'no_index' => true
        ]);
    }

    #[Route(path: '/join', name: 'app_recruit', options: ['sitemap' => false], methods: ['GET','POST'])]
    public function join(Request $request,EntityManagerInterface $entityManager,Mailer $mailer,KernelInterface $kernel): Response
    {
        $j= new Join();
        $form = $this->createForm(JoinType::class,$j);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()){
            $j->setState("create");
            $j->setCreateAt(new \DateTimeImmutable("now"));
            $j->setIsDiscord(false);
            $cPdf = new Candidat();
            $cPdf->setData($j,$entityManager,$kernel);
            $cPdf->AddPage();
            $cPdf->contentDetails();
            $content = $cPdf->Output('S');
            $fileName = 'fiche_candidat.pdf';
            $tempFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $fileName;
            file_put_contents($tempFilePath, $content);
            $file = new UploadedFile($tempFilePath,$fileName,"application/pdf",0,true);
            $j->setFiche($file);
            $entityManager->persist($j);
            $entityManager->flush();

           $mailer->send('contact@e-cosplay.fr',
                'E-Cosplay',
                '[E-Cosplay] - Nouvelle candidature',
                'mails/candidat/new.twig',
                ['joint'=>$j],
               [new DataPart($content,'candidat.pdf','application/pdf')]
            );


            $mailer->send($j->getEmail(),
                $j->getSurname()." ".$j->getName(),
                "[E-Cosplay] - Confirmation de votre candidature",
                'mails/candidat/confirm.twig',
                ['joint'=>$j],
                [new DataPart($content,'candidat.pdf','application/pdf')]
            );
            return $this->redirectToRoute('app_recruit_confirmed');
        }
        return $this->render('join.twig',[
            'form' => $form->createView(),
        ]);
    }
}
