<?php
/**
 * EMAIL VERIFICATION HANDLER
 * File: auth/verify-email.php
 * Handles email verification from token link
 */

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

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

if (empty($token)) {
    $error = 'Invalid verification link. Token is missing.';
} else {
    try {
        $userModel = new User();
        
        // Find user by verification token
        $user = $userModel->findByVerificationToken($token);
        
        if (!$user) {
            $error = 'Invalid or expired verification token. The link may have expired or already been used.';
        } elseif ($user['email_verified'] == 1) {
            $error = 'This email address has already been verified. You can login to your account.';
        } else {
            // Verify the email
            $result = $userModel->verifyEmail($user['id']);
            
            if ($result) {
                $success = true;
                
                // Send welcome email (optional)
                try {
                    $mailer = new Mailer();
                    
                    // Get user full name
                    $name = '';
                    if ($user['user_type'] === 'customer') {
                        $customerModel = new Customer();
                        $customer = $customerModel->findByUserId($user['id']);
                        $name = $customer['first_name'] ?? 'there';
                    } elseif ($user['user_type'] === 'vendor') {
                        $vendorModel = new Vendor();
                        $vendor = $vendorModel->findByUserId($user['id']);
                        $name = $vendor['business_name'] ?? 'there';
                    }
                    
                    $mailer->sendWelcomeEmail($user['email'], $name);
                } catch (Exception $e) {
                    log_message('Welcome email error: ' . $e->getMessage(), 'error');
                }
                
                flash('success', 'Email verified successfully! You can now login to your account.');
            } else {
                $error = 'Failed to verify email. Please try again or contact support.';
            }
        }
    } catch (Exception $e) {
        log_message('Email verification error: ' . $e->getMessage(), 'error');
        $error = 'An error occurred during verification. Please try again later.';
    }
}

$page_title = 'Email Verification - ' . APP_NAME;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="../assets/plugins/fontawesome/css/all.min.css">
    <link rel="stylesheet" href="../assets/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../assets/css/style.css">
    <style>
        .verification-container {
            min-height: 80vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 40px 20px;
        }
        .verification-card {
            max-width: 600px;
            width: 100%;
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            padding: 40px;
            text-align: center;
        }
        .verification-icon {
            width: 100px;
            height: 100px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0 auto 30px;
            font-size: 50px;
        }
        .verification-icon.success {
            background: #d4edda;
            color: #28a745;
        }
        .verification-icon.error {
            background: #f8d7da;
            color: #05472A;
        }
        .verification-icon.already {
            background: #fff3cd;
            color: #ffc107;
        }
        .verification-card h2 {
            margin-bottom: 20px;
            color: #333;
        }
        .verification-card p {
            color: #666;
            font-size: 16px;
            margin-bottom: 30px;
            line-height: 1.6;
        }
        .btn-lg {
            padding: 12px 40px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <!-- Header -->
    <?php include '../templates/layouts/frontend/header.php'; ?>
    
    <div class="verification-container">
        <div class="verification-card">
            <?php if ($success): ?>
                <div class="verification-icon success">
                    <i class="fas fa-check-circle"></i>
                </div>
                <h2>Email Verified Successfully!</h2>
                <p>Your email address has been verified. You can now access all features of your account.</p>
                <a href="login.php" class="btn btn-primary btn-lg">
                    <i class="fas fa-sign-in-alt me-2"></i>Login to Your Account
                </a>
            <?php elseif (strpos($error, 'already been verified') !== false): ?>
                <div class="verification-icon already">
                    <i class="fas fa-check-double"></i>
                </div>
                <h2>Already Verified</h2>
                <p><?= Security::escapeOutput($error) ?></p>
                <a href="login.php" class="btn btn-primary btn-lg">
                    <i class="fas fa-sign-in-alt me-2"></i>Login to Your Account
                </a>
            <?php else: ?>
                <div class="verification-icon error">
                    <i class="fas fa-times-circle"></i>
                </div>
                <h2>Verification Failed</h2>
                <p><?= Security::escapeOutput($error) ?></p>
                <div class="d-flex gap-3 justify-content-center">
                    <a href="../index.php" class="btn btn-secondary">
                        <i class="fas fa-home me-2"></i>Go to Homepage
                    </a>
                    <a href="mailto:support@rentidium.com" class="btn btn-outline-primary">
                        <i class="fas fa-envelope me-2"></i>Contact Support
                    </a>
                </div>
            <?php endif; ?>
        </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>
