<?php
/**
 * Database Installer Script
 * CMTC Shopping
 */
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/functions/helpers.php';

$message = "";
$status = "";

if (isset($_POST['install'])) {
    try {
        $database = new Database();
        $db = $database->getConnection();
        
        // Read and execute schema.sql
        $sql = file_get_contents(__DIR__ . '/database/schema.sql');
        $db->exec($sql);
        
        // Dynamically update admin password to ensure correct bcrypt hash
        $admin_password_hash = password_hash('admin123', PASSWORD_BCRYPT);
        $stmt_admin = $db->prepare("UPDATE users SET password = ? WHERE id = 1");
        $stmt_admin->execute([$admin_password_hash]);
        
        $status = "success";
        $message = "ติดตั้ง/รีเซ็ตโครงสร้างฐานข้อมูลเรียบร้อยแล้ว! สามารถเข้าใช้งานได้ทันทีด้วยบัญชีผู้ใช้: admin / รหัสผ่าน: admin123";
    } catch (Exception $e) {
        $status = "error";
        $message = "เกิดข้อผิดพลาดในการติดตั้ง: " . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="th">
<head>
    <meta charset="UTF-8">
    <title>Database Setup - CMTC Shopping</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #1e1e2f 0%, #11111d 100%);
            color: #ffffff;
            font-family: 'Sarabun', sans-serif;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .install-card {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 16px;
            padding: 40px;
            width: 100%;
            max-width: 500px;
            box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
        }
    </style>
</head>
<body>
    <div class="install-card">
        <h3 class="text-center mb-4"><i class="bi bi-database-fill-gear text-primary"></i> ติดตั้งระบบฐานข้อมูล</h3>
        
        <?php if (!empty($message)): ?>
            <div class="alert alert-<?php echo $status === 'success' ? 'success' : 'danger'; ?> mb-4">
                <?php echo $message; ?>
            </div>
            <?php if ($status === 'success'): ?>
                <div class="text-center">
                    <a href="<?php echo base_url('login.php'); ?>" class="btn btn-primary w-100 py-2">เข้าสู่ระบบ</a>
                </div>
            <?php endif; ?>
        <?php else: ?>
            <p class="text-muted text-center mb-4">คลิกปุ่มด้านล่างเพื่อทำการสร้างฐานข้อมูล `shop037` และนำเข้าตารางและข้อมูลเริ่มต้นทั้งหมด</p>
            <form method="POST">
                <button type="submit" name="install" class="btn btn-primary w-100 py-2">
                    <i class="bi bi-play-fill me-1"></i> เริ่มการติดตั้งฐานข้อมูล
                </button>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
