<?php
/**
 * Sawdabazar cPanel Setup Helper Script
 * 
 * Visited this file in browser to run migrations, key generation, seeders, and storage link.
 * URL: https://api.yourdomain.com/run-commands.php
 * 
 * SECURITY WARNING: Please DELETE this file from your public/ folder after running it successfully!
 */

define('LARAVEL_START', microtime(true));

// Load Composer Autoloader
if (file_exists($a = __DIR__.'/../vendor/autoload.php')) {
    require $a;
} elseif (file_exists($b = __DIR__.'/../sawdabazar/vendor/autoload.php')) {
    // If files are one level deep
    require $b;
} else {
    die("Error: vendor/autoload.php not found. Did you include the 'vendor' folder in your ZIP upload? If Terminal is disabled on cPanel, you must upload the vendor folder from your local machine.");
}

// Bootstrap Laravel Application
if (file_exists($c = __DIR__.'/../bootstrap/app.php')) {
    $app = require $c;
} elseif (file_exists($d = __DIR__.'/../sawdabazar/bootstrap/app.php')) {
    $app = require $d;
} else {
    die("Error: bootstrap/app.php not found.");
}

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

echo "<!DOCTYPE html>
<html>
<head>
    <title>Sawdabazar cPanel Setup Helper</title>
    <style>
        body { font-family: 'Courier New', monospace; background: #0f172a; color: #38bdf8; padding: 20px; line-height: 1.5; }
        .container { max-width: 900px; margin: 0 auto; background: #1e293b; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); }
        h1 { color: #f43f5e; border-bottom: 2px solid #f43f5e; padding-bottom: 10px; margin-top: 0; }
        .cmd { color: #e2e8f0; font-weight: bold; margin-top: 15px; }
        .output { background: #020617; padding: 12px; border-left: 4px solid #10b981; margin-bottom: 15px; overflow-x: auto; color: #10b981; white-space: pre-wrap; }
        .alert { background: #7f1d1d; color: #fecaca; padding: 15px; border-radius: 6px; font-weight: bold; border-left: 5px solid #ef4444; }
        .success { color: #34d399; font-size: 1.2em; font-weight: bold; margin-top: 20px; text-align: center; border: 2px solid #34d399; padding: 15px; border-radius: 6px; }
    </style>
</head>
<body>
<div class='container'>
    <h1>Sawdabazar cPanel Setup Helper</h1>
    <div class='alert'>
        ⚠️ SECURITY WARNING: Please DELETE this file (public/run-commands.php) from your server immediately after setup is complete!
    </div>";

function runCommand($kernel, $command, $parameters = []) {
    echo "<div class='cmd'>$ php artisan $command " . http_build_query($parameters, '', ' ') . "</div>";
    try {
        $status = $kernel->call($command, $parameters);
        $output = $kernel->output();
        echo "<div class='output'>" . htmlspecialchars($output) . "\nExit Code: $status</div>";
    } catch (\Exception $e) {
        echo "<div class='output' style='border-left-color: #ef4444; color: #f87171;'>ERROR: " . htmlspecialchars($e->getMessage()) . "</div>";
    }
}

// 1. Generate Application Key (if not already set)
if (empty(env('APP_KEY'))) {
    runCommand($kernel, 'key:generate');
} else {
    echo "<div class='cmd'>$ php artisan key:generate</div>";
    echo "<div class='output'>Application key is already set. Skipping.</div>";
}

// 2. Run Database Migrations
runCommand($kernel, 'migrate', ['--force' => true]);

// 3. Run Seeders
runCommand($kernel, 'db:seed', ['--force' => true]);

// 4. Create Storage Symlink
runCommand($kernel, 'storage:link');

// 5. Clear and Cache Configuration/Routes
runCommand($kernel, 'optimize:clear');
runCommand($kernel, 'optimize');
runCommand($kernel, 'filament:optimize');

echo "<div class='success'>
    🎉 Setup Completed Successfully!<br>
    <span style='font-size: 0.8em; font-weight: normal; color: #94a3b8;'>Don't forget to delete public/run-commands.php file from cPanel.</span>
</div>
</div>
</body>
</html>";
