<?php
require_once __DIR__ . '/../config/auth.php';

if (isLoggedIn()) {
    header('Location: ' . getRedirectUrl());
    exit;
}

$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    if (empty($email)) {
        $error = 'Please enter your email address.';
    } else {
        $pdo = getDB();
        $stmt = $pdo->prepare("SELECT id, name FROM users WHERE email = ? AND status = 1");
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user) {
            $token = bin2hex(random_bytes(32));
            $expires = date('Y-m-d H:i:s', strtotime('+1 hour'));
            $stmt = $pdo->prepare("UPDATE users SET reset_token = ?, reset_token_expires = ? WHERE id = ?");
            $stmt->execute([$token, $expires, $user['id']]);

            $resetLink = BASE_URL . 'auth/reset_password.php?token=' . $token;
            logActivity('Password Reset Request', 'Auth', $user['id']);

            // In production, send email. For now, show the link.
            $message = 'Password reset link generated: <a href="' . $resetLink . '">Click here to reset</a> (In production, this would be emailed).';
        } else {
            // Don't reveal if email exists
            $message = 'If this email is registered, you will receive a password reset link.';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password - DigiMobiz</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="<?= BASE_URL ?>assets/css/style.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container">
        <div class="row justify-content-center align-items-center min-vh-100">
            <div class="col-md-4">
                <div class="card shadow-lg border-0 rounded-3">
                    <div class="card-header bg-warning text-dark text-center py-4 rounded-top">
                        <i class="fas fa-key fa-3x mb-2"></i>
                        <h4 class="mb-0">Forgot Password</h4>
                        <small>Enter your email to reset</small>
                    </div>
                    <div class="card-body p-4">
                        <?php if ($message): ?>
                            <div class="alert alert-success alert-dismissible fade show"><?= $message ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
                        <?php endif; ?>
                        <?php if ($error): ?>
                            <div class="alert alert-danger alert-dismissible fade show"><?= htmlspecialchars($error) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>
                        <?php endif; ?>
                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label"><i class="fas fa-envelope me-2"></i>Email Address</label>
                                <input type="email" name="email" class="form-control form-control-lg" placeholder="admin@example.com" required>
                            </div>
                            <div class="d-grid">
                                <button type="submit" class="btn btn-warning btn-lg"><i class="fas fa-paper-plane me-2"></i>Send Reset Link</button>
                            </div>
                            <div class="text-center mt-3">
                                <a href="<?= BASE_URL ?>auth/login.php" class="text-muted small"><i class="fas fa-arrow-left me-1"></i>Back to Login</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
