<?php
// Update Admin Password Script
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('includes/config.php');

echo "<h2>Admin Password Update Script</h2>";

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

echo "✅ Connected to database<br><br>";

// New password to set
$new_password = "12345678";
$password_hash = password_hash($new_password, PASSWORD_DEFAULT);

echo "<h3>Step 1: Finding Admin Accounts</h3>";

// Find all admin-like users
$query = "SELECT id, name, email FROM users WHERE email LIKE '%admin%' OR name LIKE '%admin%'";
$result = mysqli_query($con, $query);

if ($result && mysqli_num_rows($result) > 0) {
    echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
    echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Action</th></tr>";

    $admin_users = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $admin_users[] = $row;
        echo "<tr>";
        echo "<td>{$row['id']}</td>";
        echo "<td>{$row['name']}</td>";
        echo "<td>{$row['email']}</td>";
        echo "<td><a href='?update_id={$row['id']}' style='color: blue;'>Update Password</a></td>";
        echo "</tr>";
    }
    echo "</table>";

    // If update requested
    if (isset($_GET['update_id'])) {
        $user_id = (int) $_GET['update_id'];

        echo "<br><h3>Step 2: Updating Password</h3>";

        // Update password
        $stmt = $con->prepare("UPDATE users SET password = ? WHERE id = ?");
        $stmt->bind_param("si", $password_hash, $user_id);

        if ($stmt->execute()) {
            // Get updated user info
            $check = $con->prepare("SELECT name, email FROM users WHERE id = ?");
            $check->bind_param("i", $user_id);
            $check->execute();
            $user = $check->get_result()->fetch_assoc();

            echo "<div style='background: #e8f5e9; padding: 20px; border-left: 4px solid #4caf50; border-radius: 4px;'>";
            echo "<h4 style='color: #2e7d32; margin: 0 0 10px 0;'>✅ Password Updated Successfully!</h4>";
            echo "<p><strong>User:</strong> {$user['name']}</p>";
            echo "<p><strong>Email:</strong> {$user['email']}</p>";
            echo "<p><strong>New Password:</strong> <code style='background: #f5f5f5; padding: 5px 10px; border-radius: 3px;'>$new_password</code></p>";
            echo "</div>";

            echo "<br><div style='background: #fff3cd; padding: 15px; border-left: 4px solid #ffc107; border-radius: 4px;'>";
            echo "<p><strong>⚠️ Login Credentials:</strong></p>";
            echo "<p>Email: <strong>{$user['email']}</strong></p>";
            echo "<p>Password: <strong>$new_password</strong></p>";
            echo "</div>";

            $check->close();
        } else {
            echo "<div style='background: #fee; padding: 20px; border-left: 4px solid #d32f2f; border-radius: 4px;'>";
            echo "<p style='color: #d32f2f;'>❌ Failed to update password: " . mysqli_error($con) . "</p>";
            echo "</div>";
        }
        $stmt->close();
    }

} else {
    echo "<p style='color: orange;'>⚠️ No admin users found. Let me search for ALL users...</p>";

    // Show all users
    $all_users = mysqli_query($con, "SELECT id, name, email FROM users LIMIT 10");

    if ($all_users && mysqli_num_rows($all_users) > 0) {
        echo "<h3>All Users in Database:</h3>";
        echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
        echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Action</th></tr>";

        while ($row = mysqli_fetch_assoc($all_users)) {
            echo "<tr>";
            echo "<td>{$row['id']}</td>";
            echo "<td>{$row['name']}</td>";
            echo "<td>{$row['email']}</td>";
            echo "<td><a href='?update_id={$row['id']}' style='color: blue;'>Update Password</a></td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p style='color: red;'>❌ No users found in database!</p>";
        echo "<p>You need to create an admin user first.</p>";

        // Offer to create admin
        if (isset($_GET['create_admin']) && $_GET['create_admin'] == 'yes') {
            echo "<h3>Creating Admin User...</h3>";

            $admin_name = "Admin User";
            $admin_email = "admin@awesomemall.com";
            $admin_contact = "0206994579";

            $stmt = $con->prepare("INSERT INTO users (name, email, contactno, password) VALUES (?, ?, ?, ?)");
            $stmt->bind_param("ssss", $admin_name, $admin_email, $admin_contact, $password_hash);

            if ($stmt->execute()) {
                echo "<div style='background: #e8f5e9; padding: 20px; border-left: 4px solid #4caf50; border-radius: 4px;'>";
                echo "<h4 style='color: #2e7d32;'>✅ Admin User Created!</h4>";
                echo "<p><strong>Email:</strong> $admin_email</p>";
                echo "<p><strong>Password:</strong> $new_password</p>";
                echo "</div>";
            } else {
                echo "<p style='color: red;'>❌ Failed to create admin: " . mysqli_error($con) . "</p>";
            }
            $stmt->close();
        } else {
            echo "<br><a href='?create_admin=yes' style='padding: 10px 20px; background: #4caf50; color: white; text-decoration: none; border-radius: 5px; display: inline-block;'>Create Admin User</a>";
        }
    }
}

echo "<br><br>";
echo "<div style='margin-top: 30px; padding: 20px; background: #f5f5f5; border-radius: 5px;'>";
echo "<h3>Quick Actions:</h3>";
echo "<a href='workflow/test-db.php' style='padding: 10px 20px; background: #2196f3; color: white; text-decoration: none; border-radius: 5px; margin-right: 10px;'>Test Database</a>";
echo "<a href='login.php' style='padding: 10px 20px; background: #E8663D; color: white; text-decoration: none; border-radius: 5px; margin-right: 10px;'>Go to Login</a>";
echo "<a href='workflow/reset-login.php' style='padding: 10px 20px; background: #333; color: white; text-decoration: none; border-radius: 5px;'>Reset Login Attempts</a>";
echo "</div>";

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

mysqli_close($con);
?>
