<?php
/**
 * Update Admin Accounts to Have Email
 * Everyone logs in with email now
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('includes/config.php');

echo "<h2>Update Admin Accounts - Add Email</h2>";
echo "<style>
body { font-family: Arial; padding: 20px; }
.success { background: #d4edda; color: #155724; padding: 15px; border-left: 4px solid #28a745; margin: 10px 0; }
.info { background: #d1ecf1; color: #0c5460; padding: 15px; border-left: 4px solid #17a2b8; margin: 10px 0; }
.warning { background: #fff3cd; color: #856404; padding: 15px; border-left: 4px solid #ffc107; margin: 10px 0; }
table { border-collapse: collapse; width: 100%; margin: 10px 0; }
th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
th { background: #f8f9fa; }
</style>";

echo "<div class='info'>";
echo "<h3>New Structure:</h3>";
echo "<ul>";
echo "<li>✅ <strong>Everyone logs in with EMAIL</strong></li>";
echo "<li>✅ Admins have: email + username (username is just for display)</li>";
echo "<li>✅ Customers have: email only</li>";
echo "</ul>";
echo "</div>";

// Get admins without email
$admins = mysqli_query($con, "SELECT id, username, email FROM users WHERE role = 'admin'");

echo "<h3>Current Admin Accounts:</h3>";
echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th>Status</th><th>Suggested Email</th></tr>";

$needs_update = [];

while ($admin = mysqli_fetch_assoc($admins)) {
    echo "<tr>";
    echo "<td>{$admin['id']}</td>";
    echo "<td>{$admin['username']}</td>";
    echo "<td>" . ($admin['email'] ?: '<em style="color: red;">NULL</em>') . "</td>";

    if ($admin['email']) {
        echo "<td style='color: green;'>✅ Has Email</td>";
        echo "<td>-</td>";
    } else {
        echo "<td style='color: orange;'>⚠️ Needs Email</td>";
        $suggested = strtolower($admin['username']) . '@awesomemall.com';
        echo "<td><strong>$suggested</strong></td>";
        $needs_update[] = ['id' => $admin['id'], 'username' => $admin['username'], 'email' => $suggested];
    }

    echo "</tr>";
}
echo "</table>";

if (count($needs_update) > 0) {
    echo "<h3>Updating Admin Accounts...</h3>";

    foreach ($needs_update as $admin) {
        $email = $admin['email'];
        $id = $admin['id'];

        $update = "UPDATE users SET email = '$email' WHERE id = $id";

        if (mysqli_query($con, $update)) {
            echo "<div class='success'>✅ Updated admin '{$admin['username']}' with email: <strong>$email</strong></div>";
        } else {
            echo "<div class='warning'>⚠️ Failed to update {$admin['username']}: " . mysqli_error($con) . "</div>";
        }
    }
} else {
    echo "<div class='success'>✅ All admins already have email addresses!</div>";
}

// Final verification
echo "<h3>Final User List:</h3>";
$all_users = mysqli_query($con, "SELECT id, username, email, role FROM users ORDER BY role DESC, id");

echo "<table>";
echo "<tr><th>ID</th><th>Username</th><th>Email</th><th>Role</th><th>Login With</th></tr>";

while ($user = mysqli_fetch_assoc($all_users)) {
    echo "<tr>";
    echo "<td>{$user['id']}</td>";
    echo "<td>" . ($user['username'] ?: '-') . "</td>";
    echo "<td><strong>{$user['email']}</strong></td>";
    echo "<td>" . strtoupper($user['role']) . "</td>";
    echo "<td style='background: #e3f2fd;'><strong>Email: {$user['email']}</strong></td>";
    echo "</tr>";
}
echo "</table>";

echo "<div class='success'>";
echo "<h3>✅ Update Complete!</h3>";
echo "<p><strong>Login Instructions:</strong></p>";
echo "<ul>";
echo "<li><strong>Everyone</strong> logs in with their <strong>EMAIL</strong></li>";
echo "<li>Admins have username (for display) but login with email</li>";
echo "<li>Customers only have email</li>";
echo "</ul>";

echo "<p><strong>Admin Login Credentials:</strong></p>";
$admins_final = mysqli_query($con, "SELECT username, email FROM users WHERE role = 'admin'");
echo "<ul>";
while ($admin = mysqli_fetch_assoc($admins_final)) {
    echo "<li>Email: <strong>{$admin['email']}</strong> (Username: {$admin['username']})</li>";
}
echo "</ul>";
echo "</div>";

echo "<br>";
echo "<a href='login.php' style='padding: 10px 20px; background: #4caf50; color: white; text-decoration: none; border-radius: 5px;'>Test Login</a> ";
echo "<a href='signup.php' style='padding: 10px 20px; background: #2196f3; color: white; text-decoration: none; border-radius: 5px; margin-left: 10px;'>Test Signup</a>";

mysqli_close($con);
?>
