<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/csrf.php';

$token = $_GET['token'] ?? $_POST['token'] ?? '';
$error = '';
$done  = false;

// Validate token first
$admin = $token ? db_row(
    'SELECT * FROM admin_users WHERE reset_token = :t AND reset_expires > NOW() AND active = 1',
    ['t' => hash('sha256', $token)]
) : null;

if (!$admin) {
    $error = 'This reset link is invalid or has expired. Please request a new one.';
}

if ($admin && $_SERVER['REQUEST_METHOD'] === 'POST') {
    csrf_verify();
    $pw1 = (string)($_POST['password']  ?? '');
    $pw2 = (string)($_POST['password2'] ?? '');

    if (strlen($pw1) < 8) {
        $error = 'Password must be at least 8 characters.';
    } elseif ($pw1 !== $pw2) {
        $error = 'Passwords do not match.';
    } else {
        $consumed = auth_admin_consume_reset_token($token);
        if ($consumed) {
            auth_admin_set_password((int)$consumed['id'], $pw1);
            app_log("Admin password reset completed for: {$consumed['email']}");
            $done = true;
        } else {
            $error = 'Could not complete the reset. Please request a new link.';
        }
    }
}
?><!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<title>Set new password — Admin</title>
<link rel="icon" type="image/png" href="../assets/img/favicon.png">
<style>
*{box-sizing:border-box;}
html,body{margin:0;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:#0e0e0e;color:#fff;min-height:100vh;}
body{display:flex;align-items:center;justify-content:center;padding:1rem;background:radial-gradient(circle at 50% 0%, #232323 0%, #0e0e0e 60%);}
.al-card{background:#1a1a1a;border:1px solid #2a2a2a;border-radius:14px;padding:2.5rem;width:100%;max-width:420px;box-shadow:0 30px 80px rgba(0,0,0,.6);}
.al-logo{text-align:center;margin-bottom:1.5rem;}
.al-logo img{height:56px;}
.al-title{font-size:1.4rem;font-weight:600;text-align:center;margin:0 0 .25rem;}
.al-sub{text-align:center;color:#9a9a9a;font-size:.85rem;margin:0 0 2rem;}
.al-card label{display:block;color:#9a9a9a;font-size:.78rem;text-transform:uppercase;letter-spacing:.08em;font-weight:600;margin-bottom:.4rem;}
.al-card input[type="password"]{width:100%;padding:.85rem 1rem;background:#0e0e0e;color:#fff;border:1px solid #2a2a2a;border-radius:8px;font-size:1rem;margin-bottom:1.25rem;}
.al-card input:focus{outline:none;border-color:#7a9d47;}
.al-btn{width:100%;padding:.95rem;background:#7a9d47;color:#fff;border:none;border-radius:8px;font-size:1rem;font-weight:600;cursor:pointer;}
.al-error{background:#3b1818;border:1px solid #7a2828;color:#ffabab;padding:.75rem 1rem;border-radius:8px;font-size:.88rem;margin-bottom:1.25rem;}
.al-success{background:#1a3a1a;border:1px solid #2a6e2a;color:#a8e3a8;padding:1rem;border-radius:8px;font-size:.9rem;margin-bottom:1rem;}
.al-foot{text-align:center;margin-top:1.5rem;font-size:.85rem;}
.al-foot a{color:#9a9a9a;text-decoration:none;}
</style>
</head><body>

<div class="al-card">
    <div class="al-logo"><img src="../assets/img/buylocal-stamp.png" alt="Buy Local Lowveld"></div>
    <h1 class="al-title">Set New Password</h1>
    <p class="al-sub"><?= $admin && !$done ? htmlspecialchars($admin['email']) : '' ?></p>

    <?php if ($done): ?>
        <div class="al-success">
            <strong>Password updated.</strong> You can now sign in with your new password.
        </div>
        <p class="al-foot"><a href="login.php" style="color:#7a9d47;">→ Continue to sign in</a></p>
    <?php elseif (!$admin): ?>
        <div class="al-error"><?= htmlspecialchars($error) ?></div>
        <p class="al-foot"><a href="forgot.php">Request a new reset link →</a></p>
    <?php else: ?>
        <?php if ($error): ?><div class="al-error"><?= htmlspecialchars($error) ?></div><?php endif; ?>
        <form method="post">
            <?= csrf_field() ?>
            <input type="hidden" name="token" value="<?= htmlspecialchars($token) ?>">
            <label>New password</label>
            <input type="password" name="password" required minlength="8" autocomplete="new-password">
            <label>Confirm password</label>
            <input type="password" name="password2" required minlength="8" autocomplete="new-password">
            <button type="submit" class="al-btn">Set password →</button>
        </form>
    <?php endif; ?>
</div>

</body></html>