<?php

namespace Tests\Unit\Services;

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

class CatalogLandingPageTrackingDocumentTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_injects_google_conversion_payload_and_cta_click_handler_for_google_traffic(): void
    {
        $page = $this->catalogPage([
            'google_conversion_send_to' => 'AW-16990837573/i9NRCNzf3PkcEMW27qU_',
            'brand_name' => 'Office Depot',
        ]);

        $html = app(CatalogLandingPageTrackingDocument::class)->apply($page, 'google');

        $this->assertStringContainsString("gtag('config', 'AW-16990837573')", $html);
        $this->assertStringContainsString('window.__admediaCatalogGoogle = {"send_to":"AW-16990837573/i9NRCNzf3PkcEMW27qU_"}', $html);
        $this->assertStringContainsString("gtag('event', 'conversion', window.__admediaCatalogGoogle)", $html);
        $this->assertStringContainsString('isCatalogProductCta', $html);
        $this->assertStringNotContainsString('window.__admediaCatalogMetaLead =', $html);
    }

    public function test_it_injects_meta_lead_event_name_and_cta_click_handler_for_facebook_traffic(): void
    {
        $page = $this->catalogPage([
            'meta_pixel_id' => '680344444359627',
            'brand_name' => 'OD',
        ]);

        $html = app(CatalogLandingPageTrackingDocument::class)->apply($page, 'facebook');

        $this->assertStringContainsString("fbq('init', '680344444359627')", $html);
        $this->assertStringContainsString('window.__admediaCatalogMetaLead = "ODLead"', $html);
        $this->assertStringContainsString("fbq('trackCustom', window.__admediaCatalogMetaLead)", $html);
        $this->assertStringContainsString('isCatalogProductCta', $html);
        $this->assertStringNotContainsString('window.__admediaCatalogGoogle =', $html);
    }

    public function test_it_does_not_inject_tracking_when_utm_source_does_not_match(): void
    {
        $page = $this->catalogPage([
            'google_conversion_send_to' => 'AW-16990837573/i9NRCNzf3PkcEMW27qU_',
            'meta_pixel_id' => '680344444359627',
        ]);

        $html = app(CatalogLandingPageTrackingDocument::class)->apply($page, 'bing');

        $this->assertStringNotContainsString('googletagmanager.com', $html);
        $this->assertStringNotContainsString('fbevents.js', $html);
        $this->assertStringNotContainsString('isCatalogProductCta', $html);
    }

    public function test_it_does_not_inject_google_tracking_without_a_saved_send_to_value(): void
    {
        $page = $this->catalogPage([
            'google_conversion_send_to' => null,
        ]);

        $html = app(CatalogLandingPageTrackingDocument::class)->apply($page, 'google');

        $this->assertStringNotContainsString('googletagmanager.com', $html);
        $this->assertStringNotContainsString('isCatalogProductCta', $html);
    }

    /**
     * @param  array<string, mixed>  $generationOverrides
     */
    private function catalogPage(array $generationOverrides = []): LandingPage
    {
        $generation = LandingPageGeneration::factory()
            ->catalog()
            ->withPartnerWebsite()
            ->for(User::factory())
            ->create(array_merge([
                'layout_mode' => LandingPageLayoutMode::Catalog,
            ], $generationOverrides));

        return LandingPage::factory()
            ->for($generation, 'generation')
            ->completed()
            ->create([
                'version' => LandingPageVersion::Benefits,
                'html' => '<section><h1>Office Depot catalog</h1></section>',
            ])
            ->load('generation');
    }
}
