<?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 ReviewModerationTemplateTest extends TestCase
{
    public function testSubmissionEmailContainsReviewDetailsAndModerationActions(): void
    {
        $context = $this->context(
            'https://example.test/admincp/marketing/avis-produits/review-id?intent=publish#review-publish',
            'https://example.test/admincp/marketing/avis-produits/review-id?intent=reject#review-reject',
        );
        $twig = $this->twig();

        foreach ([
            $twig->render('mail/review_submitted.html.twig', $context),
            $twig->render('mail/review_submitted.txt.twig', $context),
        ] as $content) {
            self::assertStringContainsString('Alice D.', $content);
            self::assertStringContainsString('Cuvée Réserve — Brut 75 cl', $content);
            self::assertStringContainsString('2/5', $content);
            self::assertStringContainsString('Titre de l’avis', $content);
            self::assertStringContainsString('Commentaire complet', $content);
            self::assertStringContainsString('intent=publish', $content);
            self::assertStringContainsString('intent=reject', $content);
        }
    }

    public function testSubmissionEmailUsesProductInSubjectAndResponsiveLayout(): void
    {
        $content = $this->twig()->render(
            'mail/review_submitted.html.twig',
            $this->context(null, null),
        );

        self::assertStringContainsString(
            '<title>Nouvel avis reçu pour Cuvée Réserve — Brut 75 cl</title>',
            $content,
        );
        self::assertStringContainsString('name="x-apple-disable-message-reformatting"', $content);
        self::assertStringContainsString('background:#f7f2ea', $content);
        self::assertStringContainsString('class="email-card"', $content);
    }

    public function testDecisionEmailsUseTheStyledLayout(): void
    {
        $twig = $this->twig();
        $context = $this->context(null, null) + [
            'reason_code' => 'incomprehensible',
            'reason_detail' => 'Le commentaire ne contient pas de texte intelligible.',
        ];

        foreach (['mail/review_rejected.html.twig', 'mail/review_withdrawn.html.twig'] as $template) {
            $content = $twig->render($template, $context);

            self::assertStringContainsString('name="x-apple-disable-message-reformatting"', $content);
            self::assertStringContainsString('background:#f7f2ea', $content);
            self::assertStringContainsString('class="email-card"', $content);
            self::assertStringContainsString('Le commentaire ne contient pas de texte intelligible.', $content);
        }
    }

    public function testRejectionEmailUsesProductAndVariantInSubject(): void
    {
        $content = $this->twig()->render(
            'mail/review_rejected.html.twig',
            $this->context(null, null) + [
                'reason_code' => 'incomprehensible',
                'reason_detail' => null,
            ],
        );

        self::assertStringContainsString(
            '<title>Décision concernant votre avis pour Cuvée Réserve — Brut 75 cl</title>',
            $content,
        );
        self::assertStringNotContainsString(
            'Décision concernant votre avis chez Domaine Exemple',
            $content,
        );
    }

    public function testProcessedReviewEmailOnlyLinksToItsAdministrationPage(): void
    {
        $twig = $this->twig();
        $content = $twig->render('mail/review_submitted.txt.twig', $this->context(null, null));

        self::assertStringContainsString(
            'https://example.test/admincp/marketing/avis-produits/review-id',
            $content,
        );
        self::assertStringNotContainsString('intent=publish', $content);
        self::assertStringNotContainsString('intent=reject', $content);
    }

    private function twig(): Environment
    {
        $twig = new Environment(new FilesystemLoader(
            __DIR__ . '/../../src/Bridge/Symfony/Bundle/Resources/views',
        ));
        $twig->addFilter(new TwigFilter(
            'trans',
            static fn(string $key, array $parameters = []): string => strtr(match ($key) {
                'marketing.review.submission_mail.subject' => 'Nouvel avis reçu pour %product%',
                'marketing.review.submission_mail.resubmitted_subject' => 'Nouvelle version d’un avis reçue pour %product%',
                'marketing.review.moderation_mail.subject' => 'Décision concernant votre avis pour %product%',
                default => $key,
            }, $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 $publishUrl, ?string $rejectUrl): array
    {
        return [
            'contact' => null,
            'company' => 'Domaine Exemple',
            'product' => [
                'fullName' => 'Cuvée Réserve',
            ],
            'product_label' => 'Cuvée Réserve — Brut 75 cl',
            'review' => [
                'authorDisplayName' => 'Alice D.',
                'rating' => 2,
                'title' => 'Titre de l’avis',
                'comment' => 'Commentaire complet',
                'submittedAt' => new \DateTimeImmutable('2026-07-23 15:30:00'),
            ],
            'resubmission' => false,
            'locale' => 'fr',
            'moderation_url' => 'https://example.test/admincp/marketing/avis-produits/review-id',
            'publish_url' => $publishUrl,
            'reject_url' => $rejectUrl,
        ];
    }
}
