<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../auth/AuthController.php';

$auth = new AuthController();
$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Bitte eine gültige E-Mail-Adresse eingeben.';
    } else {
        if ($auth->requestPasswordReset($email)) {
            $message = 'Ein Link zum Zurücksetzen des Passworts wurde gesendet.';
        } else {
            $error = 'Kein aktives Benutzerkonto mit dieser E-Mail gefunden.';
        }
    }
}

ob_start();
?>

<?php if ($message): ?>
    <div class="alert alert-success"><?= htmlspecialchars($message) ?></div>
<?php elseif ($error): ?>
    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>

<form method="post">
    <div class="mb-3">
        <label for="email" class="form-label">E-Mail-Adresse</label>
        <input type="email" name="email" id="email" required class="form-control" value="<?= htmlspecialchars($_POST['email'] ?? '') ?>">
    </div>
    <button type="submit" class="btn btn-primary w-100">Link anfordern</button>
    <a href="login.php" class="btn btn-link w-100 text-center">Zurück zum Login</a>
</form>
<?php
$pageContent = ob_get_clean();
$title = "Passwort vergessen";
include __DIR__ . '/template_form.php';
