<?php
// RC-009 Verification Script
// Simulates Theme Engine bootstrap (no HTTP, no DB).
define('ROOT_PATH', 'c:/xampp/htdocs/inveetaire');
define('DIRECTORY_SEPARATOR', '/');

require_once ROOT_PATH . '/app/modules/Theme/ThemeManifest.php';
require_once ROOT_PATH . '/app/modules/Theme/ThemeRegistry.php';
require_once ROOT_PATH . '/app/modules/Theme/ThemeLoader.php';
require_once ROOT_PATH . '/app/modules/Theme/ThemeRenderer.php';
require_once ROOT_PATH . '/app/modules/Theme/ThemeService.php';

use App\Modules\Theme\ThemeRegistry;
use App\Modules\Theme\ThemeLoader;
use App\Modules\Theme\ThemeManifest;

echo "=== RC-009 Theme Engine Verification ===\n\n";

// 1. Registry discovers theme-001
$registry = new ThemeRegistry();
$available = $registry->getAvailableThemes();
echo "1. Registry scan:\n";
if (isset($available['theme-001'])) {
    echo "   [PASS] theme-001 discovered\n";
    echo "   Manifest: " . json_encode($available['theme-001']) . "\n";
} else {
    echo "   [FAIL] theme-001 not discovered\n";
}

// 2. Loader loads theme.json
$loader = new ThemeLoader();
$loaded = $loader->load('theme-001');
echo "\n2. ThemeLoader:\n";
if ($loaded['manifest']['code'] === 'theme-001') {
    echo "   [PASS] theme.json loaded correctly\n";
    echo "   Code: " . $loaded['manifest']['code'] . "\n";
    echo "   Name: " . $loaded['manifest']['name'] . "\n";
    echo "   Entry: " . $loaded['entry'] . "\n";
} else {
    echo "   [FAIL] loader failed\n";
}

// 3. Renderer resolves index.php
echo "\n3. ThemeRenderer entry file:\n";
if (file_exists($loaded['entry'])) {
    echo "   [PASS] index.php resolved and exists: " . $loaded['entry'] . "\n";
} else {
    echo "   [FAIL] index.php not found\n";
}

// 4. Fallback: invalid theme falls back to theme-001
echo "\n4. Fallback test:\n";
$fallback = $loader->load('theme-nonexistent-xyz');
if ($fallback['manifest']['code'] === 'theme-001') {
    echo "   [PASS] Invalid theme 'theme-nonexistent-xyz' fell back to theme-001\n";
} else {
    echo "   [FAIL] Fallback did not work correctly\n";
}

// 5. themeExists check
echo "\n5. Registry existence check:\n";
echo "   theme-001 exists: " . ($registry->themeExists('theme-001') ? '[PASS] true' : '[FAIL] false') . "\n";
echo "   theme-999 exists: " . (!$registry->themeExists('theme-999') ? '[PASS] false (correct)' : '[FAIL] true (wrong)') . "\n";

echo "\n=== All verification checks complete. ===\n";