<?php

namespace App\Tests\Controller;

use App\Controller\DnsReportController;
use App\Repository\EmailTrackingRepository;
use App\Service\AwsSesService;
use App\Service\DnsCheckService;
use App\Service\DnsInfraHelper;
use App\Service\MailcowService;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Twig\Environment;

class DnsReportControllerTest extends TestCase
{
    private function setupController(DnsReportController $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 testNotFound(): void
    {
        $repo = $this->createStub(EmailTrackingRepository::class);
        $repo->method('findOneBy')->willReturn(null);

        $controller = new DnsReportController();
        $this->setupController($controller);

        $this->expectException(NotFoundHttpException::class);
        $controller(
            'invalid-token',
            $this->createStub(DnsCheckService::class),
            $this->createStub(AwsSesService::class),
            $this->createStub(MailcowService::class),
            $this->createStub(DnsInfraHelper::class),
            $repo,
            $this->createStub(CacheInterface::class),
        );
    }
}
