<?php
/**
 * Admin Setup Script
 * Run this ONCE to add the admin role and create the admin account.
 * Then DELETE this file for security.
 * URL: http://localhost/GP-Crackers-shop/admin/setup_admin.php
 */

require_once '../includes/config.php';

$response = [];

try {
    // Step 1: Add role column if it doesn't exist
    $pdo->exec("ALTER TABLE `users` ADD COLUMN IF NOT EXISTS `role` ENUM('customer','admin') NOT NULL DEFAULT 'customer' AFTER `email`");
    $response[] = "✅ 'role' column added (or already exists).";

    // Step 2: Check if admin already exists
    $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
    $stmt->execute(['admin@gpcrackers.com']);
    $existing = $stmt->fetch();

    if ($existing) {
        // Update role to admin just in case
        $upd = $pdo->prepare("UPDATE users SET role = 'admin' WHERE email = ?");
        $upd->execute(['admin@gpcrackers.com']);
        $response[] = "ℹ️ Admin user already exists. Role confirmed as 'admin'.";
    } else {
        // Create admin user
        $hashedPassword = password_hash('Admin@123', PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("INSERT INTO users (full_name, phone_number, email, role, password) VALUES (?, ?, ?, 'admin', ?)");
        $stmt->execute(['GP Admin', '9999999999', 'admin@gpcrackers.com', $hashedPassword]);
        $response[] = "✅ Admin user created successfully!";
    }

    $response[] = "<br><strong>Admin Credentials:</strong>";
    $response[] = "📧 Email: admin@gpcrackers.com";
    $response[] = "🔑 Password: Admin@123";
    $response[] = "<br>⚠️ <strong>Delete this file after setup!</strong>";

} catch (PDOException $e) {
    $response[] = "❌ Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Setup - GP Crackers</title>
    <style>
        body { font-family: 'Segoe UI', sans-serif; max-width: 600px; margin: 80px auto; padding: 30px; background: #f8f9fa; }
        .card { background: white; border-radius: 12px; padding: 30px; box-shadow: 0 4px 20px rgba(0,0,0,0.1); }
        h2 { color: #0C2D5E; margin-bottom: 20px; }
        p { line-height: 2; font-size: 15px; }
        .badge { background: #FFBA00; color: #0C2D5E; padding: 4px 12px; border-radius: 20px; font-weight: 700; }
    </style>
</head>
<body>
    <div class="card">
        <h2>🛠️ GP Crackers — Admin Setup</h2>
        <?php foreach ($response as $line): ?>
            <p><?= $line ?></p>
        <?php endforeach; ?>
        <br>
        <p>Go to Admin Login: <a href="login.php"><span class="badge">Admin Login →</span></a></p>
    </div>
</body>
</html>
