add_shortcode('agenda_auto', function() {
    $dir = '/var/www/html/wp-content/uploads/';
    $files = glob($dir . "*.ics");

    if (!$files) return "Aucun fichier calendrier trouvé.";

    // Trouver le fichier le plus récent
    usort($files, function($a, $b) {
        return filemtime($b) - filemtime($a);
    });

    $url_fichier = "https://www.nuage.ooo/wp-content/uploads/" . basename($files[0]);

    // On génère le calendrier en Javascript (FullCalendar)
    // C'est le navigateur qui travaille, pas le Raspberry !
    return '
    <script src="https://cdn.jsdelivr.net/npm/fullcalendar@6.1.8/index.global.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@fullcalendar/icalendar@6.1.8/index.global.min.js"></script>
    <div id="calendar-auto" style="height: 600px; background: white; padding: 10px; border: 1px solid #ddd;"></div>
    <script>
      document.addEventListener("DOMContentLoaded", function() {
        var calendar = new FullCalendar.Calendar(document.getElementById("calendar-auto"), {
          initialView: "dayGridMonth",
          locale: "fr",
          headerToolbar: { left: "prev,next today", center: "title", right: "dayGridMonth,listMonth" },
          events: { url: "' . $url_fichier . '?t=' . time() . '", format: "ics" }
        });
        calendar.render();
      });
    </script>';
});