<?php
/**
 * FORCE FIX — Edit .env directly + nuke all caches
 * DELETE AFTER USE
 */

// Step 1: Find and fix .env
$envPaths = [
    __DIR__ . '/.env',
    __DIR__ . '/../.env',
];
$envPath = null;
foreach ($envPaths as $p) { if (file_exists($p)) { $envPath = realpath($p); break; } }

echo "<h2>🔧 Force Fix — Sarkin Mota Group</h2>";
echo "<pre style='background:#1e1e1e;color:#d4d4d4;padding:20px;font-size:13px;'>";

if (!$envPath) {
    die("❌ .env not found at any expected path");
}

echo "📄 .env found: $envPath\n\n";

// Show current values
$env = file_get_contents($envPath);
preg_match('/^APP_ENV=(.*)$/m', $env, $m);  echo "Current APP_ENV: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_NAME=(.*)$/m', $env, $m); echo "Current APP_NAME: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_URL=(.*)$/m', $env, $m);  echo "Current APP_URL: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_DEBUG=(.*)$/m', $env, $m); echo "Current APP_DEBUG: " . ($m[1] ?? 'NOT SET') . "\n";

// Fix them
$env = preg_replace('/^APP_ENV=.*$/m', 'APP_ENV=production', $env);
$env = preg_replace('/^APP_NAME=.*$/m', 'APP_NAME="Sarkin Mota Group"', $env);
$env = preg_replace('/^APP_URL=.*$/m', 'APP_URL=https://sarkinmota-autos.com', $env);
$env = preg_replace('/^APP_DEBUG=.*$/m', 'APP_DEBUG=false', $env);

// Remove ASSET_URL if exists, or add it
if (preg_match('/^ASSET_URL=.*$/m', $env)) {
    $env = preg_replace('/^ASSET_URL=.*$/m', '', $env);
}

file_put_contents($envPath, $env);
echo "\n✅ .env updated\n";

// Step 2: Nuke ALL caches
$cacheDirs = [
    __DIR__ . '/../storage/framework/views',
    __DIR__ . '/../storage/framework/cache/data',
    __DIR__ . '/../bootstrap/cache',
    __DIR__ . '/storage/framework/views',
    __DIR__ . '/storage/framework/cache/data',
    __DIR__ . '/bootstrap/cache',
];

$total = 0;
foreach ($cacheDirs as $dir) {
    if (is_dir($dir)) {
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
            RecursiveIteratorIterator::CHILD_FIRST
        );
        foreach ($files as $file) {
            if ($file->isFile() && $file->getFilename() !== '.gitignore') {
                unlink($file->getRealPath());
                $total++;
            }
        }
    }
}
echo "✅ Cleared $total cached files\n";

// Step 3: Verify
$env = file_get_contents($envPath);
preg_match('/^APP_ENV=(.*)$/m', $env, $m);  echo "\nNew APP_ENV: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_NAME=(.*)$/m', $env, $m); echo "New APP_NAME: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_URL=(.*)$/m', $env, $m);  echo "New APP_URL: " . ($m[1] ?? 'NOT SET') . "\n";
preg_match('/^APP_DEBUG=(.*)$/m', $env, $m); echo "New APP_DEBUG: " . ($m[1] ?? 'NOT SET') . "\n";

echo "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "✅ DONE! Now visit:\n";
echo "   https://sarkinmota-autos.com/\n";
echo "   OR https://sarkinmota-autos.com/public/\n";
echo "\nIf still white, press Ctrl+F5 (hard refresh)\n";
echo "⚠️ DELETE this file (force-fix.php) NOW!\n";
echo "</pre>";
