<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Mailer;

use PHPUnit\Framework\TestCase;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;

final class ReviewInvitationTemplateTest extends TestCase
{
    public function testConfiguredTitleIsDisplayedInHtmlAndTextEmails(): void
    {
        $context = array_replace($this->context(null), [
            'email_title' => 'Partagez votre expérience',
        ]);
        $twig = $this->twig();

        self::assertStringContainsString(
            'Partagez votre expérience',
            $twig->render('mail/review_invitation.html.twig', $context),
        );
        self::assertStringContainsString(
            'Partagez votre expérience',
            $twig->render('mail/review_invitation.txt.twig', $context),
        );
    }

    public function testEmailDisplaysOrderProductsAndConfiguredGoogleReviewLink(): void
    {
        $context = $this->context('https://g.page/r/example/review');
        $twig = $this->twig();

        $html = $twig->render('mail/review_invitation.html.twig', $context);
        $text = $twig->render('mail/review_invitation.txt.twig', $context);

        foreach ([$html, $text] as $content) {
            self::assertStringContainsString('CMD-123', $content);
            self::assertStringContainsString('2 × Cuvée Réserve 75 cl', $content);
            self::assertStringContainsString('https://g.page/r/example/review', $content);
        }
    }

    public function testEmailOmitsGoogleReviewCallToActionWhenLinkIsNotConfigured(): void
    {
        $context = $this->context(null);
        $twig = $this->twig();

        self::assertStringNotContainsString(
            'marketing.review_request.mail.google_button',
            $twig->render('mail/review_invitation.html.twig', $context),
        );
        self::assertStringNotContainsString(
            'marketing.review_request.mail.google_button',
            $twig->render('mail/review_invitation.txt.twig', $context),
        );
    }

    public function testEmailSubjectsUseAReviewEmoji(): void
    {
        $twig = $this->twig();
        $context = $this->context(null);

        self::assertStringContainsString(
            'Domaine Exemple aimerait connaître votre avis ⭐',
            $twig->render('mail/review_invitation.html.twig', $context),
        );
        self::assertStringContainsString(
            'Rappel : Domaine Exemple aimerait connaître votre avis ⭐',
            $twig->render('mail/review_invitation.html.twig', array_replace($context, [
                'is_reminder' => true,
            ])),
        );
    }

    private function twig(): Environment
    {
        $twig = new Environment(new FilesystemLoader(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views',
        ));
        $twig->addFilter(new TwigFilter(
            'trans',
            static function (string $key, array $parameters = []): string {
                $message = match ($key) {
                    'marketing.review_request.mail.subject' => '%company% aimerait connaître votre avis ⭐',
                    'marketing.review_request.mail.reminder_subject' => 'Rappel : %company% aimerait connaître votre avis ⭐',
                    'marketing.review_request.mail.order_heading' => 'Commande n° %reference%',
                    'marketing.review_request.mail.product_line' => '%quantity% × %product%',
                    default => $key,
                };

                return strtr($message, $parameters);
            },
        ));
        $twig->addFilter(new TwigFilter(
            'imagine_filter',
            static fn(string $path): string => $path,
        ));
        $twig->addFunction(new TwigFunction(
            'asset',
            static fn(string $path): string => $path,
        ));
        $twig->addFunction(new TwigFunction(
            'absolute_url',
            static fn(string $path): string => $path,
        ));

        return $twig;
    }

    /**
     * @return array<string, mixed>
     */
    private function context(?string $googleBusinessReviewUrl): array
    {
        return [
            'contact' => null,
            'company' => 'Domaine Exemple',
            'email_title' => 'Votre avis compte',
            'cart' => [
                'id' => '00000000-0000-0000-0000-000000000000',
                'token' => 'CMD-123',
                'items' => [[
                    'quantity' => 2,
                    'variant' => [
                        'fullName' => 'Cuvée Réserve 75 cl',
                        'product' => null,
                    ],
                ]],
            ],
            'review_url' => 'https://example.test/review/token',
            'opt_out_url' => 'https://example.test/review/opt-out/token',
            'review_policy_url' => 'https://example.test/review/policy',
            'google_business_review_url' => $googleBusinessReviewUrl,
            'expires_at' => new \DateTimeImmutable('2026-09-01'),
            'locale' => 'fr',
            'is_reminder' => false,
        ];
    }
}
