<?php

namespace Tests\Unit\Support;

use App\Support\PromptLandingPagePixels;
use Tests\TestCase;

class PromptLandingPagePixelsTest extends TestCase
{
    public function test_it_maps_utm_source_to_the_google_or_meta_pixel(): void
    {
        $this->assertSame(PromptLandingPagePixels::GOOGLE, PromptLandingPagePixels::provider('google'));
        $this->assertSame(PromptLandingPagePixels::GOOGLE, PromptLandingPagePixels::provider(' Google '));
        $this->assertSame(PromptLandingPagePixels::META, PromptLandingPagePixels::provider('facebook'));
        $this->assertSame(PromptLandingPagePixels::META, PromptLandingPagePixels::provider('fb'));
        $this->assertSame(PromptLandingPagePixels::META, PromptLandingPagePixels::provider('meta'));
        $this->assertNull(PromptLandingPagePixels::provider('bing'));
        $this->assertNull(PromptLandingPagePixels::provider(null));
        $this->assertNull(PromptLandingPagePixels::provider(''));
    }

    public function test_it_extracts_the_google_ads_account_id_from_send_to(): void
    {
        $this->assertSame(
            'AW-16990837573',
            PromptLandingPagePixels::googleAccountId('AW-16990837573/i9NRCNzf3PkcEMW27qU_'),
        );
        $this->assertNull(PromptLandingPagePixels::googleAccountId('not-an-id'));
    }

    public function test_it_rejects_pixel_ids_that_are_not_safe_to_embed(): void
    {
        $this->assertTrue(PromptLandingPagePixels::isSafeId('AW-16990837573/i9NRCNzf3PkcEMW27qU_'));
        $this->assertTrue(PromptLandingPagePixels::isSafeId('680344444359627'));
        $this->assertFalse(PromptLandingPagePixels::isSafeId("AW-1';alert(1)//"));
        $this->assertFalse(PromptLandingPagePixels::isSafeId(''));
    }

    public function test_it_accepts_google_send_to_and_numeric_meta_pixel_ids(): void
    {
        $this->assertTrue(PromptLandingPagePixels::isGoogleSendTo('AW-16990837573/i9NRCNzf3PkcEMW27qU_'));
        $this->assertFalse(PromptLandingPagePixels::isGoogleSendTo('680344444359627'));
        $this->assertTrue(PromptLandingPagePixels::isMetaPixelId('680344444359627'));
        $this->assertFalse(PromptLandingPagePixels::isMetaPixelId('AW-16990837573/i9NRCNzf3PkcEMW27qU_'));
        $this->assertFalse(PromptLandingPagePixels::isMetaPixelId("12';alert(1)//"));
    }
}
