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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['email'])) {
    $admin = getData('admin');
    if (($admin['email'] ?? '') === trim($_POST['email'])) {
        // Generate 6-digit code
        $code = str_pad((string)random_int(0, 999999), 6, '0', STR_PAD_LEFT);
        $tokens = getData('reset-token');
        $tokens[] = [
            'email'     => $admin['email'],
            'code'      => $code,
            'expires'   => date('Y-m-d H:i:s', strtotime('+30 minutes')),
            'used'      => false,
            'created'   => date('Y-m-d H:i:s'),
        ];
        saveData('reset-token', $tokens);

        // Send email
        $subject = 'Password Reset Code — Ethiopia Food Expo Admin';
        $body = "<p>Your password reset code is:</p><h2 style='color:#2e7d32;'>$code</h2><p>This code expires in 30 minutes.</p>";
        sendEmail($admin['email'], $subject, $body);

        $success = 'A 6-digit reset code has been sent to your email.';
        $_SESSION['reset_email'] = $admin['email'];
        header('Location: reset-verify.php');
        exit;
    } else {
        $error = 'Email not found.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot 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;">Forgot 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;">Admin Email</label>
                <input type="email" name="email" class="form-control" required style="background:var(--bg2);color:var(--text);border-color:var(--border);" value="marketing@infairsafrica.com">
            </div>
            <button type="submit" class="btn-admin-primary w-100" style="padding:0.6rem;">Send Reset Code</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>
