<?php

namespace App\Tests\Security;

use App\Entity\Customer;
use App\Entity\OrderSession;
use App\Security\FlowAuthenticator;
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 FlowAuthenticatorTest 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 FlowAuthenticator($this->urlGenerator, $this->entityManager);
    }

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

    public function testAuthenticate()
    {
        $request = Request::create('/reservation/flow', '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));
    }

    public function testOnAuthenticationSuccess()
    {
        $request = Request::create('/reservation/flow');
        $request->attributes->set('sessionId', 'session-123');
        
        $token = $this->createMock(TokenInterface::class);
        $customer = new Customer();
        $token->method('getUser')->willReturn($customer);

        $sessionRepo = $this->createMock(EntityRepository::class);
        $orderSession = $this->createMock(OrderSession::class);

        $this->entityManager->method('getRepository')->with(OrderSession::class)->willReturn($sessionRepo);
        $sessionRepo->method('findOneBy')->with(['uuid' => 'session-123'])->willReturn($orderSession);
        
        $orderSession->expects($this->once())->method('setCustomer')->with($customer);
        $this->entityManager->expects($this->once())->method('flush');

        $this->urlGenerator->expects($this->once())
            ->method('generate')
            ->with('reservation_flow', ['sessionId' => 'session-123'])
            ->willReturn('/reservation/flow/session-123');

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

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