<?php

declare(strict_types=1);

namespace CpCreation\AltchaBundle\Tests\PublicApi;

use CpCreation\AltchaBundle\Form\AltchaType;
use CpCreation\AltchaBundle\Tests\Fixture\TestKernel;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

final class BundleKernelTest extends TestCase
{
    private ?TestKernel $kernel = null;

    protected function tearDown(): void
    {
        $this->kernel?->shutdown();
    }

    public function testBundleCompilesRouteImportsAndFormThemeRendersOpaqueInertHoneypot(): void
    {
        $this->kernel = new TestKernel('test', true);
        $this->kernel->boot();
        $container = $this->kernel->getContainer()->get('test.service_container');
        $requestStack = $container->get('request_stack');
        self::assertInstanceOf(RequestStack::class, $requestStack);
        $requestStack->push(Request::create('https://example.test/form'));

        $router = $container->get('router');
        self::assertInstanceOf(RouterInterface::class, $router);
        self::assertSame(
            '/_altcha/challenge/custom.form',
            $router->generate('cp_creation_altcha_challenge', ['scope' => 'custom.form']),
        );

        $factory = $container->get('test.altcha.form_factory');
        self::assertInstanceOf(FormFactoryInterface::class, $factory);
        $form = $factory->create(AltchaType::class, null, ['scope' => 'custom.form']);
        $twig = $container->get('test.altcha.twig');
        self::assertInstanceOf(Environment::class, $twig);
        $html = $twig->createTemplate('{{ form_row(form) }}')->render(['form' => $form->createView()]);

        $crawler = new Crawler($html);
        self::assertCount(1, $crawler->filter('.cpcreation-altcha-row'));
        self::assertCount(0, $crawler->filter('label'));
        $honeypot = $crawler->filter('.cpcreation-altcha-honeypot input');
        self::assertCount(1, $honeypot);
        self::assertMatchesRegularExpression('/^cp_creation_altcha\[hp_[a-f0-9]{24}\]$/', (string) $honeypot->attr('name'));
        self::assertNotNull($honeypot->attr('inert'));
        self::assertSame('true', $honeypot->attr('aria-hidden'));
        self::assertSame('-1', $honeypot->attr('tabindex'));
        self::assertSame('off', $honeypot->attr('autocomplete'));
        self::assertSame('honeypot', $honeypot->attr('data-cpcreation-altcha-target'));
        $honeypotMirror = $crawler->filter('#cp_creation_altcha__honeypot');
        self::assertCount(1, $honeypotMirror);
        self::assertSame('uninitialized', $honeypotMirror->attr('value'));
        self::assertSame(
            'cp_creation_altcha__honeypot',
            $crawler->filter('#cp_creation_altcha')->attr('data-cpcreation-altcha-honeypot-input-id-value'),
        );
        self::assertStringContainsString('formToken=', (string) $crawler->filter('altcha-widget')->attr('data-cpcreation-altcha-challenge-url'));
    }
}
