<?php
session_start();
require_once 'config.php';

$error = '';
$success = '';
$email = filter_var($_GET['email'] ?? $_POST['email'] ?? '', FILTER_SANITIZE_EMAIL);

if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($email)) {
    $stmt = $conn->prepare("SELECT id, is_verified, verification_code FROM users WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($user = $result->fetch_assoc()) {
        if ($user['is_verified'] == 1) {
            $success = "Your email is already verified. You can <a href='login.php' class='underline font-bold'>log in</a>.";
        } else {
            // Generate new code
            $new_code = substr(md5(uniqid()), 0, 10);
            $upd = $conn->prepare("UPDATE users SET verification_code = ? WHERE id = ?");
            $upd->bind_param("si", $new_code, $user['id']);
            $upd->execute();
            $upd->close();

            // Send email
            require_once 'smtp_mailer.php';
            $mailer = new SmtpMailer(SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASS);
            $verify_link = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/verify.php?email=" . urlencode($email) . "&code=" . $new_code;
            $subject = "Verify your SaniBook Account";
            $message = "<h2>SaniBook Email Verification</h2><p>Click the link below to verify your email:</p><p><a href='$verify_link'>Verify My Account</a></p>";
            try {
                $mailer->send($email, $subject, $message);
                $success = "A new verification email has been sent to <strong>" . htmlspecialchars($email) . "</strong>. Please check your inbox and spam folder.";
            } catch (Exception $e) {
                error_log("Failed to resend verification: " . $e->getMessage());
                $error = "Failed to send email. Please try again later.";
            }
        }
    } else {
        $error = "No account found with that email address.";
    }
    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Resend Verification - SaniBook</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Outfit', sans-serif;
        }
    </style>
</head>

<body class="min-h-screen bg-slate-100 flex items-center justify-center p-4">
    <div class="w-full max-w-md bg-white rounded-2xl shadow-xl overflow-hidden border border-slate-100">
        <div class="bg-blue-900 p-8 text-center">
            <a href="index.php"><img src="images/saniBook logo.png" alt="SaniBook"
                    class="h-12 mx-auto bg-white/10 p-1 rounded-lg"></a>
            <h2 class="text-xl font-bold text-white mt-3">Resend Verification Email</h2>
        </div>
        <div class="p-8">
            <?php if ($success): ?>
                <div class="bg-green-50 border-l-4 border-green-500 text-green-700 p-4 mb-6 rounded text-sm">
                    <i class="fa-solid fa-check-circle mr-2"></i>
                    <?= $success ?>
                </div>
                <a href="login.php"
                    class="block text-center px-6 py-3 bg-blue-900 text-white rounded-lg font-bold hover:bg-blue-800 transition-colors">
                    <i class="fa-solid fa-sign-in-alt mr-2"></i>Go to Login
                </a>
            <?php else: ?>
                <?php if ($error): ?>
                    <div class="bg-red-50 border-l-4 border-red-500 text-red-700 p-4 mb-6 rounded text-sm">
                        <i class="fa-solid fa-exclamation-circle mr-2"></i>
                        <?= htmlspecialchars($error) ?>
                    </div>
                <?php endif; ?>
                <p class="text-slate-600 text-sm mb-4">Enter your email to receive a new verification link.</p>
                <form method="POST" action="" class="space-y-4">
                    <input type="email" name="email" required value="<?= htmlspecialchars($email) ?>"
                        placeholder="your@email.com"
                        class="w-full px-4 py-3 border border-slate-300 rounded-lg focus:ring-2 focus:ring-blue-500 outline-none">
                    <button type="submit"
                        class="w-full px-6 py-3 bg-blue-900 text-white rounded-lg font-bold hover:bg-blue-800 transition-colors">
                        <i class="fa-solid fa-paper-plane mr-2"></i>Resend Verification
                    </button>
                </form>
                <p class="text-center text-sm text-slate-500 mt-4">
                    <a href="login.php" class="text-blue-600 hover:text-blue-800 font-medium">Back to Login</a>
                </p>
            <?php endif; ?>
        </div>
    </div>
</body>

</html>