<?php

declare(strict_types=1);

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

use App\Framework\DI\DefaultContainer;
use App\Framework\View\DomWrapper;
use App\Framework\View\Processors\ForProcessor;
use App\Framework\View\RenderContext;
use App\Framework\Meta\MetaData;

// Initialize container
$container = new DefaultContainer();

// Create ForProcessor
$forProcessor = $container->get(ForProcessor::class);

// Test HTML with foreach attribute
$html = <<<'HTML'
<!DOCTYPE html>
<html>
<body>
    <table>
        <thead>
            <tr>
                <th>Model</th>
                <th>Version</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <tr foreach="$models as $model">
                <td>{{ $model['model_name'] }}</td>
                <td>{{ $model['version'] }}</td>
                <td>{{ $model['status'] }}</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
HTML;

// Test data
$data = [
    'models' => [
        ['model_name' => 'fraud-detector', 'version' => '1.0.0', 'status' => 'healthy'],
        ['model_name' => 'spam-classifier', 'version' => '2.0.0', 'status' => 'degraded'],
    ]
];

// Create render context
$context = new RenderContext(
    template: 'test',
    metaData: new MetaData('test', 'test'),
    data: $data,
    controllerClass: null
);

// Process
echo "=== ORIGINAL HTML ===\n";
echo $html . "\n\n";

$dom = DomWrapper::fromString($html);

echo "=== CHECKING FOR FOREACH NODES ===\n";
$foreachNodes = $dom->document->querySelectorAll('[foreach]');
echo "Found " . count($foreachNodes) . " foreach nodes\n\n";

foreach ($foreachNodes as $idx => $node) {
    echo "Node $idx:\n";
    echo "  Tag: " . $node->tagName . "\n";
    echo "  Foreach: " . $node->getAttribute('foreach') . "\n";
    echo "  HTML: " . substr($dom->document->saveHTML($node), 0, 200) . "\n\n";
}

echo "=== PROCESSING WITH ForProcessor ===\n";
$processedDom = $forProcessor->process($dom, $context);

echo "=== PROCESSED HTML ===\n";
echo $processedDom->toHtml(true) . "\n";
