<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

use Symfony\Component\Routing\Annotation\Route;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;

use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;

use App\Security\LoginAuthenticator;

use Doctrine\ORM\EntityManagerInterface;

use App\Entity\User;
use App\Entity\Parrainage;

use App\Form\Frontend\UserRegistrationType;

class SecurityController extends AbstractController
{
    
    #[Route(path: '/login', name: 'frontend_security_login')]
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        if ($this->getUser()) {
            return $this->redirectToRoute('frontend_home_index');
        }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
            
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();
        
        return $this->render('frontend/security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'titlePage' => 'Connexion',]);
    }

    #[Route(path: '/logout', name: 'frontend_security_logout')]
    public function logout(): void
    {
        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
    }


    #[Route(path: '/registration', name: 'frontend_security_registration')]
    public function registration(Request $request, 
                                UserPasswordHasherInterface $userPasswordHasher, 
                                UserAuthenticatorInterface $userAuthenticator,
                                LoginAuthenticator $authenticator, 
                                EntityManagerInterface $em,
                                Session $session): Response
    {
        $user = new User();

        $form = $this->createForm(UserRegistrationType::class, $user);
        $form->handleRequest($request);

        
        if($form->isSubmitted() && $form->isValid()) {
            
            $user->setPassword($userPasswordHasher->hashPassword($user, $form->get('password')->getData()));
            
            $em->persist($user);
            $em->flush();

            $IdOrigine = $session->get('user_parrai');
            if($IdOrigine){
                $parrainage = new Parrainage();
                $parrainage->setNew($user);
                $orginUser = $em->getRepository(User::class)->find($IdOrigine);
                $parrainage->setOrigine($orginUser);
                $em->persist($parrainage);
                $em->flush();
                $session->remove('user_parrai');
            }
            

            return $userAuthenticator->authenticateUser($user, $authenticator, $request);
        }
        
        
        return $this->render('frontend/security/register.html.twig', [
            'form' => $form->createView(),
            'errors' => $form->getErrors(),
            'titlePage' => 'Inscription',
        ]);
    }

    
}
