<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../classes/User.php';

$user = new User();
$error = null;
$success = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $identifier = trim($_POST['email'] ?? '');
    if ($identifier === '') {
        $error = 'Please enter your email address.';
    } else {
        try {
            $user->resetPassword($identifier);
            $success = 'If an account exists for that email, a password reset link has been sent.';
        } catch (Exception $e) {
            // Do not expose whether the email exists; show generic message
            $success = 'If an account exists for that email, a password reset link has been sent.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password - SmartThink</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body {
            margin: 0;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #fdfcfb 0%, #f9fafb 40%, #fef3c7 100%);
        }
        .auth-page { width: 100%; padding: 1.5rem; }
        .auth-shell {
            max-width: 520px;
            margin: 0 auto;
            background: #f9fafb;
            border-radius: 32px;
            box-shadow: 0 28px 80px rgba(15, 23, 42, 0.18);
            padding: 2.5rem 2.75rem;
        }
        .auth-brand { display:flex; align-items:center; gap:0.75rem; margin-bottom:2rem; }
        .auth-brand-logo {
            width: 40px; height: 40px; border-radius:999px; background:#fef9c3;
            display:flex; align-items:center; justify-content:center;
            box-shadow: 0 6px 18px rgba(234, 179, 8, 0.45);
        }
        .auth-brand-logo i { color:#f59e0b; }
        .auth-brand-text { font-weight:600; letter-spacing:0.04em; font-size:0.95rem; }
        .auth-heading { font-size:1.6rem; font-weight:600; margin-bottom:0.4rem; }
        .auth-subtitle { font-size:0.9rem; color:#6b7280; margin-bottom:1.5rem; }
        .form-label { font-size:0.8rem; color:#4b5563; }
        .form-control {
            background-color:#f9fafb; border-color:#e5e7eb; color:#111827;
            border-radius:999px; padding-left:1.15rem; padding-right:1.15rem;
            height:46px; font-size:0.9rem;
        }
        .form-control:focus {
            border-color:#fb923c; box-shadow:0 0 0 1px rgba(251,146,60,0.6);
            background-color:#fefce8; color:#111827;
        }
        .auth-submit-btn {
            background: linear-gradient(90deg, #facc15, #fb923c);
            border:none; border-radius:999px; font-weight:600; color:#111827;
            height:48px; font-size:0.95rem;
        }
        .auth-submit-btn:hover { background: linear-gradient(90deg, #eab308, #f97316); }
        .auth-footer-text { margin-top:1.75rem; font-size:0.8rem; color:#9ca3af; }
        .text-muted-link { color:#9ca3af; text-decoration:none; }
        .text-muted-link:hover { color:#111827; }
        .alert { border-radius:0.75rem; font-size:0.85rem; }
    </style>
</head>
<body>
<div class="auth-page">
    <div class="auth-shell">
        <div class="auth-brand">
            <div class="auth-brand-logo"><i class="fas fa-graduation-cap"></i></div>
            <div class="auth-brand-text">SmartThink</div>
        </div>

        <h1 class="auth-heading">Forgot your password?</h1>
        <p class="auth-subtitle">Enter the email address associated with your account and we'll send you a link to reset your password.</p>

        <?php if ($success): ?>
            <div class="alert alert-success py-2 mb-3" role="alert">
                <?php echo htmlspecialchars($success); ?>
            </div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-danger py-2 mb-3" role="alert">
                <?php echo htmlspecialchars($error); ?>
            </div>
        <?php endif; ?>

        <form method="post" novalidate>
            <div class="mb-3">
                <label for="email" class="form-label">Email address</label>
                <input type="email" class="form-control" id="email" name="email" required>
            </div>

            <button type="submit" class="btn w-100 mb-2 auth-submit-btn">
                <i class="fas fa-paper-plane me-2"></i>Send reset link
            </button>

            <div class="text-center small text-muted mt-3">
                Remembered your password? <a href="login.php" class="text-muted-link">Back to login</a>
            </div>
        </form>

        <div class="auth-footer-text">
            &copy; <?php echo date('Y'); ?> SmartThink. All rights reserved.
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
