<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Mailer;

use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormView;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;

final class MarketingTestEmailModalTemplateTest extends TestCase
{
    public function testEmailTitleModalImportsAndRendersA2lixMacro(): void
    {
        $loader = new FilesystemLoader(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views',
        );
        $a2lixReflection = new \ReflectionClass(TranslationsType::class);
        $a2lixFile = $a2lixReflection->getFileName();
        self::assertNotFalse($a2lixFile);
        $loader->addPath(
            \dirname($a2lixFile, 4) . '/src/Resources/views',
            'A2lixTranslationForm',
        );

        $twig = new Environment($loader);
        $twig->addGlobal('app', (object) [
            'request' => (object) ['locale' => 'fr'],
        ]);
        $twig->addFilter(new TwigFilter('trans', static fn(string $key): string => $key));
        $twig->addFilter(new TwigFilter('humanize', static fn(string $value): string => $value));
        $twig->addFunction(new TwigFunction('form_errors', static fn(): string => ''));
        $twig->addFunction(new TwigFunction(
            'form_row',
            static fn(FormView $view): string => sprintf(
                '<span data-field="%s"></span>',
                $view->vars['name'],
            ),
            ['is_safe' => ['html']],
        ));

        $translations = new FormView();
        $translations->vars = [
            'default_locale' => 'fr',
            'valid' => true,
        ];
        foreach (['fr', 'en'] as $locale) {
            $translation = new FormView($translations);
            $translation->vars = [
                'id' => 'configuration_translations_' . $locale,
                'label' => mb_strtoupper($locale),
                'name' => $locale,
                'required' => false,
            ];
            $emailTitle = new FormView($translation);
            $emailTitle->vars = ['name' => 'emailTitle'];
            $translation->children['emailTitle'] = $emailTitle;
            $translations->children[$locale] = $translation;
        }

        $html = $twig->render('admin/marketing/_email_title_modal.html.twig', [
            'modal_id' => 'emailTitleModal',
            'title_key' => 'marketing.review_request.configuration.email_content_title',
            'help_key' => 'marketing.review_request.configuration.email_content_help',
            'translations_form' => $translations,
            'fields' => ['emailTitle'],
        ]);

        self::assertStringContainsString('data-target="#emailTitleModal"', $html);
        self::assertSame(2, substr_count($html, 'data-field="emailTitle"'));
        self::assertStringContainsString('FR', $html);
        self::assertStringContainsString('EN', $html);
    }

    public function testButtonAndModalCanBeRenderedOutsideOfEachOthersForms(): void
    {
        $twig = new Environment(new FilesystemLoader(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views',
        ));
        $twig->addFilter(new TwigFilter('trans', static fn(string $key): string => $key));
        $twig->addFunction(new TwigFunction('csrf_token', static fn(string $id): string => $id));
        $context = [
            'modal_id' => 'testEmailModal',
            'action' => '/email-test',
            'recipient' => 'admin@example.com',
            'csrf_token_id' => 'email-test',
        ];

        $button = $twig->render('admin/marketing/_test_email_modal.html.twig', [
            ...$context,
            'show_modal' => false,
        ]);
        $modal = $twig->render('admin/marketing/_test_email_modal.html.twig', [
            ...$context,
            'show_button' => false,
        ]);

        self::assertStringContainsString('data-target="#testEmailModal"', $button);
        self::assertStringNotContainsString('<form', $button);
        self::assertStringNotContainsString('data-target="#testEmailModal"', $modal);
        self::assertStringContainsString('<form method="post" action="/email-test">', $modal);
        self::assertStringContainsString('value="admin@example.com"', $modal);
    }
}
