<?php
/**
 * NUCLEAR FIX — Move everything to root so /public/ is never needed again
 * Run once, then delete.
 */
set_time_limit(300);
error_reporting(E_ALL);
ini_set('display_errors', 1);

$root = realpath(__DIR__ . '/..');
$public = __DIR__;

echo "<h2>☢️ NUCLEAR FIX — Moving to Root</h2>";
echo "<pre style='background:#1e1e1e;color:#d4d4d4;padding:20px;font-size:12px;'>";
echo "Root:   $root\n";
echo "Public: $public\n\n";

// STEP 1: Recursively copy all files from public/ to root
echo "━━━ STEP 1: Copy public/* → root ━━━\n";

function copyAll($src, $dst) {
    $dir = opendir($src);
    while (($file = readdir($dir)) !== false) {
        if ($file === '.' || $file === '..') continue;
        $srcPath = $src . '/' . $file;
        $dstPath = $dst . '/' . $file;

        if (is_dir($srcPath)) {
            if (!is_dir($dstPath)) mkdir($dstPath, 0755, true);
            copyAll($srcPath, $dstPath);
        } else {
            copy($srcPath, $dstPath);
            echo "  📄 $file → root\n";
        }
    }
    closedir($dir);
}

copyAll($public, $root);
echo "✅ Public files copied to root\n\n";

// STEP 2: Fix root index.php — remove ../ from paths
echo "━━━ STEP 2: Fix root index.php paths ━━━\n";
$indexPath = $root . '/index.php';
if (file_exists($indexPath)) {
    $content = file_get_contents($indexPath);
    $content = str_replace("__DIR__.'/../vendor/", "__DIR__.'/vendor/", $content);
    $content = str_replace("__DIR__.'/../bootstrap/", "__DIR__.'/bootstrap/", $content);
    $content = str_replace("__DIR__.'/../storage/", "__DIR__.'/storage/", $content);
    file_put_contents($indexPath, $content);
    echo "✅ index.php paths fixed\n\n";
}

// STEP 3: Fix admin-utils.php at root (if it was copied)
echo "━━━ STEP 3: Fix admin-utils.php paths ━━━\n";
$adminPath = $root . '/admin-utils.php';
if (file_exists($adminPath)) {
    $content = file_get_contents($adminPath);
    $content = str_replace("__DIR__.'/../vendor/", "__DIR__.'/vendor/", $content);
    $content = str_replace("__DIR__.'/../bootstrap/", "__DIR__.'/bootstrap/", $content);
    $content = str_replace("__DIR__.'/../.env", "__DIR__.'/.env", $content);
    file_put_contents($adminPath, $content);
    echo "✅ admin-utils.php paths fixed\n\n";
}

// STEP 4: Ensure root .htaccess is standard Laravel
echo "━━━ STEP 4: Fix root .htaccess ━━━\n";
$htaccess = $root . '/.htaccess';
$htaccessContent = <<<'HTACCESS'
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
    RewriteEngine On
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
HTACCESS;
file_put_contents($htaccess, $htaccessContent);
echo "✅ .htaccess written\n\n";

// STEP 5: Rename public/ → public_old/
echo "━━━ STEP 5: Rename public/ → public_old/ ━━━\n";
if (is_dir($root . '/public_old')) {
    echo "   public_old/ already exists, skipping rename\n";
} else {
    if (rename($public, $root . '/public_old')) {
        echo "✅ public/ renamed to public_old/\n";
    } else {
        echo "❌ Could not rename public/. You may need to do this manually.\n";
    }
}

echo "\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "🎉 DONE!\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Your site: https://sarkinmota-autos.com/\n";
echo "Admin:     https://sarkinmota-autos.com/admin-utils.php\n";
echo "\nIf you see errors, your files are safe in public_old/\n";
echo "⚠️ DELETE this script NOW!\n";
echo "</pre>";
