<?php

declare(strict_types=1);

namespace Tests\Framework\View\Processors;

use App\Framework\View\DomWrapper;
use App\Framework\View\Processors\LayoutTagProcessor;
use PHPUnit\Framework\TestCase;

/**
 * Simplified test to check if LayoutTagProcessor preserves forms
 */
final class LayoutTagProcessorSimpleTest extends TestCase
{
    public function test_layout_processing_preserves_form_content(): void
    {
        // Test what happens when we simulate the LayoutTagProcessor behavior

        // 1. Original content with layout tag and form
        $originalContent = '<layout src="main"/>
        <section>
            <h1>Kontakt</h1>
            <form action="/kontakt" method="post">
                <input type="text" name="name" />
                <input type="email" name="email" />
                <input type="submit" value="Submit" />
            </form>
        </section>';

        // 2. Layout template
        $layoutTemplate = '<!DOCTYPE html>
        <html>
        <head><title>Layout</title></head>
        <body>
            <header>Header</header>
            <main>This will be replaced</main>
            <footer>Footer</footer>
        </body>
        </html>';

        // 3. Simulate what LayoutTagProcessor does
        $contentDom = DomWrapper::fromString($originalContent);
        $layoutTag = $contentDom->document->querySelector('layout[src]');

        // This is the content that should go into the layout
        $contentToInsert = $layoutTag->innerHTML;

        echo "Content to insert:\n" . $contentToInsert . "\n\n";

        // Check that the content contains the form
        $this->assertStringContainsString('<form action="/kontakt"', $contentToInsert);
        $this->assertStringContainsString('name="name"', $contentToInsert);
        $this->assertStringContainsString('name="email"', $contentToInsert);

        // 4. Now simulate inserting into layout
        $layoutDom = DomWrapper::fromString($layoutTemplate);
        $mainSlot = $layoutDom->document->querySelector('main');

        $this->assertNotNull($mainSlot, 'Layout should have main slot');

        // This is what LayoutTagProcessor does
        $mainSlot->innerHTML = $contentToInsert;

        $finalHtml = $layoutDom->document->saveHTML();

        echo "Final HTML:\n" . $finalHtml . "\n\n";

        // Check that form is preserved in final result
        $this->assertStringContainsString('<form action="/kontakt"', $finalHtml);
        $this->assertStringContainsString('name="name"', $finalHtml);
        $this->assertStringContainsString('name="email"', $finalHtml);

        // Check that layout structure is there
        $this->assertStringContainsString('<header>Header</header>', $finalHtml);
        $this->assertStringContainsString('<footer>Footer</footer>', $finalHtml);

        // The form should be inside main
        $this->assertStringContainsString('<main>', $finalHtml);

        // Now test if querySelectorAll still works on final result
        $finalDom = DomWrapper::fromString($finalHtml);
        $forms = $finalDom->document->querySelectorAll('form');

        $this->assertEquals(1, count($forms), 'Should find 1 form in final HTML');
        $this->assertEquals('/kontakt', $forms[0]->getAttribute('action'));
    }
}
