<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

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

use Symfony\Component\Routing\Attribute\Route;

use Symfony\Bundle\SecurityBundle\Security;

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\Form\UserRegistrationType;


use Symfony\Contracts\Translation\TranslatorInterface;

#[Route('/authentication')]
final class AuthenticationController extends AbstractController
{
    #[Route('/sign-in', name: 'app_sign_in')]
    public function sign_in(TranslatorInterface $translator, AuthenticationUtils $authenticationUtils): Response
    {
        //login
        if ($this->getUser()) {
            return $this->redirectToRoute('app_home');
        }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
            
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('authentication/sign-in.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'titlePage' => 'Connexion']);
    }

    #[Route('/sign-up', name: 'app_sign_up')]
    public function sign_up(Request $request, 
                                UserPasswordHasherInterface $userPasswordHasher, 
                                UserAuthenticatorInterface $userAuthenticator,
                                LoginAuthenticator $authenticator, 
                                EntityManagerInterface $em): 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()));
            $user->setTranslator($request->getSession()->get('_locale'));

            $em->persist($user);
            $em->flush();
            
            return $userAuthenticator->authenticateUser($user, $authenticator, $request);
        }

        return $this->render('authentication/sign-up.html.twig', [
            'form' => $form->createView(),
            'errors' => $form->getErrors(),
            'titlePage' => 'Inscription',
        ]);
    }

    #[Route('/recover-pw', name: 'app_recover_pw')]
    public function recover_pw(): Response
    {
        return $this->render('authentication/recoverpw.html.twig', []);
    }

    #[Route(path: '/sign_out', name: 'app_sign_out')]
    public function logout(Security $security): RedirectResponse
    {
        $response = $security->logout(false);

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

    #[Route('/confirm-mail', name: 'app_confirm_mail')]
    public function confirm_mail(): Response
    {
        return $this->render('authentication/confirm-mail.html.twig', []);
    }


    #[Route('/change-language', name: 'change_language')]
    public function change_language(Request $request): RedirectResponse
    {
        $language = $request->query->get('lang');
        
        // Stocker la langue dans la session
        $request->getSession()->set('_locale', $language);

        $request->setLocale($language);
        
        // Rediriger vers la page précédente ou l'accueil
        $referer = $request->headers->get('referer');
        
        return $this->redirect($referer);
       
    }
   
}

