<?php

namespace App\Tests\Controller;

use App\Controller\ExternalRedirectController;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Twig\Environment;

class ExternalRedirectControllerTest extends TestCase
{
    private function setupController(ExternalRedirectController $controller): void
    {
        $session = new Session(new MockArraySessionStorage());
        $stack = $this->createStub(RequestStack::class);
        $stack->method('getSession')->willReturn($session);

        $twig = $this->createStub(Environment::class);
        $twig->method('render')->willReturn('<html></html>');

        $container = $this->createStub(ContainerInterface::class);
        $container->method('has')->willReturn(true);
        $container->method('get')->willReturnMap([
            ['twig', $twig],
            ['security.authorization_checker', $this->createStub(AuthorizationCheckerInterface::class)],
            ['security.token_storage', $this->createStub(TokenStorageInterface::class)],
            ['request_stack', $stack],
            ['parameter_bag', $this->createStub(\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface::class)],
        ]);
        $controller->setContainer($container);
    }

    public function testIndexWithUrl(): void
    {
        $controller = new ExternalRedirectController();
        $this->setupController($controller);

        $request = new Request(['redirUrl' => 'https://example.com']);
        $response = $controller->index($request);
        $this->assertSame(200, $response->getStatusCode());
    }

    public function testIndexWithoutUrl(): void
    {
        $controller = new ExternalRedirectController();
        $this->setupController($controller);

        $request = new Request();
        $response = $controller->index($request);
        $this->assertSame(200, $response->getStatusCode());
    }
}
