<?php
$conn = new mysqli('localhost', 'root', '', 'lostfound_db');
if ($conn->connect_error) die("<p style='color:red'>❌ DB Error: " . $conn->connect_error . "</p>");

$fixes = [];

// 1. Add account_status column if missing
$col = $conn->query("SHOW COLUMNS FROM users LIKE 'account_status'");
if ($col->num_rows === 0) {
    $conn->query("ALTER TABLE users ADD COLUMN account_status ENUM('pending','active','rejected') DEFAULT 'pending'");
    $fixes[] = "✅ Added missing <strong>account_status</strong> column.";
}

// 2. Make password nullable (for Google users)
$conn->query("ALTER TABLE users MODIFY COLUMN password VARCHAR(255) NULL");

// 3. Fix admin password to admin123 and set active
$correct_hash = password_hash('admin123', PASSWORD_DEFAULT);
$conn->query("UPDATE users SET password='$correct_hash', account_status='active' WHERE role='admin'");
$fixes[] = "✅ Reset admin password to <strong>admin123</strong> and set status to <strong>active</strong>.";

// Show fixes
foreach ($fixes as $f) echo "<p style='color:green;font-family:sans-serif'>$f</p>";

// Show all users
$result = $conn->query("SELECT id, name, email, role, account_status, password FROM users ORDER BY id");
echo "<table border='1' cellpadding='8' style='font-family:sans-serif;font-size:13px;border-collapse:collapse'>
    <tr style='background:#f3f4f6'>
        <th>ID</th><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th>Test admin123</th>
    </tr>";
while ($u = $result->fetch_assoc()) {
    $match = password_verify('admin123', $u['password'] ?? '') ? '✅ MATCH' : '❌ no match';
    $status_color = $u['account_status'] === 'active' ? 'green' : ($u['account_status'] === 'rejected' ? 'red' : 'orange');
    echo "<tr>
        <td>{$u['id']}</td>
        <td>{$u['name']}</td>
        <td>{$u['email']}</td>
        <td>{$u['role']}</td>
        <td style='color:{$status_color};font-weight:bold'>{$u['account_status']}</td>
        <td><strong>$match</strong></td>
    </tr>";
}
echo "</table>";

echo "<br><a href='/lostfound_relink/auth/login.php' style='font-family:sans-serif;color:blue'>→ Go to Login</a>";
$conn->close();
?>
