<?php
/**
 * EMAIL VERIFICATION PAGE
 * File: auth/verify-email.php
 * Handles email verification with token
 */

require_once '../app/Core/init.php';

$token = $_GET['token'] ?? '';
$success = false;
$error_message = '';

// Verify email with token
if (!empty($token)) {
    try {
        $user_model = new User();
        $user = $user_model->findByVerificationToken($token);
        
        if ($user) {
            // Check if already verified
            if ($user['email_verified']) {
                $error_message = 'This email has already been verified. You can sign in now.';
            } else {
                // Update user as verified
                $result = $user_model->update($user['id'], [
                    'email_verified' => 1,
                    'verification_token' => null
                ]);
                
                if ($result) {
                    $success = true;
                    
                    // Send welcome email
                    try {
                        $mailer = new Mailer();
                        $mailer->sendWelcomeEmail($user['email'], $user['first_name']);
                    } catch (Exception $e) {
                        log_error("Welcome email error: " . $e->getMessage());
                    }
                } else {
                    $error_message = 'Failed to verify email. Please try again or contact support.';
                }
            }
        } else {
            $error_message = 'Invalid verification link. The link may have expired or already been used.';
        }
    } catch (Exception $e) {
        log_error("Email verification error: " . $e->getMessage());
        $error_message = 'An error occurred during verification. Please try again or contact support.';
    }
} else {
    $error_message = 'No verification token provided.';
}

$page_title = "Verify Email - " . APP_NAME;
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="../assets/images/favicon.png">
    <title><?= $page_title ?></title>
    <link href="../assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/plugins/fontawesome/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
</head>

<body>
    <!-- Navbar -->
    <?php include '../templates/layouts/frontend/navbar.php'; ?>
    
    <div class="p-3 p-sm-5">
        <div class="row g-4 g-xl-5 justify-content-center">
            <div class="col-xl-5 col-lg-6 col-md-8">
                <div class="authentication-wrap overflow-hidden position-relative text-center my-5">
                    
                    <?php if ($success): ?>
                        <!-- Success Message -->
                        <div class="text-center">
                            <div class="mb-4">
                                <i class="fas fa-check-circle text-success" style="font-size: 64px;"></i>
                            </div>
                            <h2 class="display-6 fw-semibold mb-3">Email Verified Successfully!</h2>
                            <p class="mb-4">Your email has been verified. You can now sign in and start using <?= APP_NAME ?>.</p>
                            <a href="login.php" class="btn btn-primary btn-lg">Sign In Now</a>
                            
                            <?php if (isset($user) && $user['user_type'] === 'vendor'): ?>
                                <div class="alert alert-info mt-4 text-start">
                                    <i class="fas fa-info-circle me-2"></i>
                                    <strong>Vendor Account:</strong> Your vendor account is pending approval. You'll receive an email once it's approved.
                                </div>
                            <?php endif; ?>
                        </div>
                        
                    <?php else: ?>
                        <!-- Error Message -->
                        <div class="text-center">
                            <div class="mb-4">
                                <i class="fas fa-times-circle text-danger" style="font-size: 64px;"></i>
                            </div>
                            <h2 class="display-6 fw-semibold mb-3">Verification Failed</h2>
                            <p class="mb-4"><?= Security::escapeOutput($error_message) ?></p>
                            
                            <div class="d-flex gap-2 justify-content-center">
                                <a href="login.php" class="btn btn-primary">Sign In</a>
                                <a href="register.php" class="btn btn-outline-primary">Sign Up</a>
                            </div>
                            
                            <div class="mt-4">
                                <p class="mb-2">Need help?</p>
                                <a href="../contact.php" class="text-decoration-underline">Contact Support</a>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <div class="col-xl-7 d-none d-xl-block">
                <div class="background-image bg-light d-flex flex-column h-100 justify-content-center p-5 rounded-4" data-image-src="assets/images/lines.svg">
                    <div class="py-5 text-center">
                        <div class="mb-5">
                            <h2 class="fw-semibold">Effortlessly organize your<br> workspace with ease.</h2>
                            <p>It is a long established fact that a reader will be distracted by the readable<br> content of a page when looking at its layout. </p>
                        </div>
                        <img src="../assets/images/png-img/login.png" alt="" class="img-fluid">
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Footer -->
    <?php include '../templates/layouts/frontend/footer.php'; ?>
    
    <!-- Scripts -->
    <script src="../assets/plugins/jQuery/jquery.min.js"></script>
    <script src="../assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="../assets/js/script.js"></script>
</body>
</html>