<?php
/**
 * ====================================================
 * FILE INSTALASI LARAVEL - UNTUK HOSTING RUMAHWEB
 * ====================================================
 * INSTRUKSI:
 * 1. Upload file ini ke folder ROOT project (bukan /public)
 * 2. Akses via browser: https://namadomain.com/install.php
 * 3. HAPUS file ini segera setelah migrasi berhasil!
 * ====================================================
 */

// Keamanan: ganti dengan kunci rahasia Anda sendiri
define('INSTALL_KEY', 'rahasia123');

if (!isset($_GET['key']) || $_GET['key'] !== INSTALL_KEY) {
    die('<h2 style="color:red;">❌ Akses Ditolak. Tambahkan ?key=rahasia123 di URL.</h2>');
}

$action = $_GET['action'] ?? 'menu';

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

ob_start();
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LiteChat - Installer</title>
<style>
    body { font-family: monospace; background: #0f172a; color: #e2e8f0; padding: 2rem; }
    h1 { color: #14b8a6; }
    .card { background: #1e293b; border-radius: 12px; padding: 1.5rem; margin: 1rem 0; }
    .btn { display: inline-block; padding: 0.6rem 1.2rem; border-radius: 8px; text-decoration: none; margin: 0.3rem; font-weight: bold; }
    .btn-green { background: #14b8a6; color: white; }
    .btn-red { background: #ef4444; color: white; }
    .btn-blue { background: #3b82f6; color: white; }
    .output { background: #0f172a; border: 1px solid #334155; padding: 1rem; border-radius: 8px; white-space: pre-wrap; font-size: 0.85rem; color: #94a3b8; max-height: 400px; overflow-y: auto; }
    .ok { color: #4ade80; }
    .err { color: #f87171; }
    .warn { color: #facc15; }
</style>
</head>
<body>
<h1>🚀 LiteChat Installer</h1>
<div class="card">
    <p class="warn">⚠️ PENTING: Hapus file ini setelah instalasi selesai!</p>
</div>

<?php if ($action === 'menu'): ?>
<div class="card">
    <h2>📋 Pilih Aksi:</h2>
    <a href="?key=<?= INSTALL_KEY ?>&action=info" class="btn btn-blue">📊 Info Sistem</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=migrate" class="btn btn-green">🗄️ Jalankan Migrate</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=seed" class="btn btn-green">🌱 Jalankan Seeder</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=fresh" class="btn btn-red">🔄 Migrate Fresh + Seed</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=optimize" class="btn btn-blue">⚡ Optimize (Cache Config/Route)</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=storage_link" class="btn btn-blue">🔗 Storage Link</a>
    <a href="?key=<?= INSTALL_KEY ?>&action=clear" class="btn btn-red">🗑️ Clear All Cache</a>
</div>

<?php elseif ($action === 'info'): ?>
<div class="card">
    <h2>📊 Info Sistem</h2>
    <div class="output">
PHP Version    : <?= phpversion() ?> <?= version_compare(phpversion(), '8.2', '>=') ? '<span class="ok">✓ OK</span>' : '<span class="err">✗ Butuh PHP 8.2+</span>' ?>

DB Connection  : <?= config('database.default') ?>

DB Database    : <?= config('database.connections.'.config('database.default').'.database') ?>

APP_ENV        : <?= config('app.env') ?>

APP_URL        : <?= config('app.url') ?>

APP_TIMEZONE   : <?= config('app.timezone') ?>

    </div>
    <br>
    <a href="?key=<?= INSTALL_KEY ?>" class="btn btn-blue">← Kembali</a>
</div>

<?php else:
    $output = "";
    $artisanMap = [
        'migrate'      => 'migrate --force',
        'seed'         => 'db:seed --force',
        'fresh'        => 'migrate:fresh --seed --force',
        'optimize'     => 'optimize',
        'storage_link' => 'storage:link',
        'clear'        => 'optimize:clear',
    ];

    if (isset($artisanMap[$action])) {
        $command = $artisanMap[$action];
        $exitCode = Artisan::call($command);
        $output = Artisan::output();
    } else {
        $output = "Perintah tidak dikenal.";
    }
?>
<div class="card">
    <h2>🔧 Hasil: <code><?= htmlspecialchars($action) ?></code></h2>
    <div class="output"><?= htmlspecialchars($output) ?></div>
    <br>
    <a href="?key=<?= INSTALL_KEY ?>" class="btn btn-blue">← Kembali ke Menu</a>
</div>
<?php endif; ?>

<div class="card">
    <p class="err">🔴 INGAT: Hapus file <strong>install.php</strong> ini setelah semua beres!</p>
</div>
</body>
</html>
