<?php

namespace Tests\Unit\Services\PromptLandingPageEditor;

use App\Services\PromptLandingPageEditor\MergeEditedDocument;
use Tests\Fixtures\PromptLandingPageEditorDocument;
use Tests\TestCase;

class MergeEditedDocumentTest extends TestCase
{
    public function test_it_keeps_the_original_head_and_replaces_only_the_body(): void
    {
        $original = PromptLandingPageEditorDocument::fullHtml();
        $edited = str_replace(
            'Grow Your Business Faster',
            'Grow Your Business With AI',
            PromptLandingPageEditorDocument::html(),
        );

        $merged = $this->merger()->merge(
            $original,
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
            $edited,
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
        );

        $this->assertStringContainsString('https://cdn.example.test/lp-analytics.js', $merged['html']);
        $this->assertStringContainsString('window.LP_PAGE = { id: 39 };', $merged['html']);
        $this->assertStringContainsString('fonts.googleapis.com', $merged['html']);
        $this->assertStringContainsString('class="lp-body"', $merged['html']);
        $this->assertStringContainsString('Grow Your Business With AI', $merged['html']);
        $this->assertStringNotContainsString('Grow Your Business Faster', $merged['html']);
        $this->assertSame(PromptLandingPageEditorDocument::css(), $merged['css']);
        $this->assertSame(PromptLandingPageEditorDocument::js(), $merged['js']);
    }

    public function test_it_keeps_original_css_when_the_editor_rewrites_rules(): void
    {
        $rewritten = <<<'CSS'
[data-gjs-type="wrapper"] { min-height: 100vh; }
.gjs-selected { outline: 2px solid #3b97e3 !important; }
.hero h1 { font-size: 56px; color: rgb(17, 17, 17); }
CSS;

        $merged = $this->merger()->merge(
            PromptLandingPageEditorDocument::html(),
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
            '<h1>Grow Your Business With AI</h1>',
            $rewritten,
            '',
        );

        $this->assertSame(PromptLandingPageEditorDocument::css(), $merged['css']);
        $this->assertSame(PromptLandingPageEditorDocument::js(), $merged['js']);
        $this->assertStringNotContainsString('data-gjs-type', $merged['css']);
    }

    public function test_it_keeps_a_real_css_edit_that_still_has_the_original_selectors(): void
    {
        $editedCss = PromptLandingPageEditorDocument::css()."\n".'h1 { color: #c00; }';

        $merged = $this->merger()->merge(
            PromptLandingPageEditorDocument::html(),
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
            PromptLandingPageEditorDocument::html(),
            $editedCss,
            PromptLandingPageEditorDocument::js(),
        );

        $this->assertSame($editedCss, $merged['css']);
    }

    public function test_it_does_not_persist_browser_logger_scripts(): void
    {
        $original = PromptLandingPageEditorDocument::fullHtml();
        $edited = <<<'HTML'
<section><h1>Edited</h1></section>
<script id="browser-logger-active">
const ENDPOINT = 'https://example.test/_boost/browser-logs';
</script>
HTML;

        $merged = $this->merger()->merge(
            $original,
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
            $edited,
            PromptLandingPageEditorDocument::css(),
            "const ENDPOINT = 'https://example.test/_boost/browser-logs';",
        );

        $this->assertStringNotContainsString('browser-logger-active', $merged['html']);
        $this->assertStringNotContainsString('_boost/browser-logs', $merged['html']);
        $this->assertStringContainsString('https://cdn.example.test/lp-analytics.js', $merged['html']);
        $this->assertSame(PromptLandingPageEditorDocument::js(), $merged['js']);
    }

    public function test_it_restores_body_scripts_the_editor_dropped(): void
    {
        $original = PromptLandingPageEditorDocument::html()."\n".'<script src="https://cdn.example.test/form-validate.js"></script>';

        $merged = $this->merger()->merge(
            $original,
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
            str_replace('Grow Your Business Faster', 'Grow Your Business With AI', PromptLandingPageEditorDocument::html()),
            PromptLandingPageEditorDocument::css(),
            PromptLandingPageEditorDocument::js(),
        );

        $this->assertStringContainsString('Grow Your Business With AI', $merged['html']);
        $this->assertStringContainsString('<script src="https://cdn.example.test/form-validate.js"></script>', $merged['html']);
    }

    private function merger(): MergeEditedDocument
    {
        return new MergeEditedDocument;
    }
}
