<?php
/**
 * Emergency Cache Clear Script
 * Upload ke public/ folder, akses via browser, lalu HAPUS file ini!
 * URL: https://absen.ptack.co.id/clear-cache.php
 */

// Security check - hapus atau ubah token ini setelah digunakan
$token = 'clear-cache-2026';

if (!isset($_GET['token']) || $_GET['token'] !== $token) {
    die('Unauthorized. Use ?token=' . $token);
}

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

$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$kernel->bootstrap();

echo "<pre>";
echo "=== CLEARING CACHE ===\n\n";

// Clear all caches
$commands = [
    'config:clear' => 'Config cache cleared',
    'route:clear' => 'Route cache cleared',
    'view:clear' => 'View cache cleared',
    'cache:clear' => 'Application cache cleared',
    'event:clear' => 'Event cache cleared',
];

foreach ($commands as $command => $message) {
    try {
        Artisan::call($command);
        echo "✅ $message\n";
    } catch (Exception $e) {
        echo "❌ $command failed: " . $e->getMessage() . "\n";
    }
}

// Clear Filament cache
try {
    if (class_exists(\Filament\Facades\Filament::class)) {
        $filamentCachePath = storage_path('framework/cache/filament');
        if (is_dir($filamentCachePath)) {
            array_map('unlink', glob("$filamentCachePath/*.*"));
            echo "✅ Filament cache cleared\n";
        }
    }
} catch (Exception $e) {
    echo "⚠️ Filament cache: " . $e->getMessage() . "\n";
}

// Clear opcache if available
if (function_exists('opcache_reset')) {
    opcache_reset();
    echo "✅ OPcache cleared\n";
}

echo "\n=== DONE ===\n";
echo "\n⚠️ PENTING: Hapus file clear-cache.php ini setelah selesai!\n";
echo "</pre>";
