<?php
require_once __DIR__ . '/auth-check.php';
require_once __DIR__ . '/../includes/functions.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['code']) && !empty($_POST['new_password'])) {
    if ($_POST['new_password'] !== ($_POST['confirm_password'] ?? '')) {
        $error = 'Passwords do not match.';
    } else {
        $tokens = getData('reset-token');
        $now = date('Y-m-d H:i:s');
        $found = false;

        foreach ($tokens as &$token) {
            if ($token['code'] === trim($_POST['code']) && !$token['used'] && $token['expires'] > $now) {
                // Update admin password
                $admin = getData('admin');
                $admin['password'] = password_hash($_POST['new_password'], PASSWORD_BCRYPT);
                saveData('admin', $admin);

                $token['used'] = true;
                $found = true;
                break;
            }
        }

        if ($found) {
            saveData('reset-token', $tokens);
            $success = 'Password updated successfully!';
            setFlash('success', 'Password updated. Please log in with your new password.');
            header('Location: index.php');
            exit;
        } else {
            $error = 'Invalid or expired code.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password | Ethiopia Food Expo</title>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&family=Roboto+Condensed:wght@400;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/assets/css/admin.css">
    <style>
        body { background: var(--green-deep); min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        .login-card { background: var(--bg3); border: 1px solid var(--border); border-radius: 8px; padding: 2.5rem; width: 100%; max-width: 400px; }
    </style>
</head>
<body>
    <div class="login-card">
        <h1 style="font-family:var(--font-heading);font-size:1.1rem;color:#fff;text-transform:uppercase;text-align:center;margin-bottom:1.5rem;">Reset Password</h1>
        <?php if ($error): ?>
        <div class="alert alert-danger" style="font-size:0.85rem;padding:0.75rem 1rem;"><?php echo e($error); ?></div>
        <?php endif; ?>
        <?php if ($success): ?>
        <div class="alert alert-success" style="font-size:0.85rem;padding:0.75rem 1rem;"><?php echo e($success); ?></div>
        <?php endif; ?>
        <form method="POST">
            <div class="mb-3">
                <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">6-Digit Code</label>
                <input type="text" name="code" class="form-control" required maxlength="6" pattern="[0-9]{6}" placeholder="000000" style="background:var(--bg2);color:var(--text);border-color:var(--border);letter-spacing:0.5rem;font-size:1.2rem;text-align:center;">
            </div>
            <div class="mb-3">
                <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">New Password</label>
                <input type="password" name="new_password" class="form-control" required style="background:var(--bg2);color:var(--text);border-color:var(--border);">
            </div>
            <div class="mb-3">
                <label style="font-size:0.75rem;color:var(--muted);text-transform:uppercase;">Confirm Password</label>
                <input type="password" name="confirm_password" class="form-control" required style="background:var(--bg2);color:var(--text);border-color:var(--border);">
            </div>
            <button type="submit" class="btn-admin-primary w-100" style="padding:0.6rem;">Update Password</button>
        </form>
        <div class="text-center mt-3">
            <a href="index.php" style="font-size:0.8rem;color:var(--muted);text-decoration:none;">← Back to Login</a>
        </div>
    </div>
</body>
</html>
