<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Controller;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Site\ProductReviewPolicyController;
use CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Site\ReviewSolicitationOptOutController;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

final class ProductReviewPolicyControllerTest extends TestCase
{
    public function testPublicPolicyIsExposedAsAnIndexableLocalizedPage(): void
    {
        $routes = $this->routes();
        $route = $routes['shop_review_policy'];

        self::assertSame(ProductReviewPolicyController::class, $route['controller']);
        self::assertSame(['GET'], $route['methods']);
        self::assertTrue($route['options']['sitemap']);
        self::assertSame('/boutique/politique-des-avis', $route['path']['fr']);
    }

    public function testOptOutPageDelegatesConfirmationToTheLiveComponent(): void
    {
        $routes = $this->routes();
        $route = $routes['shop_review_solicitation_opt_out'];

        self::assertSame(ReviewSolicitationOptOutController::class, $route['controller']);
        self::assertSame(['GET'], $route['methods']);
        self::assertSame('[A-Za-z0-9_-]{43}', $route['requirements']['token']);

        $template = file_get_contents(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views/marketing/review/opt_out.html.twig',
        );

        self::assertIsString($template);
        self::assertStringContainsString(
            '<twig:marketing:ReviewSolicitationOptOut :token="token" />',
            $template,
        );
        self::assertStringNotContainsString('<form', $template);
    }

    /** @return array<string, mixed> */
    private function routes(): array
    {
        return Yaml::parseFile(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/config/routes/site_shop.yaml',
        );
    }
}
