<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Tests\Twig;

use CpCreation\VitiCore\Shop\Model\ShopConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Translator;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;

final class ShopBlockedNoticeTemplateTest extends TestCase
{
    #[DataProvider('localeProvider')]
    public function testShopTitleUsesTheMessagesTranslationDomain(string $locale, string $expectedTitle): void
    {
        $bundleDirectory = \dirname(__DIR__, 2) . '/src/Bridge/Symfony/Bundle';
        $translator = new Translator($locale);
        $translator->addLoader('yaml', new YamlFileLoader());
        $translator->addResource(
            'yaml',
            $bundleDirectory . '/Resources/translations/messages.' . $locale . '.yaml',
            $locale,
            'messages',
        );

        $twig = new Environment(new FilesystemLoader($bundleDirectory . '/Resources/views'));
        $twig->addExtension(new TranslationExtension($translator));
        $twig->addFilter(new TwigFilter('country_name', static fn(string $country): string => $country));
        $twig->addFunction(new TwigFunction('path', static fn(string $route): string => '/' . $route));

        $html = $twig->render('shop/partials/blocked_notice.html.twig', [
            'blockedCountryCode' => 'US',
            'config' => new ShopConfig(),
        ]);

        self::assertStringContainsString('>' . $expectedTitle . '<', (string) preg_replace('/\s+/', '', $html));
        self::assertStringNotContainsString('shop.title', $html);
    }

    /**
     * @return iterable<string, array{string, string}>
     */
    public static function localeProvider(): iterable
    {
        yield 'français' => ['fr', 'Boutique'];
        yield 'anglais' => ['en', 'Shop'];
        yield 'néerlandais' => ['nl', 'Winkel'];
    }
}
