<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Marketing\Emailing;

use CpCreation\VitiCore\Marketing\Emailing\UtmLinkEnricher;
use CpCreation\VitiCore\Marketing\Model\EmailCampaign;
use PHPUnit\Framework\TestCase;

final class UtmLinkEnricherTest extends TestCase
{
    public function testItEnrichesEveryHttpLinkAndPreservesQueryAndFragment(): void
    {
        $campaign = new EmailCampaign(
            'Vendanges 2026',
            'Nos vendanges',
            "Voir https://example.com/cuvee?year=2026#details.\nEt http://shop.example/boutique",
            42,
            [7],
            'Vendanges 2026',
            'email',
            'juillet',
            null,
            'corps',
        );

        $html = new UtmLinkEnricher()->enrichPlainText($campaign);

        self::assertStringContainsString('year=2026&amp;utm_source=Vendanges%202026', $html);
        self::assertStringContainsString('utm_medium=email', $html);
        self::assertStringContainsString('utm_campaign=juillet', $html);
        self::assertStringContainsString('utm_content=corps#details', $html);
        self::assertSame(2, substr_count($html, '<a href='));
    }

    public function testManualUtmValuesReplaceExistingOnes(): void
    {
        $campaign = new EmailCampaign(
            'Titre',
            'Objet',
            'https://example.com/?utm_source=ancien',
            1,
            [1],
            'manuel',
            null,
            null,
            null,
            null,
        );

        $html = new UtmLinkEnricher()->enrichPlainText($campaign);

        self::assertStringContainsString('utm_source=manuel', $html);
        self::assertStringNotContainsString('utm_source=ancien', $html);
    }
}
