<?php

namespace Tests\Unit\Services\PromptLandingPageEditor;

use App\Enums\PromptLandingPageVersionKind;
use App\Models\PromptLandingPage;
use App\Models\User;
use App\Services\PromptLandingPageEditor\MergeEditedDocument;
use App\Services\PromptLandingPageEditor\RecordPublishedVersion;
use App\Services\PromptLandingPageEditor\SanitizeDocument;
use App\Services\PromptLandingPageEditor\SavePage;
use App\Services\PromptLandingPageEditor\UniqueDuplicateIds;
use App\Services\PromptLandingPageHtmlSanitizer;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Fixtures\PromptLandingPageEditorDocument;
use Tests\TestCase;

class SavePageTest extends TestCase
{
    use RefreshDatabase;

    public function test_save_snapshots_the_original_page_then_writes_the_new_document(): void
    {
        $user = User::factory()->create();
        $page = PromptLandingPage::factory()->approved()->for($user)->create([
            'html' => PromptLandingPageEditorDocument::html(),
            'css' => PromptLandingPageEditorDocument::css(),
            'js' => PromptLandingPageEditorDocument::js(),
        ]);

        $saved = $this->savePage()->handle(
            $page,
            $user,
            str_replace('Grow Your Business Faster', 'Grow Your Business With AI', PromptLandingPageEditorDocument::html()),
            PromptLandingPageEditorDocument::css().' h1 { color: #c00; }',
            PromptLandingPageEditorDocument::js(),
        );

        $page->refresh();

        $this->assertStringContainsString('Grow Your Business With AI', $page->html);
        $this->assertStringContainsString('h1 { color: #c00; }', $page->css);
        $this->assertSame(PromptLandingPageEditorDocument::js(), $page->js);
        $this->assertSame(2, $page->versions()->count());
        $this->assertStringContainsString('Grow Your Business Faster', $page->versions()->where('version', 1)->value('html'));
        $this->assertSame(2, $saved['version']);
        $this->assertDatabaseHas('prompt_landing_page_versions', [
            'prompt_landing_page_id' => $page->id,
            'version' => 1,
            'kind' => PromptLandingPageVersionKind::Published->value,
        ]);
        $this->assertDatabaseHas('prompt_landing_page_versions', [
            'prompt_landing_page_id' => $page->id,
            'version' => 2,
            'kind' => PromptLandingPageVersionKind::Published->value,
        ]);
    }

    public function test_save_keeps_head_scripts_and_unedited_css_and_javascript(): void
    {
        $user = User::factory()->create();
        $page = PromptLandingPage::factory()->approved()->for($user)->create([
            'html' => PromptLandingPageEditorDocument::fullHtml(),
            'css' => PromptLandingPageEditorDocument::css(),
            'js' => PromptLandingPageEditorDocument::js(),
        ]);

        $this->savePage()->handle(
            $page,
            $user,
            str_replace('Grow Your Business Faster', 'Grow Your Business With AI', PromptLandingPageEditorDocument::html())
                .'<script>alert(1)</script>',
            '[data-gjs-type="wrapper"] { min-height: 100vh; } .hero h1 { color: red; }',
            '',
        );

        $page->refresh();

        $this->assertStringContainsString('https://cdn.example.test/lp-analytics.js', $page->html);
        $this->assertStringContainsString('window.LP_PAGE = { id: 39 };', $page->html);
        $this->assertStringContainsString('Grow Your Business With AI', $page->html);
        $this->assertStringNotContainsString('alert(1)', $page->html);
        $this->assertStringNotContainsString('browser-logger-active', $page->html);
        $this->assertSame(PromptLandingPageEditorDocument::css(), $page->css);
        $this->assertSame(PromptLandingPageEditorDocument::js(), $page->js);
    }

    private function savePage(): SavePage
    {
        return new SavePage(
            new SanitizeDocument(new PromptLandingPageHtmlSanitizer),
            new UniqueDuplicateIds,
            new MergeEditedDocument,
            new RecordPublishedVersion,
        );
    }
}
