<?php
// Inclure la configuration
require_once 'includes/config.php';

// Vérifier si le mode compte à rebours est activé
if (defined('COUNTDOWN_ENABLED') && COUNTDOWN_ENABLED === true) {
    // Rediriger vers la page de compte à rebours
    require_once 'countdown.php';
    exit;
}

// Inclure les fonctions de données structurées
require_once 'includes/structured-data.php';
// Appliquer les en-têtes de sécurité
setSecurityHeaders();

// Récupérer les épisodes Castopod (réutilisés pour le JSON-LD et l'affichage)
$castopodEpisodes = [];
if (defined('CASTOPOD_ENABLED') && CASTOPOD_ENABLED && defined('CASTOPOD_URL') && !empty(CASTOPOD_URL)) {
    $castopodEpisodes = getCastopodEpisodes();
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="<?php echo generateCSRFToken(); ?>">
    <title><?php echo SITE_NAME; ?></title>
    <meta name="description" content="<?php echo htmlspecialchars(SITE_DESCRIPTION); ?>">
    <link rel="canonical" href="<?php echo getBaseUrl() . '/'; ?>">
    <link rel="stylesheet" href="css/styles.css?v=<?php echo filemtime('css/styles.css'); ?>">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css">
    <link rel="stylesheet" href="css/mastodon-timeline.min.css?v=<?php echo filemtime('css/mastodon-timeline.min.css'); ?>">
    <?php if (defined('WORDPRESS_ENABLED') && WORDPRESS_ENABLED): ?>
    <link rel="stylesheet" href="css/wordpress-posts.css?v=<?php echo filemtime('css/wordpress-posts.css'); ?>">
    <?php endif; ?>
    <?php if (defined('CASTOPOD_ENABLED') && CASTOPOD_ENABLED): ?>
    <link rel="stylesheet" href="css/castopod-podcasts.css?v=<?php echo filemtime('css/castopod-podcasts.css'); ?>">
    <?php endif; ?>
    <?php if (defined('FUNKWHALE_ENABLED') && FUNKWHALE_ENABLED): ?>
    <link rel="stylesheet" href="css/funkwhale-tracks.css?v=<?php echo filemtime('css/funkwhale-tracks.css'); ?>">
    <?php endif; ?>
    <link rel="stylesheet" href="css/grid-layout.css?v=<?php echo filemtime('css/grid-layout.css'); ?>">

    <!-- Favicons -->
    <link rel="apple-touch-icon" sizes="180x180" href="img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="img/favicon-16x16.png">
    <link rel="manifest" href="site.webmanifest">
    <link rel="icon" type="image/x-icon" href="img/favicon.ico">
    <meta name="theme-color" content="#FF0000">

    <!-- PWA Meta Tags -->
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <meta name="apple-mobile-web-app-title" content="<?php echo SITE_NAME; ?>">
    <meta name="application-name" content="<?php echo SITE_NAME; ?>">
    <meta name="msapplication-TileColor" content="#FF0000">
    <meta name="msapplication-config" content="browserconfig.xml">

    <!-- Open Graph Meta Tags -->
    <meta property="og:title" content="<?php echo SITE_NAME; ?>">
    <meta property="og:description" content="<?php echo htmlspecialchars(SITE_DESCRIPTION); ?>">
    <meta property="og:image" content="<?php echo getBaseUrl() . '/img/logo.png'; ?>">
    <meta property="og:url" content="<?php echo getBaseUrl() . '/'; ?>">
    <meta property="og:type" content="website">
    <meta property="og:site_name" content="<?php echo SITE_NAME; ?>">
    <meta property="og:locale" content="fr_FR">

    <!-- Twitter Card Meta Tags -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="<?php echo SITE_NAME; ?>">
    <meta name="twitter:description" content="<?php echo htmlspecialchars(SITE_DESCRIPTION); ?>">
    <meta name="twitter:image" content="<?php echo getBaseUrl() . '/img/logo.png'; ?>">

    <!-- Données structurées JSON-LD pour le site web -->
    <?php
    $websiteJsonLd = generateWebSiteJsonLd();
    outputJsonLd($websiteJsonLd);

    // Générer les fils d'Ariane pour la page d'accueil
    $breadcrumbs = [
        ['name' => 'Accueil', 'url' => getBaseUrl()]
    ];
    $breadcrumbJsonLd = generateBreadcrumbJsonLd($breadcrumbs);
    outputJsonLd($breadcrumbJsonLd);

    // Données structurées du podcast (PodcastSeries + PodcastEpisode)
    if (!empty($castopodEpisodes)) {
        outputJsonLd(generatePodcastJsonLd($castopodEpisodes));
    }
    ?>

    <!-- Script pour éviter le flash en mode sombre -->
    <script nonce="<?php echo getCspNonce(); ?>">
        (function() {
            const savedTheme = localStorage.getItem('theme');
            const systemPrefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
            const shouldUseDark = savedTheme === 'dark' || (!savedTheme && systemPrefersDark);

            if (shouldUseDark) {
                document.documentElement.setAttribute('data-theme', 'dark');
            }
        })();
    </script>

    <!-- ------ Script Plausible ------ -->

    <!-- <script defer data-domain="<?php echo $_SERVER['HTTP_HOST'] ?>" src="https://plausible.io/js/script.hash.outbound-links.pageview-props.tagged-events.js"></script> -->
    <!-- <script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script> -->

    <!-- ------ Script Plausible ------ -->

</head>
<body>
    <?php include 'includes/sidebar.php'; ?>
    <!-- Contenu principal -->
    <main class="main-content" id="main-content" role="main">
        <h1 class="sr-only"><?php echo SITE_NAME . ' — ' . SITE_DESCRIPTION; ?></h1>
        <?php include 'includes/header.php'; ?>
        <!-- Hero and Mastodon container -->
        <div class="hero-mastodon-wrapper">
            <div class="timeline-wordpress-container">
                <!-- Hero Banner -->
                <?php include 'includes/hero-section.php'; ?>

                <!-- Section Podcasts Castopod -->
            <?php if (defined('CASTOPOD_ENABLED') && CASTOPOD_ENABLED && defined('CASTOPOD_URL') && !empty(CASTOPOD_URL)): ?>
                <div class="castopod-section">
                    <div class="castopod-container">
                        <div class="castopod-header">
                            <i class="fas fa-podcast"></i>
                            Podcasts
                            <?php if (!empty(CASTOPOD_URL)): ?>
                                <a href="<?php echo htmlspecialchars(CASTOPOD_URL); ?>"
                                target="_blank"
                                rel="noopener noreferrer"
                                title="Visiter <?php echo htmlspecialchars(parse_url(CASTOPOD_URL, PHP_URL_HOST)); ?>">
                                    <?php echo htmlspecialchars(parse_url(CASTOPOD_URL, PHP_URL_HOST)); ?>
                                </a>
                            <?php endif; ?>
                        </div>
                        <div class="castopod-episodes-list">
                            <?php
                            // $castopodEpisodes a été récupéré en début de page (JSON-LD + affichage)
                            if (empty($castopodEpisodes)) {
                                echo '<div class="castopod-no-episodes">Aucun épisode disponible</div>';
                            } else {
                                foreach ($castopodEpisodes as $index => $episode):
                            ?>
                            <div class="castopod-episode-item"
                                 data-episode-id="<?php echo $index; ?>"
                                 data-audio-url="<?php echo htmlspecialchars($episode['audioUrl']); ?>">

                                <button class="castopod-episode-thumb castopod-play-btn"
                                        type="button"
                                        aria-label="Lire <?php echo htmlspecialchars($episode['title']); ?>">
                                    <?php if (!empty($episode['image'])): ?>
                                        <img src="<?php echo htmlspecialchars($episode['image']); ?>"
                                            alt="<?php echo htmlspecialchars($episode['title']); ?>"
                                            loading="lazy">
                                        <div class="castopod-play-icon">
                                            <i class="fas fa-play-circle"></i>
                                        </div>
                                        <div class="castopod-pause-icon">
                                            <i class="fas fa-pause-circle"></i>
                                        </div>
                                    <?php else: ?>
                                        <i class="fas fa-podcast"></i>
                                    <?php endif; ?>
                                </button>

                                <div class="castopod-episode-info">
                                    <h3 class="castopod-episode-title">
                                        <?php echo htmlspecialchars($episode['title']); ?>
                                    </h3>

                                    <?php if (!empty($episode['podcastTitle'])): ?>
                                    <div class="castopod-podcast-badge">
                                        <i class="fas fa-podcast"></i> <?php echo htmlspecialchars($episode['podcastTitle']); ?>
                                    </div>
                                    <?php endif; ?>

                                    <div class="castopod-episode-meta">
                                        <span><i class="far fa-calendar"></i> <?php echo htmlspecialchars($episode['formattedDate']); ?></span>
                                        <?php if (!empty($episode['formattedDuration'])): ?>
                                        <span>•</span>
                                        <span><i class="far fa-clock"></i> <?php echo htmlspecialchars($episode['formattedDuration']); ?></span>
                                        <?php endif; ?>
                                    </div>
                                </div>

                                <a href="<?php echo htmlspecialchars($episode['link']); ?>"
                                   class="castopod-external-link"
                                   target="_blank"
                                   rel="noopener noreferrer"
                                   title="Voir sur Castopod"
                                   aria-label="Voir <?php echo htmlspecialchars($episode['title']); ?> sur Castopod">
                                    <i class="fas fa-external-link-alt"></i>
                                </a>

                                <!-- Élément audio caché -->
                                <audio preload="none">
                                    <source src="<?php echo htmlspecialchars($episode['audioUrl']); ?>" type="audio/mpeg">
                                </audio>
                            </div>
                            <?php
                                endforeach;
                            }
                            ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>

            <!-- Section Morceaux Funkwhale -->
            <?php if (defined('FUNKWHALE_ENABLED') && FUNKWHALE_ENABLED && defined('FUNKWHALE_URL') && !empty(FUNKWHALE_URL)): ?>
                <div class="funkwhale-section">
                    <div class="funkwhale-container">
                        <div class="funkwhale-header">
                            <i class="fas fa-music"></i>
                            Morceaux
                            <?php if (!empty(FUNKWHALE_URL)): ?>
                                <a href="<?php echo htmlspecialchars(FUNKWHALE_URL); ?>"
                                   target="_blank"
                                   rel="noopener noreferrer"
                                   title="Visiter <?php echo htmlspecialchars(parse_url(FUNKWHALE_URL, PHP_URL_HOST)); ?>">
                                    <?php echo htmlspecialchars(parse_url(FUNKWHALE_URL, PHP_URL_HOST)); ?>
                                </a>
                            <?php endif; ?>
                        </div>
                        <div class="funkwhale-tracks-list">
                            <?php
                            $funkwhaleTracks = getFunkwhaleTracks();

                            if (empty($funkwhaleTracks)) {
                                echo '<div class="funkwhale-no-tracks">Aucun morceau disponible</div>';
                            } else {
                                foreach ($funkwhaleTracks as $index => $track):
                            ?>
                            <div class="funkwhale-track-item"
                                 data-track-id="<?php echo $index; ?>"
                                 data-audio-url="<?php echo htmlspecialchars($track['audioUrl']); ?>">

                                <button class="funkwhale-track-thumb funkwhale-play-btn"
                                        type="button"
                                        aria-label="Lire <?php echo htmlspecialchars($track['title']); ?>">
                                    <?php if (!empty($track['cover'])): ?>
                                        <img src="<?php echo htmlspecialchars($track['cover']); ?>"
                                             alt="<?php echo htmlspecialchars($track['album']); ?>"
                                             loading="lazy">
                                        <div class="funkwhale-play-icon">
                                            <i class="fas fa-play-circle"></i>
                                        </div>
                                        <div class="funkwhale-pause-icon">
                                            <i class="fas fa-pause-circle"></i>
                                        </div>
                                    <?php else: ?>
                                        <i class="fas fa-music"></i>
                                    <?php endif; ?>
                                </button>

                                <div class="funkwhale-track-info">
                                    <h3 class="funkwhale-track-title">
                                        <?php echo htmlspecialchars($track['title']); ?>
                                    </h3>

                                    <div class="funkwhale-track-artist">
                                        <i class="fas fa-user"></i> <?php echo htmlspecialchars($track['artist']); ?>
                                    </div>

                                    <div class="funkwhale-track-meta">
                                        <span><i class="fas fa-compact-disc"></i> <?php echo htmlspecialchars($track['album']); ?></span>
                                        <?php if (!empty($track['formattedDuration'])): ?>
                                        <span>•</span>
                                        <span><i class="far fa-clock"></i> <?php echo htmlspecialchars($track['formattedDuration']); ?></span>
                                        <?php endif; ?>
                                    </div>
                                </div>

                                <a href="<?php echo htmlspecialchars($track['link']); ?>"
                                   class="funkwhale-external-link"
                                   target="_blank"
                                   rel="noopener noreferrer"
                                   title="Voir sur Funkwhale"
                                   aria-label="Voir <?php echo htmlspecialchars($track['title']); ?> sur Funkwhale">
                                    <i class="fas fa-external-link-alt"></i>
                                </a>

                                <!-- Élément audio caché -->
                                <audio preload="none">
                                    <source src="<?php echo htmlspecialchars($track['audioUrl']); ?>" type="audio/mpeg">
                                </audio>
                            </div>
                            <?php
                                endforeach;
                            }
                            ?>
                        </div>
                    </div>
                </div>
            <?php endif; ?>

                <!-- Timeline Mastodon -->
                <div class="mastodon-section">
                    <div id="mt-container" class="mt-container">
                        <div class="mt-header">
                            <h2 class="mt-title">Fil d'actualités <i class="fab fa-mastodon"></i></h2>
                        </div>
                        <div class="mt-body" role="feed">
                            <div class="mt-loading-spinner"></div>
                        </div>
                    </div>
                </div>

                <!-- Section Articles WordPress -->
                <?php if (defined('WORDPRESS_ENABLED') && WORDPRESS_ENABLED && !empty(WORDPRESS_URL)): ?>
                <div class="wordpress-section">
                    <div class="wordpress-container">
                        <div class="wordpress-header">
                            <i class="fas fa-newspaper"></i>
                            Articles <?php if (!empty(WORDPRESS_URL)): ?>
                                <a href="<?php echo htmlspecialchars(WORDPRESS_URL); ?>"
                                   target="_blank"
                                   rel="noopener noreferrer"
                                   title="Visiter <?php echo htmlspecialchars(parse_url(WORDPRESS_URL, PHP_URL_HOST)); ?>">
                                    <?php echo htmlspecialchars(parse_url(WORDPRESS_URL, PHP_URL_HOST)); ?>
                                </a>
                            <?php endif; ?>
                        </div>
                        <div class="wordpress-posts-list">
                            <?php
                            $wordpressPosts = getWordPressPosts();

                            if (empty($wordpressPosts)) {
                                echo '<div class="wordpress-no-posts">Aucun article disponible</div>';
                            } else {
                                foreach ($wordpressPosts as $post):
                            ?>
                            <a href="<?php echo htmlspecialchars($post['link']); ?>"
                               class="wordpress-post-item"
                               target="_blank"
                               rel="noopener noreferrer"
                               title="<?php echo htmlspecialchars($post['title']); ?>">

                                <div class="wordpress-post-thumb">
                                    <?php if (!empty($post['featured_image'])): ?>
                                        <img src="<?php echo htmlspecialchars($post['featured_image']); ?>"
                                             alt="<?php echo htmlspecialchars($post['title']); ?>"
                                             loading="lazy">
                                    <?php else: ?>
                                        <i class="fas fa-file-alt"></i>
                                    <?php endif; ?>
                                </div>

                                <div class="wordpress-post-info">
                                    <h3 class="wordpress-post-title">
                                        <?php echo htmlspecialchars($post['title']); ?>
                                    </h3>

                                    <div class="wordpress-post-meta">
                                        <span><?php echo htmlspecialchars($post['date']); ?></span>
                                        <?php if (!empty($post['author'])): ?>
                                        <span>•</span>
                                        <span><?php echo htmlspecialchars($post['author']); ?></span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </a>
                            <?php
                                endforeach;
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>


        <!-- Hashtags en ligne -->
        <div class="tags-section">
            <?php
            if (defined('POPULAR_TAGS') && !empty(POPULAR_TAGS)):
                foreach (POPULAR_TAGS as $tag):
                    $encodedTag = urlencode('#' . $tag);
            ?>
            <a href="recherche.php?q=<?php echo $encodedTag; ?>" class="tag">#<?php echo htmlspecialchars($tag); ?></a>
            <?php
                endforeach;
            endif;
            ?>
        </div>

        <!-- Séparateur stylisé -->
        <hr class="section-divider">


        <!-- Section Shorts -->
        <section class="video-section" aria-labelledby="shorts-heading">
            <div class="section-header">
                <div class="section-logo">
                    <img src="img/logo.png" alt="" aria-hidden="true">
                </div>
                <h2 id="shorts-heading" class="section-title">Shorts</h2>
            </div>

            <div class="carousel">
                <div class="carousel-container">
                    <?php
                    // Récupérer les shorts depuis l'API PeerTube
                    $shorts = getShorts();

                    // Traiter le cas où aucun short n'est trouvé
                    if (empty($shorts)) {
                        echo '<div class="no-results">Aucun short disponible pour le moment</div>';
                    } else {
                        foreach ($shorts as $video):
                    ?>
                    <div class="carousel-item">
                        <article class="video-card short-card" data-video-id="<?php echo $video['id']; ?>">
                            <div class="video-thumbnail short-thumbnail">
                                <img src="<?php echo $video['thumbnail']; ?>" alt="Miniature de la vidéo: <?php echo htmlspecialchars($video['title']); ?>">
                                <div class="video-duration" aria-label="Durée: <?php echo formatDuration($video['duration']); ?>">
                                    <?php echo formatDuration($video['duration']); ?>
                                </div>
                                <div class="short-play-overlay" aria-hidden="true">
                                    <i class="fas fa-play-circle"></i>
                                </div>
                                <div class="short-title-overlay">
                                    <?php echo htmlspecialchars($video['title']); ?>
                                </div>
                            </div>
                        </article>
                    </div>
                    <?php
                        endforeach;
                    }
                    ?>
                </div>

                <?php if (!empty($shorts) && count($shorts) > 1): ?>
                <div class="carousel-controls">
                    <?php for ($i = 0; $i < count($shorts); $i++): ?>
                    <div class="carousel-dot <?php echo $i === 0 ? 'active' : ''; ?>"></div>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>
            </div>
        </section>

        <!-- Séparateur stylisé -->
        <hr class="section-divider">

        <!-- Section Dernières vidéos -->
        <section class="video-section" aria-labelledby="recent-videos-heading">
            <header class="section-header">
                <div class="section-logo">
                    <img src="img/logo.png" alt="" aria-hidden="true">
                </div>
                <h2 id="recent-videos-heading" class="section-title">Dernières vidéos</h2>
            </header>

            <div class="video-grid">
                <?php
                // Récupérer les vidéos récentes depuis l'API PeerTube
                $recentVideos = getRecentVideos();

                // Traiter le cas où aucune vidéo n'est trouvée
                if (empty($recentVideos)) {
                    echo '<div class="no-results">Aucune vidéo disponible pour le moment</div>';
                } else {
                    foreach ($recentVideos as $video):
                ?>
                <article class="video-card" data-video-id="<?php echo $video['id']; ?>">
                    <div class="video-thumbnail">
                        <img src="<?php echo $video['thumbnail']; ?>" alt="Miniature de la vidéo: <?php echo htmlspecialchars($video['title']); ?>">
                        <div class="video-play-icon" aria-hidden="true">
                            <i class="fas fa-play-circle"></i>
                        </div>
                        <div class="video-duration" aria-label="Durée: <?php echo formatDuration($video['duration']); ?>">
                            <?php echo formatDuration($video['duration']); ?>
                        </div>
                    </div>
                    <div class="video-info">
                        <h3 class="video-title"><?php echo htmlspecialchars($video['title']); ?></h3>
                        <div class="video-channel">
                            <?php if (strpos($video['channelAvatar'], 'default-avatar.png') !== false || empty($video['channelAvatar'])): ?>
                                <div class="channel-avatar-placeholder">
                                    <i class="fas fa-user-circle"></i>
                                </div>
                            <?php else: ?>
                                <img src="<?php echo $video['channelAvatar']; ?>" alt="<?php echo $video['channel']; ?>" class="channel-avatar">
                            <?php endif; ?>
                            <span class="channel-name"><?php echo $video['channel']; ?></span>
                        </div>
                        <div class="video-metadata">
                            <?php if (defined('SHOW_VIDEO_VIEWS') && SHOW_VIDEO_VIEWS): ?>
                            <span class="video-views"><i class="fas fa-eye"></i> <?php echo formatViewCount($video['views']); ?> vues</span>
                            <?php endif; ?>
                            <span class="video-date"><i class="far fa-calendar-alt"></i> <?php echo formatDate($video['date']); ?></span>
                        </div>
                    </div>
                </article>
                <?php
                    endforeach;
                }
                ?>
            </div>

            <button class="view-more">Voir plus</button>
        </section>

        <!-- Séparateur stylisé -->
        <hr class="section-divider">

        <!-- Section Tendances -->
        <section class="video-section" aria-labelledby="trending-videos-heading">
            <header class="section-header">
                <div class="section-logo">
                    <img src="img/logo.png" alt="" aria-hidden="true">
                </div>
                <h2 id="trending-videos-heading" class="section-title">Tendances</h2>
            </header>

            <div class="video-grid">
                <?php
                // Récupérer les vidéos tendances depuis l'API PeerTube
                $trendingVideos = getTrendingVideos();

                // Traiter le cas où aucune vidéo n'est trouvée
                if (empty($trendingVideos)) {
                    echo '<div class="no-results">Aucune vidéo disponible pour le moment</div>';
                } else {
                    foreach ($trendingVideos as $video):
                ?>
                <article class="video-card" data-video-id="<?php echo $video['id']; ?>">
                    <div class="video-thumbnail">
                        <img src="<?php echo htmlspecialchars($video['thumbnail']); ?>" alt="<?php echo htmlspecialchars($video['title']); ?>">
                        <div class="video-play-icon">
                            <i class="fas fa-play-circle"></i>
                        </div>
                        <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 if (strpos($video['channelAvatar'], 'default-avatar.png') !== false || empty($video['channelAvatar'])): ?>
                                <div class="channel-avatar-placeholder">
                                    <i class="fas fa-user-circle"></i>
                                </div>
                            <?php else: ?>
                                <img src="<?php echo $video['channelAvatar']; ?>" alt="<?php echo $video['channel']; ?>" class="channel-avatar">
                            <?php endif; ?>
                            <span class="channel-name"><?php echo $video['channel']; ?></span>
                        </div>
                        <div class="video-metadata">
                            <?php if (defined('SHOW_VIDEO_VIEWS') && SHOW_VIDEO_VIEWS): ?>
                            <span class="video-views"><i class="fas fa-eye"></i> <?php echo formatViewCount($video['views']); ?> vues</span>
                            <?php endif; ?>
                            <span class="video-date"><i class="far fa-calendar-alt"></i> <?php echo formatDate($video['date']); ?></span>
                        </div>
                    </div>
                </article>
                <?php
                    endforeach;
                }
                ?>
            </div>

            <button class="view-more">Voir plus</button>
        </section>

        <!-- Séparateur stylisé -->
        <hr class="section-divider">

        <!-- Sections par catégorie -->
        <?php
        // Récupérer les catégories avec leurs vidéos
        $displayCategories = getDisplayCategories();

        // Afficher chaque catégorie qui a des vidéos
        foreach ($displayCategories as $category):
            if (!empty($category['videos'])):
        ?>
        <!-- Section Catégorie: <?php echo $category['name']; ?> -->
        <section class="video-section" data-category-id="<?php echo $category['id']; ?>" aria-labelledby="category-heading-<?php echo $category['id']; ?>">
            <header class="section-header">
                <div class="section-logo">
                    <img src="img/logo.png" alt="" aria-hidden="true">
                </div>
                <h2 id="category-heading-<?php echo $category['id']; ?>" class="section-title"><?php echo htmlspecialchars($category['name']); ?></h2>
            </header>

            <div class="video-grid">
                <?php foreach ($category['videos'] as $video): ?>
                <article class="video-card" data-video-id="<?php echo $video['id']; ?>">
                    <div class="video-thumbnail">
                        <img src="<?php echo htmlspecialchars($video['thumbnail']); ?>" alt="<?php echo htmlspecialchars($video['title']); ?>">
                        <div class="video-play-icon">
                            <i class="fas fa-play-circle"></i>
                        </div>
                        <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 if (strpos($video['channelAvatar'], 'default-avatar.png') !== false || empty($video['channelAvatar'])): ?>
                                <div class="channel-avatar-placeholder">
                                    <i class="fas fa-user-circle"></i>
                                </div>
                            <?php else: ?>
                                <img src="<?php echo $video['channelAvatar']; ?>" alt="<?php echo $video['channel']; ?>" class="channel-avatar">
                            <?php endif; ?>
                            <span class="channel-name"><?php echo $video['channel']; ?></span>
                        </div>
                        <div class="video-metadata">
                            <?php if (defined('SHOW_VIDEO_VIEWS') && SHOW_VIDEO_VIEWS): ?>
                            <span class="video-views"><i class="fas fa-eye"></i> <?php echo formatViewCount($video['views']); ?> vues</span>
                            <?php endif; ?>
                            <span class="video-date"><i class="far fa-calendar-alt"></i> <?php echo formatDate($video['date']); ?></span>
                        </div>
                    </div>
                </article>
                <?php endforeach; ?>
            </div>

            <button class="view-more">Voir plus</button>
        </section>

        <!-- Séparateur stylisé -->
        <hr class="section-divider">

        <?php
            endif;
        endforeach;
        ?>

        <!-- Section Flexbox pour Informations et Tendances Hashtags -->
        <section class="info-tags-container">
            <?php if (defined('MOVEMENT_DESCRIPTION') && !empty(MOVEMENT_DESCRIPTION)): ?>
            <!-- Section Informations -->
            <article class="info-section">
                <h2 class="section-title centered"><?php echo defined('MOVEMENT_TITLE') && !empty(MOVEMENT_TITLE) ? MOVEMENT_TITLE : 'À propos'; ?></h2>

                <p class="info-description"><?php echo MOVEMENT_DESCRIPTION; ?></p>

                <?php if (defined('MOVEMENT_DESCRIPTION_2') && !empty(MOVEMENT_DESCRIPTION_2)): ?>
                <p class="info-description"><?php echo MOVEMENT_DESCRIPTION_2; ?></p>
                <?php endif; ?>

                <?php if (defined('MOVEMENT_IMAGE') && !empty(MOVEMENT_IMAGE)): ?>
                <figure class="movement-figure">
                    <img src="<?php echo MOVEMENT_IMAGE; ?>" alt="<?php echo defined('MOVEMENT_IMAGE_ALT') ? MOVEMENT_IMAGE_ALT : ''; ?>" class="info-image">
                    <?php if (defined('MOVEMENT_CAPTION') && !empty(MOVEMENT_CAPTION)): ?>
                    <figcaption class="movement-caption">
                        <?php echo MOVEMENT_CAPTION; ?>
                    </figcaption>
                    <?php endif; ?>
                </figure>
                <?php endif; ?>
            </article>
            <?php endif; ?>

            <!-- Section Tendances Hashtags -->
            <aside class="tags-section-container" aria-labelledby="hashtags-heading">
                <h2 id="hashtags-heading" class="section-title centered">Tendances</h2>

                <div class="tags-section">
                    <?php
                    if (defined('POPULAR_TAGS') && !empty(POPULAR_TAGS)):
                        foreach (POPULAR_TAGS as $tag):
                            $encodedTag = urlencode('#' . $tag);
                    ?>
                    <a href="recherche.php?q=<?php echo $encodedTag; ?>" class="tag">#<?php echo htmlspecialchars($tag); ?></a>
                    <?php
                        endforeach;
                    endif;
                    ?>
                </div>
            </aside>
        </section>
    </main>
    <?php include 'includes/footer.php'; ?>
    <?php include 'includes/mobile-menu.php'; ?>
    <script src="js/main.js"></script>
    <script src="js/pleroma-adapter.js"></script>
    <script src="js/mastodon-timeline.umd.js"></script>
    <script src="js/mastodon-config.php?v=<?php echo md5(MASTODON_INSTANCE_URL . MASTODON_DATE_FORMAT . MASTODON_BTN_SEE_MORE . MASTODON_BTN_RELOAD . MASTODON_MAX_POST_FETCH . MASTODON_MAX_POST_SHOW); ?>"></script>

    <!-- PWA : enregistrement du Service Worker + modal de mise à jour -->
    <script src="js/pwa-update.js?v=<?php echo filemtime('js/pwa-update.js'); ?>"></script>

    <!-- PWA : bouton d'installation -->
    <script nonce="<?php echo getCspNonce(); ?>">
        // Gestion de l'installation PWA
        let deferredPrompt;
        const installButton = document.getElementById('install-pwa');

        window.addEventListener('beforeinstallprompt', function(e) {
            e.preventDefault();
            deferredPrompt = e;

            // Afficher le bouton d'installation s'il existe
            if (installButton) {
                installButton.style.display = 'block';
                installButton.addEventListener('click', function() {
                    deferredPrompt.prompt();
                    deferredPrompt.userChoice.then(function(choiceResult) {
                        if (choiceResult.outcome === 'accepted') {
                            console.log('PWA installée');
                        }
                        deferredPrompt = null;
                        installButton.style.display = 'none';
                    });
                });
            }
        });

        // Masquer le bouton après installation
        window.addEventListener('appinstalled', function() {
            console.log('PWA installée avec succès');
            if (installButton) {
                installButton.style.display = 'none';
            }
        });
    </script>

    <?php if ((defined('FUNKWHALE_ENABLED') && FUNKWHALE_ENABLED) || (defined('CASTOPOD_ENABLED') && CASTOPOD_ENABLED)): ?>
    <!-- Lecteur audio unifié pour Funkwhale et Castopod -->
    <script src="js/audio-player.js?v=<?php echo @filemtime('js/audio-player.js') ?: time(); ?>"></script>
    <?php endif; ?>
</body>
</html>
