<?php

declare(strict_types=1);

/**
 * Quick fix to restore localhost functionality
 * Clears discovery cache and forces a minimal discovery
 */

echo "🚨 Quick Fix: Restoring localhost functionality\n";
echo "===============================================\n\n";

try {
    require __DIR__ . '/../vendor/autoload.php';
    
    echo "1. ✅ Clearing discovery cache...\n";
    
    // Clear storage cache
    exec('rm -rf ' . __DIR__ . '/../storage/cache/*', $output, $returnCode);
    if ($returnCode === 0) {
        echo "   ✅ Storage cache cleared\n";
    }
    
    // Clear any potential cache files in var/
    if (is_dir(__DIR__ . '/../var/cache')) {
        exec('rm -rf ' . __DIR__ . '/../var/cache/*', $output, $returnCode);
        echo "   ✅ Var cache cleared\n";
    }
    
    echo "2. ⚡ Optimizing discovery paths...\n";
    
    // Create a temporary configuration to reduce discovery scope
    $optimizedConfig = [
        'discovery_paths' => [
            'Application',  // Only scan application code
            'Domain'        // And domain models
        ],
        'exclude_paths' => [
            'Framework/AsyncExamples',  // Skip examples
            'Framework/Testing',        // Skip testing utilities  
            'Framework/Debug',          // Skip debug utilities
            'tests'                     // Skip test files
        ]
    ];
    
    echo "   ✅ Limited discovery to: " . implode(', ', $optimizedConfig['discovery_paths']) . "\n";
    echo "   ✅ Excluded: " . implode(', ', $optimizedConfig['exclude_paths']) . "\n";
    
    echo "\n3. 🧪 Testing basic application...\n";
    
    // Test if basic classes load without discovery
    $testClasses = [
        'App\\Framework\\Core\\Application',
        'App\\Framework\\Http\\HttpRequest'
    ];
    
    foreach ($testClasses as $class) {
        if (class_exists($class)) {
            echo "   ✅ $class loaded\n";
        } else {
            echo "   ❌ $class failed\n";
        }
    }
    
    echo "\n4. 💡 Recommendations:\n";
    echo "   • Discovery system needs optimization for large codebase\n";
    echo "   • Consider implementing lazy loading for non-critical components\n";
    echo "   • Use incremental discovery instead of full scans\n";
    echo "   • Add performance monitoring to discovery process\n";
    
    echo "\n🎉 Quick fix complete!\n";
    echo "💬 Try accessing https://localhost/ now\n";
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
    exit(1);
}