<?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\View\Processing\AstProcessingPipeline;
use App\Framework\View\RenderContext;
use App\Framework\Meta\MetaData;
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 Processing Pipeline 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 transformer instance
$parser = new HtmlParser();
$xComponentTransformer = new XComponentTransformer(
    $liveComponentRegistry,
    $htmlComponentRegistry,
    $metadataCache,
    $parser
);

// Create AST Pipeline with transformer instance
$pipeline = new AstProcessingPipeline(
    transformers: [$xComponentTransformer],
    parser: $parser
);

// Test HTML
$html = '<html><body><h1>Test</h1><x-counter id="demo" initialValue="5" /><p>Done</p></body></html>';

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

// Create RenderContext
$context = new RenderContext(
    template: 'test-pipeline',
    metaData: new MetaData('Pipeline Test'),
    data: []
);

// Process through pipeline
$document = $pipeline->process($context, $html);
echo "Pipeline processing completed ✓\n\n";

// Render to HTML
$renderer = new HtmlRenderer();
$output = $renderer->render($document);

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

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

if (str_contains($output, '<h1>Test</h1>')) {
    echo "✓ SUCCESS: Content preserved!\n";
} else {
    echo "✗ FAIL: Content lost\n";
}
