<?php

namespace Tests\Unit\Services;

use App\Enums\LandingPageVersion;
use App\Models\CatalogLandingPageVisit;
use App\Models\LandingPage;
use App\Models\LandingPageGeneration;
use App\Models\User;
use App\Services\CatalogLandingPageVisitDocument;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class CatalogLandingPageVisitDocumentTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_injects_the_page_load_id_and_clickout_endpoint(): void
    {
        $generation = LandingPageGeneration::factory()
            ->catalog()
            ->withPartnerWebsite()
            ->withAffiliateTracking()
            ->for(User::factory())
            ->create();

        $page = LandingPage::factory()
            ->for($generation, 'generation')
            ->completed()
            ->create([
                'version' => LandingPageVersion::Benefits,
                'html' => '<html><body><a class="row-cta" href="https://example.com/product">Shop</a></body></html>',
            ]);

        $visit = CatalogLandingPageVisit::factory()->for($page, 'landingPage')->make([
            'page_load_id' => '11111111-2222-4333-8444-555555555555',
        ]);

        $html = app(CatalogLandingPageVisitDocument::class)->apply($page, $page->renderedDocument(), $visit);

        $this->assertStringContainsString('__admediaCatalogPageLoadId = "11111111-2222-4333-8444-555555555555"', $html);
        $this->assertStringContainsString('__admediaCatalogClickoutUrl', $html);
        $this->assertStringContainsString('recordCatalogClickout', $html);
    }
}
