<?php
// includes/youtube_stats_card.php

// Fetch the latest stats
try {
    // Get the configured video_id
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'youtube_video_id'");
    $stmt->execute();
    $video_id = $stmt->fetchColumn();

    $stats = null;
    if ($video_id) {
        $stmt = $pdo->prepare("SELECT * FROM stream_stats WHERE video_id = ?");
        $stmt->execute([$video_id]);
        $stats = $stmt->fetch();
    }
} catch (PDOException $e) {
    // Fail silently or log
    $stats = null;
}

$current = $stats['current_viewers'] ?? 0;
$peak = $stats['peak_viewers'] ?? 0;
$last_updated = isset($stats['last_updated']) ? date('H:i', strtotime($stats['last_updated'])) : 'Never';
$is_live = ($current > 0);
?>

<div class="card youtube-stats-card">
    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;">
        <h3 style="margin: 0;"><i class="fab fa-youtube" style="color: #ff0000;"></i> Live Stream</h3>
        <?php if ($is_live): ?>
            <span class="badge-live"><i class="fas fa-circle"></i> LIVE</span>
        <?php else: ?>
            <span class="badge-offline">OFFLINE</span>
        <?php endif; ?>
    </div>

    <div class="stats-row">
        <div class="stat-item">
            <span class="stat-label">Current Viewers</span>
            <span class="stat-value"><?php echo number_format($current); ?></span>
        </div>
        <div class="stat-item">
            <span class="stat-label">All-Time Peak</span>
            <span class="stat-value" style="color: var(--accent-color);"><?php echo number_format($peak); ?></span>
        </div>
    </div>

    <div style="margin-top: 15px; font-size: 0.8rem; color: #888; border-top: 1px solid #333; padding-top: 10px; display: flex; justify-content: space-between;">
        <span>Last Updated: <?php echo $last_updated; ?></span>
        <?php if (hasRole('admin')): ?>
            <a href="index.php?page=youtube_settings" style="color: #bbb; text-decoration: none;"><i class="fas fa-cog"></i> Config</a>
        <?php endif; ?>
    </div>
</div>

<style>
.youtube-stats-card {
    background: linear-gradient(145deg, #1a1a1a, #222);
}

.badge-live {
    background: #ff0000;
    color: #fff;
    padding: 3px 10px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: bold;
    display: flex;
    align-items: center;
    gap: 5px;
    animation: blink 2s infinite;
}

@keyframes blink {
    0% { opacity: 1; }
    50% { opacity: 0.7; }
    100% { opacity: 1; }
}

.badge-offline {
    background: #444;
    color: #888;
    padding: 3px 10px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: bold;
}

.stats-row {
    display: flex;
    gap: 20px;
    margin-top: 10px;
}

.stat-item {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.stat-label {
    font-size: 0.8rem;
    color: #aaa;
    margin-bottom: 5px;
}

.stat-value {
    font-size: 1.8rem;
    font-weight: bold;
    color: #fff;
}
</style>
