<?php

namespace App\Security;

use App\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\CustomCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use App\Repository\UserRepository;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use function mysql_xdevapi\getSession;

/**
 *
 */
class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
{
	use TargetPathTrait;
	/**
	 * @var \App\Repository\UserRepository
	 */
	private UserRepository $userRepository;
	/**
	 * @var \Symfony\Component\Routing\RouterInterface
	 */
	private RouterInterface $router;
	
	public function __construct(UserRepository $userRepository, RouterInterface $router)
	{
		$this->userRepository = $userRepository;
		$this->router = $router;
	}
	
	
	public function authenticate(Request $request): Passport
	{
		$username = $request->request->get(key: 'username');
		$password = $request->request->get(key: 'password');
		$csrfToken = $request->request->get(key: '_csrf_token');
		
		$request->getSession()->set(name: Security::LAST_USERNAME, value: $username);
		
		return new Passport(
			userBadge: new UserBadge(userIdentifier: $username, userLoader: function ($userIdentifier) {
				$user = $this->userRepository->findOneBy(['username' => $userIdentifier]);
				
				if (!$user) {
					$user = $this->userRepository->findOneBy(['email' => $userIdentifier]);
				}
				
				if (!$user) {
					throw new UserNotFoundException();
				}
				
				return $user;
			}),
			credentials: new CustomCredentials(customCredentialsChecker: function ($credentials, User $user) {
				return $credentials === 'test';
				
			},                                 credentials             : $password),
			
			// new PasswordCredentials($password),
			badges: [
				new CsrfTokenBadge(csrfTokenId: 'authenticate', csrfToken: $csrfToken),
				new RememberMeBadge()
			]
		);
	}
	
	
	public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
	{
		if ($target = $this->getTargetPath(session: $request->getSession(), firewallName: $firewallName)) {
			return new RedirectResponse(url: $target);
		}

		return new RedirectResponse(
			url: $this->router->generate(name: 'app_main')
		);
	}
	
	
	protected function getLoginUrl(Request $request): string
	{
		return $this->router->generate(name: 'app_login');
	}
}
