<?php
// Include database configuration
include('config/database.php');

// Admin credentials
$admin_username = 'admin1';
$admin_password = 'admin123'; // This will be hashed
$admin_email = 'admin@ihub.com';

// Hash the password
$hashed_password = password_hash($admin_password, PASSWORD_DEFAULT);

// Check if admin already exists
$check_sql = "SELECT * FROM admin WHERE username = ?";
$check_stmt = $conn->prepare($check_sql);
$check_stmt->bind_param("s", $admin_username);
$check_stmt->execute();
$result = $check_stmt->get_result();

if ($result->num_rows > 0) {
    echo "Admin user already exists!<br>";
    echo "Username: " . $admin_username . "<br>";
    echo "Password: " . $admin_password . "<br>";
} else {
    // Insert admin user
    $sql = "INSERT INTO admin (username, password) VALUES (?, ?)";
    
    if ($stmt = $conn->prepare($sql)) {
        $stmt->bind_param("ss", $admin_username, $hashed_password);
        
        if ($stmt->execute()) {
            echo "<h2>✅ Admin User Created Successfully!</h2>";
            echo "<div style='background: #f8f9fa; padding: 20px; border-radius: 8px; margin: 20px 0;'>";
            echo "<h3>Login Credentials:</h3>";
            echo "<p><strong>Username:</strong> " . $admin_username . "</p>";
            echo "<p><strong>Password:</strong> " . $admin_password . "</p>";

            echo "</div>";
            echo "<p><a href='login.php' style='color: #2c2b7c; text-decoration: none;'>← Go to Login Page</a></p>";
        } else {
            echo "Error creating admin user: " . $stmt->error;
        }
        $stmt->close();
    } else {
        echo "Error preparing statement: " . $conn->error;
    }
}

$check_stmt->close();
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Admin User</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        h2 {
            color: #333;
            text-align: center;
        }
        .credentials {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin: 20px 0;
        }
        .warning {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            color: #856404;
            padding: 15px;
            border-radius: 5px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="warning">
        <strong>⚠️ Security Notice:</strong> Please delete this file (create_admin.php) after creating the admin user for security purposes.
    </div>
</body>
</html>
