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

$status = 'error';
$message = 'Invalid verification link.';

$email = filter_var($_GET['email'] ?? '', FILTER_SANITIZE_EMAIL);
$code = trim($_GET['code'] ?? '');

if (!empty($email) && !empty($code)) {
    $stmt = $conn->prepare("SELECT id, is_verified FROM users WHERE email = ? AND verification_code = ?");
    $stmt->bind_param("ss", $email, $code);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($user = $result->fetch_assoc()) {
        if ($user['is_verified'] == 1) {
            $status = 'already';
            $message = 'Your email is already verified. You can log in.';
        } else {
            $upd = $conn->prepare("UPDATE users SET is_verified = 1 WHERE id = ?");
            $upd->bind_param("i", $user['id']);
            if ($upd->execute()) {
                $status = 'success';
                $message = 'Your email has been verified successfully! You can now log in.';
            } else {
                $message = 'Verification failed. Please try again or contact support.';
            }
            $upd->close();
        }
    } else {
        $message = 'Invalid verification link. The code may have expired or the email is incorrect.';
    }
    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email 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 text-center">
        <div class="bg-blue-900 p-8">
            <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>
        </div>
        <div class="p-8">
            <?php if ($status === 'success'): ?>
                <div class="w-20 h-20 mx-auto mb-6 bg-green-100 rounded-full flex items-center justify-center">
                    <i class="fa-solid fa-check-circle text-green-500 text-4xl"></i>
                </div>
                <h2 class="text-2xl font-bold text-slate-900 mb-2">Email Verified!</h2>
                <p class="text-slate-600 mb-6">
                    <?= htmlspecialchars($message) ?>
                </p>
                <a href="login.php"
                    class="inline-block px-8 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>Sign In
                </a>
            <?php elseif ($status === 'already'): ?>
                <div class="w-20 h-20 mx-auto mb-6 bg-blue-100 rounded-full flex items-center justify-center">
                    <i class="fa-solid fa-info-circle text-blue-500 text-4xl"></i>
                </div>
                <h2 class="text-2xl font-bold text-slate-900 mb-2">Already Verified</h2>
                <p class="text-slate-600 mb-6">
                    <?= htmlspecialchars($message) ?>
                </p>
                <a href="login.php"
                    class="inline-block px-8 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>Sign In
                </a>
            <?php else: ?>
                <div class="w-20 h-20 mx-auto mb-6 bg-red-100 rounded-full flex items-center justify-center">
                    <i class="fa-solid fa-times-circle text-red-500 text-4xl"></i>
                </div>
                <h2 class="text-2xl font-bold text-slate-900 mb-2">Verification Failed</h2>
                <p class="text-slate-600 mb-6">
                    <?= htmlspecialchars($message) ?>
                </p>
                <a href="signup.php"
                    class="inline-block px-8 py-3 bg-blue-900 text-white rounded-lg font-bold hover:bg-blue-800 transition-colors">
                    <i class="fa-solid fa-user-plus mr-2"></i>Try Again
                </a>
            <?php endif; ?>
        </div>
    </div>
</body>

</html>