<?php
// Insert Admin Users - Run this once
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Inserting Admin Users into Database</h2>";

// Load database config
require_once('includes/config.php');

if (!$con) {
    die("❌ Database connection failed: " . mysqli_connect_error());
}

echo "✅ Connected to database: <strong>" . DB_NAME . "</strong><br><br>";

// Check if users table exists
$check = mysqli_query($con, "SHOW TABLES LIKE 'users'");
if (mysqli_num_rows($check) == 0) {
    die("❌ Users table does not exist! Please create it first.");
}
echo "✅ Users table exists<br><br>";

// Admin users to insert
$admins = [
    [
        'name' => 'Admin User',
        'email' => 'admin@awesomemall.com',
        'contactno' => '0206994579',
        'password' => '$2y$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5lGBX3tDpFCJm', // Test@123
        'plain_password' => 'Test@123'
    ],
    [
        'name' => 'Administrator',
        'email' => 'admin@awesome-mall.com',
        'contactno' => '0548100237',
        'password' => '$2y$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5lGBX3tDpFCJm', // Test@123
        'plain_password' => 'Test@123'
    ],
    [
        'name' => 'Super Admin',
        'email' => 'superadmin@awesomemall.com',
        'contactno' => '0206994579',
        'password' => '$2y$12$9PzKH5wF.YwqE0xGkPGW2OqYL.rNxH0vXHJPvBJ6fZrQYLGKQJ8Gm', // Admin@2024
        'plain_password' => 'Admin@2024'
    ]
];

echo "<h3>Inserting Admin Accounts:</h3>";
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Status</th><th>Name</th><th>Email</th><th>Password</th></tr>";

foreach ($admins as $admin) {
    // Check if email already exists
    $check = $con->prepare("SELECT id FROM users WHERE email = ?");
    $check->bind_param("s", $admin['email']);
    $check->execute();
    $result = $check->get_result();

    if ($result->num_rows > 0) {
        echo "<tr>";
        echo "<td style='color: orange;'>⚠️ Already Exists</td>";
        echo "<td>{$admin['name']}</td>";
        echo "<td>{$admin['email']}</td>";
        echo "<td>{$admin['plain_password']}</td>";
        echo "</tr>";
        $check->close();
        continue;
    }
    $check->close();

    // Insert user
    $stmt = $con->prepare("INSERT INTO users (name, email, contactno, password) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $admin['name'], $admin['email'], $admin['contactno'], $admin['password']);

    if ($stmt->execute()) {
        echo "<tr>";
        echo "<td style='color: green;'>✅ Success</td>";
        echo "<td>{$admin['name']}</td>";
        echo "<td>{$admin['email']}</td>";
        echo "<td>{$admin['plain_password']}</td>";
        echo "</tr>";
    } else {
        echo "<tr>";
        echo "<td style='color: red;'>❌ Failed</td>";
        echo "<td>{$admin['name']}</td>";
        echo "<td>{$admin['email']}</td>";
        echo "<td>Error: " . mysqli_error($con) . "</td>";
        echo "</tr>";
    }
    $stmt->close();
}

echo "</table>";

echo "<br><h3>✅ All Users in Database:</h3>";
$all_users = mysqli_query($con, "SELECT id, name, email, contactno, regDate FROM users ORDER BY id DESC");

if ($all_users && mysqli_num_rows($all_users) > 0) {
    echo "<table border='1' cellpadding='10'>";
    echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Contact</th><th>Registered</th></tr>";

    while ($user = mysqli_fetch_assoc($all_users)) {
        echo "<tr>";
        echo "<td>{$user['id']}</td>";
        echo "<td>{$user['name']}</td>";
        echo "<td>{$user['email']}</td>";
        echo "<td>{$user['contactno']}</td>";
        echo "<td>{$user['regDate']}</td>";
        echo "</tr>";
    }

    echo "</table>";

    $count = mysqli_num_rows($all_users);
    echo "<br><strong>Total users in database: $count</strong>";
} else {
    echo "⚠️ No users found in database.";
}

echo "<br><br>";
echo "<h3>📋 Login Credentials:</h3>";
echo "<ul>";
echo "<li><strong>Email:</strong> admin@awesomemall.com | <strong>Password:</strong> Test@123</li>";
echo "<li><strong>Email:</strong> admin@awesome-mall.com | <strong>Password:</strong> Test@123</li>";
echo "<li><strong>Email:</strong> superadmin@awesomemall.com | <strong>Password:</strong> Admin@2024</li>";
echo "</ul>";

echo "<br><a href='login.php' style='padding: 10px 20px; background: #E8663D; color: white; text-decoration: none; border-radius: 5px;'>Go to Login Page</a>";
echo " <a href='test-db.php' style='padding: 10px 20px; background: #333; color: white; text-decoration: none; border-radius: 5px; margin-left: 10px;'>Run Database Tests</a>";

echo "<br><br>";
echo "<p style='color: red;'><strong>⚠️ Security Note:</strong> Delete this file after use for security!</p>";

mysqli_close($con);
?>
