<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config.php';
require_once 'db.php';

$base = 'https://www.gwennhadeck.fr';
$today = date('Y-m-d');

// Récupérer les événements à venir pour les inclure dans le sitemap
$events = [];
if ($pdo) {
    try {
        $stmt = $pdo->query("SELECT id, date_event, updated_at FROM evenements WHERE visible = 1 AND date_event >= CURDATE() ORDER BY date_event ASC LIMIT 50");
        $events = $stmt->fetchAll();
    } catch (PDOException $e) {}
}
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

  <!-- Page d'accueil -->
  <url>
    <loc><?= $base ?>/</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>

  <!-- Inscription événements -->
  <url>
    <loc><?= $base ?>/inscription.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>

  <!-- Événements dynamiques -->
  <?php foreach ($events as $ev): ?>
  <url>
    <loc><?= $base ?>/inscription.php?id=<?= $ev['id'] ?></loc>
    <lastmod><?= $ev['updated_at'] ? date('Y-m-d', strtotime($ev['updated_at'])) : $ev['date_event'] ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
  <?php endforeach; ?>

</urlset>
