<?php

namespace App\Tests\Security;

use App\Entity\Customer;
use App\Security\CustomerAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;

#[AllowMockObjectsWithoutExpectations]
class CustomerAuthenticatorTest extends TestCase
{
    private $urlGenerator;
    private $entityManager;
    private $authenticator;

    protected function setUp(): void
    {
        $this->urlGenerator = $this->createMock(UrlGeneratorInterface::class);
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
        $this->authenticator = new CustomerAuthenticator($this->urlGenerator, $this->entityManager);
    }

    public function testSupports()
    {
        $request = Request::create('/reservation/login', 'POST');
        $request->attributes->set('_route', 'reservation_login');
        
        $this->assertTrue($this->authenticator->supports($request));

        $requestInvalid = Request::create('/reservation/login', 'GET');
        $requestInvalid->attributes->set('_route', 'reservation_login');
        $this->assertFalse($this->authenticator->supports($requestInvalid));
    }

    public function testAuthenticate()
    {
        $request = Request::create('/login', 'POST', [
            '_username' => 'test@test.com',
            '_password' => 'password',
            '_csrf_token' => 'token'
        ]);
        
        $session = $this->createMock(SessionInterface::class);
        $request->setSession($session);

        $repository = $this->createMock(EntityRepository::class);
        $this->entityManager->method('getRepository')->with(Customer::class)->willReturn($repository);
        $repository->method('findOneBy')->with(['email' => 'test@test.com'])->willReturn(new Customer());

        $passport = $this->authenticator->authenticate($request);

        $this->assertInstanceOf(Passport::class, $passport);
        $this->assertTrue($passport->hasBadge(UserBadge::class));
        $this->assertTrue($passport->hasBadge(CsrfTokenBadge::class));
    }

    public function testOnAuthenticationSuccessRedirectsToReservation()
    {
        $request = Request::create('/login');
        $session = $this->createMock(SessionInterface::class);
        $request->setSession($session);
        
        $token = $this->createMock(TokenInterface::class);

        $this->urlGenerator->expects($this->once())
            ->method('generate')
            ->with('reservation')
            ->willReturn('/reservation');

        $response = $this->authenticator->onAuthenticationSuccess($request, $token, 'main');

        $this->assertInstanceOf(RedirectResponse::class, $response);
        $this->assertEquals('/reservation', $response->getTargetUrl());
    }
}
