<?php
// One-time cache clear — DELETE THIS FILE after running
$token = $_GET['token'] ?? '';
if ($token !== 'tmit_clear_2024') {
    die('Unauthorized');
}

$root = dirname(__DIR__);
$cleared = [];
$errors  = [];

// Clear compiled views
$viewsDir = $root . '/storage/framework/views';
if (is_dir($viewsDir)) {
    foreach (glob($viewsDir . '/*.php') as $file) {
        if (unlink($file)) {
            $cleared[] = basename($file);
        } else {
            $errors[] = basename($file);
        }
    }
}

// Clear route cache
foreach ([
    $root . '/bootstrap/cache/routes-v7.php',
    $root . '/bootstrap/cache/routes.php',
    $root . '/bootstrap/cache/config.php',
    $root . '/bootstrap/cache/packages.php',
    $root . '/bootstrap/cache/services.php',
] as $file) {
    if (file_exists($file)) {
        if (unlink($file)) {
            $cleared[] = basename($file);
        }
    }
}

echo '<style>body{font-family:sans-serif;max-width:600px;margin:40px auto;padding:20px}</style>';
echo '<h2>✅ Cache Cleared</h2>';
echo '<p><strong>' . count($cleared) . ' files cleared:</strong></p>';
echo '<ul>';
foreach ($cleared as $f) echo "<li>$f</li>";
echo '</ul>';
if ($errors) {
    echo '<p style="color:red"><strong>Failed:</strong></p><ul>';
    foreach ($errors as $f) echo "<li>$f</li>";
    echo '</ul>';
}
echo '<p style="color:red;font-weight:bold">⚠️ DELETE this file now: public/clear-cache.php</p>';
