<?php
// Inclure la configuration si ce n'est pas déjà fait
if (!function_exists('getTrendingVideos')) {
    require_once __DIR__ . '/config.php';
}

// Récupérer les vidéos tendances depuis l'API PeerTube
$featuredVideos = getTrendingVideos(FEATURED_VIDEOS_COUNT);

// Affichage des vidéos
foreach ($featuredVideos as $video):
?>
    <div class="video-card" data-video-id="<?php echo $video['id']; ?>">
        <div class="video-thumbnail">
            <img src="<?php echo $video['thumbnail']; ?>" alt="<?php echo $video['title']; ?>" data-src="<?php echo $video['thumbnail']; ?>">
            <div class="video-duration"><?php echo formatDuration($video['duration']); ?></div>
        </div>
        <div class="video-info">
            <h3 class="video-title"><?php echo $video['title']; ?></h3>
            <div class="video-channel"><?php echo $video['channel']; ?></div>
            <div class="video-metadata">
                <?php if (defined('SHOW_VIDEO_VIEWS') && SHOW_VIDEO_VIEWS): ?>
                <span class="video-views"><?php echo formatViewCount($video['views']); ?> vues</span>
                <?php endif; ?>
                <span class="video-date"><?php echo formatDate($video['date']); ?></span>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<?php
// Fonctions utilitaires (dans un vrai projet, ces fonctions seraient dans un fichier séparé)
function formatDuration($seconds) {
    $hours = floor($seconds / 3600);
    $minutes = floor(($seconds % 3600) / 60);
    $remainingSeconds = $seconds % 60;
    
    if ($hours > 0) {
        return sprintf('%d:%02d:%02d', $hours, $minutes, $remainingSeconds);
    } else {
        return sprintf('%d:%02d', $minutes, $remainingSeconds);
    }
}

function formatViewCount($views) {
    if ($views >= 1000000) {
        return round($views / 1000000, 1) . 'M';
    } elseif ($views >= 1000) {
        return round($views / 1000, 1) . 'K';
    } else {
        return $views;
    }
}

function formatDate($dateString) {
    $date = new DateTime($dateString);
    $now = new DateTime();
    $interval = $now->diff($date);
    
    if ($interval->days == 0) {
        return 'Aujourd\'hui';
    } elseif ($interval->days == 1) {
        return 'Hier';
    } elseif ($interval->days < 7) {
        return 'Il y a ' . $interval->days . ' jours';
    } elseif ($interval->days < 30) {
        $weeks = floor($interval->days / 7);
        return 'Il y a ' . $weeks . ' semaine' . ($weeks > 1 ? 's' : '');
    } elseif ($interval->days < 365) {
        $months = floor($interval->days / 30);
        return 'Il y a ' . $months . ' mois';
    } else {
        $years = floor($interval->days / 365);
        return 'Il y a ' . $years . ' an' . ($years > 1 ? 's' : '');
    }
}
?> 