<?php
/**
 * TraderAlign — Email Verification Banner
 * Include this on pages that require email verification awareness
 * 
 * Usage: require_once __DIR__ . '/../src/verification-banner.php';
 */

// Check if user is logged in and email not verified
if (
    isset($_SESSION['user_id']) && 
    (!isset($_SESSION['email_verified_at']) || $_SESSION['email_verified_at'] === null)
) {
    // Fetch current verification status from database
    try {
        $dsn = "mysql:host=" . DB_HOST . ";port=" . DB_PORT . ";dbname=" . DB_NAME . ";charset=utf8mb4";
        $pdo = new PDO($dsn, DB_USER, DB_PASS, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]);
        
        $stmt = $pdo->prepare('SELECT email_verified_at FROM users WHERE id = :uid AND deleted_at IS NULL');
        $stmt->execute([':uid' => $_SESSION['user_id']]);
        $user = $stmt->fetch();
        
        // Update session
        $_SESSION['email_verified_at'] = $user['email_verified_at'] ?? null;
        
        // Show banner if not verified
        if ($user && $user['email_verified_at'] === null):
?>
<div id="verification-banner" class="bg-yellow-500/10 border-b border-yellow-500/20">
    <div class="max-w-7xl mx-auto px-4 py-3">
        <div class="flex items-center justify-between flex-wrap gap-3">
            <div class="flex items-center gap-3">
                <div class="flex-shrink-0">
                    <svg class="w-5 h-5 text-yellow-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.34 16.5c-.77.833.192 2.5 1.732 2.5z"/>
                    </svg>
                </div>
                <p class="text-sm text-yellow-200">
                    <span class="font-medium">Verify your email address.</span> Some features are limited until you do.
                </p>
            </div>
            <div class="flex items-center gap-3">
                <a href="<?php echo APP_URL; ?>/resend-verification" class="text-sm font-medium text-yellow-300 hover:text-yellow-200 transition-colors underline underline-offset-2">
                    Resend verification
                </a>
                <button onclick="document.getElementById('verification-banner').remove()" class="text-yellow-500 hover:text-yellow-400 transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                    </svg>
                </button>
            </div>
        </div>
    </div>
</div>
<?php 
        endif;
    } catch (PDOException $e) {
        // Silently fail - don't break the page if DB check fails
        error_log("Verification banner DB check failed: " . $e->getMessage());
    }
}
?>