<?php

namespace Tests\Unit\Models;

use App\Enums\LandingPageVersion;
use App\Models\LandingPage;
use App\Models\LandingPageGeneration;
use Tests\TestCase;

class LandingPageTest extends TestCase
{
    public function test_preview_copies_the_stored_body_classes(): void
    {
        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
            'title' => 'Catalog page',
            'html' => '<body class="dermstore-theme aggressive-variant"><header class="topbar">Office Depot</header></body>',
            'css' => '.page-title { color: navy; }',
            'js' => '',
        ]);

        $document = $page->renderedDocument();

        $this->assertStringContainsString('<body class="dermstore-theme aggressive-variant">', $document);
        $this->assertStringContainsString('<header class="topbar">Office Depot</header>', $document);
        $this->assertSame(1, substr_count($document, '<body'));
    }

    public function test_preview_keeps_a_plain_body_when_html_is_a_fragment(): void
    {
        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
            'title' => 'Catalog page',
            'html' => '<header class="topbar">Office Depot</header>',
            'css' => '',
            'js' => '',
        ]);

        $document = $page->renderedDocument();

        $this->assertStringContainsString('<body>', $document);
        $this->assertStringContainsString('<header class="topbar">Office Depot</header>', $document);
    }

    public function test_preview_keeps_the_stored_head_on_a_full_document(): void
    {
        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
            'title' => 'Catalog page',
            'html' => <<<'HTML'
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Keep me</title>
    <link rel="stylesheet" href="style.css">
</head>
<body class="bg-light-gradient">
    <h1>Watering Can</h1>
</body>
</html>
HTML,
            'css' => '.header { color: red; }',
            'js' => 'console.log("catalog")',
        ]);

        $document = $page->renderedDocument();

        $this->assertStringContainsString('<title>Keep me</title>', $document);
        $this->assertStringContainsString('<link rel="stylesheet" href="style.css">', $document);
        $this->assertStringContainsString('<body class="bg-light-gradient">', $document);
        $this->assertStringContainsString('.header { color: red; }', $document);
        $this->assertStringContainsString('console.log("catalog")', $document);
        $this->assertSame(1, substr_count($document, '<head>'));
    }

    public function test_purpose_slugs_round_trip_catalog_versions(): void
    {
        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
        ]);

        $this->assertSame('catalog-benefits', $page->purposeSlug());
        $this->assertSame(LandingPageVersion::Benefits, LandingPage::versionFromPurposeSlug('catalog-benefits'));
        $this->assertSame(LandingPageVersion::Comparison, LandingPage::versionFromPurposeSlug('catalog-comparison'));
        $this->assertNull(LandingPage::versionFromPurposeSlug('leads-page'));
    }

    public function test_partner_preview_url_uses_the_publish_website_host(): void
    {
        $generation = new LandingPageGeneration([
            'website_url' => 'https://www.attestpath.ai',
        ]);

        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
            'status' => 'completed',
            'html' => '<section>Catalog</section>',
        ]);
        $page->setRelation('generation', $generation);
        $page->id = 42;

        $this->assertSame('https://attestpath.ai/lp/catalog-benefits/42', $page->partnerPreviewUrl());
    }

    public function test_partner_preview_url_is_null_without_a_publish_website(): void
    {
        $generation = new LandingPageGeneration([
            'website_url' => null,
        ]);

        $page = new LandingPage([
            'version' => LandingPageVersion::Benefits,
            'status' => 'completed',
            'html' => '<section>Catalog</section>',
        ]);
        $page->setRelation('generation', $generation);
        $page->id = 42;

        $this->assertNull($page->partnerPreviewUrl());
    }
}
