<?php
// TEMPORARY PASSWORD RESET TOOL — DELETE THIS FILE AFTER USE!
require_once __DIR__ . '/includes/config.php';

$done = false;
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $newPass = $_POST['new_password'] ?? '';
    $username = trim($_POST['username'] ?? 'admin');
    if (strlen($newPass) >= 4) {
        $hash = password_hash($newPass, PASSWORD_DEFAULT);
        $st = db()->prepare("UPDATE admin_users SET password=? WHERE username=?");
        $st->bind_param('ss', $hash, $username);
        $st->execute();
        if ($st->affected_rows > 0) {
            $done = true;
        } else {
            // User might not exist, insert
            $st2 = db()->prepare("INSERT INTO admin_users (username, password, full_name, email, role) VALUES (?, ?, 'CY Admin', 'admin@cyconsultancy.com', 'superadmin')");
            $st2->bind_param('ss', $username, $hash);
            $st2->execute();
            $done = true;
        }
    } else {
        $error = 'Password must be at least 4 characters.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reset Admin Password</title>
<style>
body{font-family:Arial,sans-serif;max-width:420px;margin:80px auto;padding:20px;}
h2{color:#1a56db;margin-bottom:20px;}
label{display:block;font-size:13px;font-weight:600;margin-bottom:5px;color:#374151;}
input{width:100%;padding:10px;border:1.5px solid #d1d5db;border-radius:8px;font-size:14px;margin-bottom:14px;box-sizing:border-box;}
button{width:100%;padding:11px;background:#1a56db;color:#fff;border:none;border-radius:8px;font-size:15px;font-weight:700;cursor:pointer;}
button:hover{background:#1e3a8a;}
.success{background:#d1fae5;color:#065f46;border:1px solid #6ee7b7;border-radius:8px;padding:14px;margin-bottom:16px;}
.error-box{background:#fee2e2;color:#991b1b;border:1px solid #fca5a5;border-radius:8px;padding:14px;margin-bottom:16px;}
.warn{background:#fef3c7;color:#92400e;border:1px solid #fcd34d;border-radius:8px;padding:12px;font-size:12px;margin-top:16px;}
code{background:#f3f4f6;padding:2px 6px;border-radius:4px;font-size:13px;}
</style>
</head>
<body>
<h2>🔑 Reset Admin Password</h2>

<?php if ($done): ?>
  <div class="success">
    ✅ Password updated successfully!<br>
    <strong>Now <a href="login.php">go to login</a> and use your new password.</strong>
  </div>
  <div class="warn">⚠️ <strong>Important:</strong> Delete this file after use!<br>
  File: <code>admin/reset-password.php</code></div>
<?php else: ?>

  <?php if ($error): ?>
    <div class="error-box">❌ <?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <form method="POST">
    <label>Username</label>
    <input type="text" name="username" value="admin" required>
    <label>New Password</label>
    <input type="password" name="new_password" placeholder="Enter new password" required autofocus>
    <button type="submit">Set New Password</button>
  </form>

  <div class="warn">⚠️ <strong>Delete this file after use!</strong></div>
<?php endif; ?>
</body>
</html>
