<?php
// reset-password.php - Emergency Password Reset
require_once 'includes/config.php';

echo "<h2>🔑 Password Reset Tool</h2>";

// Show all users
$users = $pdo->query("SELECT id, full_name, email, role, password FROM users")->fetchAll();

echo "<h3>Current Users:</h3>";
echo "<table border='1' cellpadding='8' style='border-collapse:collapse'>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Role</th><th>Password Hash</th><th>Action</th></tr>";

foreach($users as $u) {
    echo "<tr>";
    echo "<td>{$u['id']}</td>";
    echo "<td>{$u['full_name']}</td>";
    echo "<td>{$u['email']}</td>";
    echo "<td>{$u['role']}</td>";
    echo "<td><small>" . substr($u['password'], 0, 20) . "...</small></td>";
    echo "<td><a href='?reset={$u['id']}' style='color:red'>Reset Password</a></td>";
    echo "</tr>";
}
echo "</table>";

// Handle reset
if(isset($_GET['reset'])) {
    $id = (int)$_GET['reset'];
    $new_password = 'admin123';
    $hash = password_hash($new_password, PASSWORD_DEFAULT);
    
    $pdo->prepare("UPDATE users SET password = ? WHERE id = ?")->execute([$hash, $id]);
    
    // Get user info
    $stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$id]);
    $user = $stmt->fetch();
    
    echo "<div style='background:#eaffea;padding:20px;margin:20px 0;border-radius:10px;border:2px solid #28a745'>";
    echo "<h3 style='color:#155724'>✅ Password Reset Successful!</h3>";
    echo "<p><strong>Name:</strong> {$user['full_name']}</p>";
    echo "<p><strong>Email:</strong> {$user['email']}</p>";
    echo "<p><strong>New Password:</strong> <span style='font-size:24px;font-weight:bold;background:#f0f0f0;padding:5px 15px;border-radius:5px'>admin123</span></p>";
    echo "<p style='color:red'>⚠️ Delete this file after use for security!</p>";
    echo "<a href='auth.php' style='display:inline-block;padding:10px 30px;background:#28a745;color:white;border-radius:50px;text-decoration:none;font-weight:bold;margin-top:10px'>Go to Login Page</a>";
    echo "</div>";
}

echo "<hr><p style='color:red'><strong>DELETE THIS FILE AFTER USE!</strong></p>";
?>