<?php
header("Content-Type: application/xml; charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- Pages principales -->
  <url>
    <loc>https://grs-review.org/articles.php</loc>
    <lastmod><?= date('Y-m-d') ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.9</priority>
  </url>

  <url>
    <loc>https://grs-review.org/recherche.php</loc>
    <lastmod><?= date('Y-m-d') ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>

  <!-- Pages principales FR -->
  <url>
    <loc>https://grs-review.org/lang_fr/articles.php</loc>
    <lastmod><?= date('Y-m-d') ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.9</priority>
  </url>

  <url>
    <loc>https://grs-review.org/lang_fr/recherche.php</loc>
    <lastmod><?= date('Y-m-d') ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>

<?php
require 'config.php';

// Récupérer tous les articles publiés
$query = $pdo->query("SELECT id, titre, date_publication FROM articles WHERE statut='publie' ORDER BY date_publication DESC");
$articles = $query->fetchAll(PDO::FETCH_ASSOC);

foreach ($articles as $article):
    $year = date('Y', strtotime($article['date_publication']));
    $month = date('m', strtotime($article['date_publication']));
    $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $article['titre'])));

    // EN
    $url_seo_en = "https://grs-review.org/articles/{$year}/{$month}/" . rawurlencode($slug);
    $detail_en = "https://grs-review.org/article_detail.php?id=" . intval($article['id']);

    // FR
    $url_seo_fr = "https://grs-review.org/lang_fr/articles/{$year}/{$month}/" . rawurlencode($slug);
    $detail_fr = "https://grs-review.org/lang_fr/article_detail.php?id=" . intval($article['id']);

    // Générer les URLs
    $all_urls = [
        ['url' => $url_seo_en, 'priority' => 0.8],
        ['url' => $detail_en, 'priority' => 0.8],
        ['url' => $url_seo_fr, 'priority' => 0.8],
        ['url' => $detail_fr, 'priority' => 0.8],
    ];

    foreach ($all_urls as $u):
?>
  <url>
    <loc><?= $u['url'] ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($article['date_publication'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority><?= $u['priority'] ?></priority>
  </url>
<?php
    endforeach;

endforeach;
?>
</urlset>