<?php

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

use App\Framework\View\TemplateProcessor;
use App\Framework\View\RenderContext;
use App\Framework\View\ProcessingMode;
use App\Framework\Meta\MetaData;
use App\Framework\View\Dom\Transformer\XComponentTransformer;
use App\Framework\View\Processors\PlaceholderReplacer;
use App\Framework\View\Processors\ForStringProcessor;
use App\Framework\DI\DefaultContainer;

echo "=== TemplateProcessor AST Integration Test (Simple) ===\n\n";

// Create minimal container
$container = new DefaultContainer();

// Create TemplateProcessor WITHOUT any processors (minimal test)
$templateProcessor = new TemplateProcessor(
    astTransformers: [],  // No transformers for this simple test
    stringProcessors: [],  // No string processors
    container: $container
);

// Test HTML - just plain HTML without any special processing
$html = <<<HTML
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <h1>Hello World</h1>
    <p>This is a simple test.</p>
</body>
</html>
HTML;

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

// Create minimal RenderContext
$context = new RenderContext(
    template: 'test-simple',
    metaData: new MetaData('Simple Test'),
    data: [],
    processingMode: ProcessingMode::MINIMAL  // Minimal processing
);

try {
    // Render (should just parse and return the HTML unchanged)
    $output = $templateProcessor->render($context, $html);

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

    // Verify basic structure is preserved
    $checks = [
        'title preserved' => str_contains($output, '<title>Test Page</title>'),
        'h1 preserved' => str_contains($output, '<h1>Hello World</h1>'),
        'paragraph preserved' => str_contains($output, '<p>This is a simple test.</p>'),
        'html structure' => str_contains($output, '<html>') && str_contains($output, '</html>'),
        'body structure' => str_contains($output, '<body>') && str_contains($output, '</body>')
    ];

    echo "Verification:\n";
    foreach ($checks as $check => $passed) {
        echo ($passed ? '✓' : '✗') . " $check\n";
    }

    $allPassed = array_reduce($checks, fn($carry, $item) => $carry && $item, true);

    echo "\n" . ($allPassed ? '✅ TEMPLATE PROCESSOR AST INTEGRATION WORKING!' : '❌ SOME CHECKS FAILED') . "\n";

    exit($allPassed ? 0 : 1);

} catch (\Throwable $e) {
    echo "❌ ERROR: {$e->getMessage()}\n";
    echo "Stack trace:\n{$e->getTraceAsString()}\n";
    exit(1);
}
