<?php

require_once __DIR__ . '/../../vendor/autoload.php';

use App\Framework\View\Dom\Parser\HtmlParser;
use App\Framework\View\Dom\Renderer\HtmlRenderer;
use App\Framework\View\Dom\Transformer\XComponentTransformer;
use App\Framework\LiveComponents\Contracts\ComponentRegistryInterface;
use App\Framework\LiveComponents\Contracts\LiveComponentContract;
use App\Framework\LiveComponents\Performance\ComponentMetadataCacheInterface;
use App\Framework\LiveComponents\Performance\CompiledComponentMetadata;
use App\Framework\LiveComponents\Performance\ComponentPropertyMetadata;
use App\Framework\LiveComponents\ValueObjects\ComponentData;
use App\Framework\LiveComponents\ValueObjects\ComponentId;
use App\Framework\LiveComponents\ValueObjects\ComponentRenderData;
use App\Framework\View\Contracts\HtmlComponentRegistryInterface;

echo "=== AST-based XComponentTransformer Test ===\n\n";

// Mock LiveComponent
$mockComponent = Mockery::mock(LiveComponentContract::class);
$mockComponent->shouldReceive('getId')
    ->andReturn(ComponentId::create('counter', 'demo'));
$mockComponent->shouldReceive('getData')
    ->andReturn(ComponentData::fromArray(['initialValue' => 5]));
$mockComponent->shouldReceive('getRenderData')
    ->andReturn(new ComponentRenderData('counter-template', ['value' => 5]));

// Mock LiveComponent Registry
$liveComponentRegistry = Mockery::mock(ComponentRegistryInterface::class);
$liveComponentRegistry->shouldReceive('isRegistered')
    ->with('counter')
    ->andReturn(true);

$liveComponentRegistry->shouldReceive('getClassName')
    ->with('counter')
    ->andReturn('TestCounterComponent');

$liveComponentRegistry->shouldReceive('resolve')
    ->andReturn($mockComponent);

$liveComponentRegistry->shouldReceive('renderWithWrapper')
    ->with($mockComponent)
    ->andReturn('<div data-component-id="counter:demo">Counter HTML</div>');

$liveComponentRegistry->shouldReceive('getAllComponentNames')
    ->andReturn(['counter']);

// Mock HTML Component Registry
$htmlComponentRegistry = Mockery::mock(HtmlComponentRegistryInterface::class);
$htmlComponentRegistry->shouldReceive('has')
    ->andReturn(false);

$htmlComponentRegistry->shouldReceive('getAllComponentNames')
    ->andReturn([]);

// Mock Metadata Cache
$mockMetadata = new CompiledComponentMetadata(
    className: 'TestCounterComponent',
    componentName: 'counter',
    properties: [
        'initialValue' => new ComponentPropertyMetadata(
            name: 'initialValue',
            type: 'int',
            isPublic: true,
            isReadonly: false
        )
    ],
    actions: [],
    constructorParams: []
);

$metadataCache = Mockery::mock(ComponentMetadataCacheInterface::class);
$metadataCache->shouldReceive('get')
    ->with('TestCounterComponent')
    ->andReturn($mockMetadata);

// Create parser and transformer
$parser = new HtmlParser();
$transformer = new XComponentTransformer(
    $liveComponentRegistry,
    $htmlComponentRegistry,
    $metadataCache,
    $parser
);
$renderer = new HtmlRenderer();

// Test HTML with x-component
$html = '<html><body><p>Before</p><x-counter id="demo" initialValue="5" /><p>After</p></body></html>';

echo "Input HTML:\n$html\n\n";

// Parse HTML to AST
$document = $parser->parse($html);
echo "Parsed to AST ✓\n\n";

// Transform x-components
$transformedDocument = $transformer->transform($document);
echo "Transformed x-components ✓\n\n";

// Render back to HTML
$outputHtml = $renderer->render($transformedDocument);

echo "Output HTML:\n$outputHtml\n\n";

// Verify result
if (str_contains($outputHtml, 'data-component-id="counter:demo"')) {
    echo "✓ SUCCESS: Component rendered correctly!\n";
} else {
    echo "✗ FAIL: Component not found in output\n";
}

if (str_contains($outputHtml, '<p>Before</p>')) {
    echo "✓ SUCCESS: Content before component preserved!\n";
} else {
    echo "✗ FAIL: Content before component missing\n";
}

if (str_contains($outputHtml, '<p>After</p>')) {
    echo "✓ SUCCESS: Content after component preserved!\n";
} else {
    echo "✗ FAIL: Content after component missing\n";
}
