<?php

declare(strict_types=1);

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

use App\Framework\DI\DefaultContainer;
use App\Framework\View\ViewRenderer;
use App\Framework\View\RenderContext;
use App\Framework\Meta\MetaData;

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

// Get ViewRenderer (complete pipeline)
$viewRenderer = $container->get(ViewRenderer::class);

// Test data matching the ML Dashboard
$data = [
    'models' => [
        [
            'model_name' => 'fraud-detector',
            'version' => '1.0.0',
            'type' => 'supervised',
            'accuracy' => 0.94,
            'status' => 'healthy',
            'total_predictions' => 1234
        ],
        [
            'model_name' => 'spam-classifier',
            'version' => '2.0.0',
            'type' => 'supervised',
            'accuracy' => 0.78,
            'status' => 'degraded',
            'total_predictions' => 567
        ],
    ],
    'alerts' => [],
    'summary' => [
        'total_models' => 2,
        'healthy_models' => 1,
        'degraded_models' => 1
    ]
];

// Create render context
$context = new RenderContext(
    template: 'admin/ml-dashboard',
    metaData: new MetaData('ML Dashboard', ''),
    data: $data,
    controllerClass: null
);

echo "=== TESTING FULL TEMPLATE PIPELINE ===\n\n";
echo "Data being passed:\n";
print_r($data);
echo "\n";

try {
    echo "=== RENDERING TEMPLATE ===\n";
    $html = $viewRenderer->render($context);

    echo "=== CHECKING FOR FOREACH ATTRIBUTES IN OUTPUT ===\n";
    if (str_contains($html, 'foreach=')) {
        echo "❌ PROBLEM: foreach attribute found in output (not processed)\n";

        // Show the problematic section
        preg_match('/<tr[^>]*foreach[^>]*>.*?<\/tr>/s', $html, $matches);
        if (!empty($matches)) {
            echo "Found:\n" . $matches[0] . "\n\n";
        }
    } else {
        echo "✅ Good: No foreach attributes in output\n\n";
    }

    echo "=== CHECKING FOR PLACEHOLDERS IN OUTPUT ===\n";
    if (preg_match('/{{[^}]+}}/', $html, $matches)) {
        echo "❌ PROBLEM: Unreplaced placeholders found\n";
        echo "Example: " . $matches[0] . "\n\n";
    } else {
        echo "✅ Good: No unreplaced placeholders\n\n";
    }

    echo "=== CHECKING FOR MODEL DATA IN OUTPUT ===\n";
    if (str_contains($html, 'fraud-detector')) {
        echo "✅ Good: Model data found in output\n";
    } else {
        echo "❌ PROBLEM: Model data NOT found in output\n";
    }

    if (str_contains($html, '1.0.0')) {
        echo "✅ Good: Version data found in output\n";
    } else {
        echo "❌ PROBLEM: Version data NOT found in output\n";
    }

    // Show a snippet of the models table
    echo "\n=== MODELS TABLE SECTION ===\n";
    if (preg_match('/<tbody[^>]*>.*?<\/tbody>/s', $html, $matches)) {
        echo substr($matches[0], 0, 500) . "...\n";
    }

} catch (\Exception $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
    echo "Trace:\n" . $e->getTraceAsString() . "\n";
}
