<?php
session_start();
require_once '../connection.php';

$token = $_GET['token'] ?? '';

if (!empty($token)) {
    // Check if token exists and is not expired
    $stmt = $conn->prepare("SELECT id, username, email FROM users WHERE verification_token = ? AND verification_expiry > NOW() AND email_verified = 0");
    $stmt->bind_param("s", $token);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        
        // Verify the user
        $update = $conn->prepare("UPDATE users SET email_verified = 1, verification_token = NULL, updated_at = NOW() WHERE id = ?");
        $update->bind_param("i", $user['id']);
        
        if ($update->execute()) {
            // Set session for automatic login
            $_SESSION['user_id'] = $user['id'];
            $_SESSION['username'] = $user['username'];
            $_SESSION['email'] = $user['email'];
            $_SESSION['verified'] = true;
            
            // Set a welcome message
            $_SESSION['welcome_message'] = "Welcome to Mexes Trade! Your account has been verified successfully.";
            
            // Redirect to account dashboard
            header("Location: index.php");
            exit();
        } else {
            $error = "Verification failed. Please try again.";
        }
        $update->close();
    } else {
        $error = "Invalid or expired verification token.";
    }
    $stmt->close();
} else {
    $error = "No verification token provided.";
}

// If verification failed, show error page
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Verification - Mexes Trade</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">
    <style>
        body { font-family: 'Inter', sans-serif; background-color: #0a0f1c; }
    </style>
</head>
<body class="text-white antialiased">
    <div class="min-h-screen flex items-center justify-center p-4">
        <div class="w-full max-w-md bg-[#111827] border border-gray-800 rounded-2xl p-8 text-center">
            <div class="w-20 h-20 <?php echo isset($error) ? 'bg-red-500/10' : 'bg-green-500/10'; ?> rounded-full flex items-center justify-center mx-auto mb-6">
                <i class="<?php echo isset($error) ? 'fas fa-exclamation-circle text-red-400' : 'fas fa-check-circle text-green-400'; ?> text-3xl"></i>
            </div>
            <h1 class="text-2xl font-bold mb-4"><?php echo isset($error) ? 'Verification Failed' : 'Email Verified!'; ?></h1>
            <p class="text-gray-300 mb-8">
                <?php 
                if (isset($error)) {
                    echo htmlspecialchars($error);
                } else {
                    echo "Your email has been verified successfully!";
                }
                ?>
            </p>
            <div class="flex flex-col gap-3">
                <a href="index.php" class="bg-blue-600 hover:bg-blue-500 text-white font-bold py-3 px-6 rounded-xl inline-flex items-center justify-center gap-2 transition-colors">
                    <i class="fas fa-home"></i> Back to Home
                </a>
                <?php if (isset($error)): ?>
                    <a href="register.php" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-3 px-6 rounded-xl inline-flex items-center justify-center gap-2 transition-colors">
                        <i class="fas fa-user-plus"></i> Try Registering Again
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</body>
</html>