<?php
// install.php - Automatic Database Installer for XAMPP

$host = '127.0.0.1';
$user = 'root';
$pass = ''; // Default XAMPP MySQL password is empty
$dbName = 'online_shop';

echo "<!DOCTYPE html>
<html lang='th'>
<head>
    <meta charset='UTF-8'>
    <title>Database Auto-Installer</title>
    <script src='https://cdn.tailwindcss.com'></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        violet: {
                            400: '#f59e0b', // Gold / Amber accent text highlight
                            500: '#ef4444', 
                            600: '#b91c1c'  // CMTC Crimson Red
                        }
                    }
                }
            }
        }
    </script>
</head>
<body class='bg-slate-950 text-slate-100 flex items-center justify-center min-h-screen font-sans'>";

echo "<div class='max-w-md w-full bg-slate-900 border border-slate-800 rounded-3xl p-8 shadow-2xl'>";
echo "<h2 class='text-2xl font-black mb-4 text-center text-violet-400'>CMTC SHOP Database Installer</h2>";

try {
    // 1. Connect to MySQL Server
    $pdo = new PDO("mysql:host=$host;charset=utf8mb4", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    echo "<p class='text-xs text-emerald-400 mb-2'>✓ เชื่อมต่อฐานข้อมูล XAMPP MySQL สำเร็จ...</p>";

    // 2. Read schema.sql content
    $sqlPath = __DIR__ . '/schema.sql';
    if (!file_exists($sqlPath)) {
        throw new Exception("ไม่พบไฟล์ schema.sql กรุณาตรวจสอบตำแหน่งไฟล์");
    }
    
    $sqlContent = file_get_contents($sqlPath);
    
    // 3. Execute SQL Query
    // We split queries by DELIMITER or execute directly since PDO supports multi-queries in a single exec() if emulate prepares is on
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    
    // MariaDB doesn't like DELIMITER command in PDO, so we remove the delimiter lines and replace the trigger syntax
    $sqlContent = preg_replace('/DELIMITER \/\/|DELIMITER ;/i', '', $sqlContent);
    $sqlContent = str_replace('//', ';', $sqlContent);
    
    $pdo->exec($sqlContent);
    
    echo "<p class='text-xs text-emerald-400 mb-2'>✓ สร้างฐานข้อมูล '$dbName' เรียบร้อย...</p>";
    echo "<p class='text-xs text-emerald-400 mb-2'>✓ นำเข้าโครงสร้างตาราง (Tables) สำเร็จ...</p>";
    echo "<p class='text-xs text-emerald-400 mb-2'>✓ สร้างระบบ View และ Trigger สำเร็จ...</p>";
    echo "<p class='text-xs text-emerald-400 mb-6'>✓ นำเข้า Mockup Data เรียบร้อย...</p>";
    
    echo "<div class='bg-emerald-950/40 border border-emerald-900 text-emerald-300 p-4 rounded-xl text-sm mb-6 text-center'>";
    echo "ติดตั้งระบบฐานข้อมูลเสร็จสมบูรณ์! 🎉";
    echo "</div>";
    
    echo "<a href='index.php' class='block w-full text-center py-3 bg-violet-600 hover:bg-violet-500 text-white font-bold rounded-xl shadow-lg transition-all'>เข้าสู่ร้านค้า &rarr;</a>";

} catch (Exception $e) {
    echo "<div class='bg-red-950/60 border border-red-800 text-red-300 p-4 rounded-xl text-xs mb-6 leading-relaxed'>";
    echo "<strong>เกิดข้อผิดพลาด:</strong><br>" . htmlspecialchars($e->getMessage());
    echo "</div>";
    echo "<p class='text-xs text-slate-500 text-center'>กรุณาเปิดบริการ MySQL ในหน้าต่างควบคุม XAMPP Control Panel</p>";
}

echo "</div></body></html>";
