<?php
// sitemap.php — sitemap XML généré depuis la base (toujours à jour).
require_once __DIR__ . '/inc/config.php';
require_once __DIR__ . '/inc/data.php';
header('Content-Type: application/xml; charset=utf-8');

$urls = [];

// Pages fixes (priorité, fréquence)
$fixes = [
  ['', '1.0', 'weekly'],
  ['le-tuina.php', '0.8', 'monthly'],
  ['les-soins.php', '0.8', 'monthly'],
  ['le-praticien.php', '0.7', 'monthly'],
  ['temoignages/', '0.9', 'weekly'],
  ['formations.php', '0.7', 'monthly'],
  ['contact.php', '0.6', 'monthly'],
  ['avant-apres.php', '0.6', 'monthly'],
  ['conseils-dietetiques-qigong.php', '0.5', 'monthly'],
  ['proposer-temoignage.php', '0.4', 'yearly'],
  ['mentions-legales.php', '0.2', 'yearly'],
  ['deontologie.php', '0.3', 'yearly'],
  ['rgpd.php', '0.2', 'yearly'],
];
foreach ($fixes as $f) {
    $urls[] = ['loc'=>SITE_URL.'/'.$f[0], 'priority'=>$f[1], 'changefreq'=>$f[2]];
}

// Pages des piliers (sans tuina : couvert par le-tuina.php)
foreach (['acupuncture','pharmacopee','qigong','dietetique'] as $pil) {
    $urls[] = ['loc'=>SITE_URL.'/piliers/'.$pil.'/', 'priority'=>'0.5', 'changefreq'=>'monthly'];
}

// Pages des spécialités du cabinet
foreach (['zhenggu-tuina','qigong-tuina','zangfu-tuina'] as $spe) {
    $urls[] = ['loc'=>SITE_URL.'/specialites/'.$spe.'/', 'priority'=>'0.7', 'changefreq'=>'monthly'];
}

// Types de soins (pages filtrées)
foreach (types_de_soins() as $t) {
    $urls[] = ['loc'=>url_type($t['slug']), 'priority'=>'0.6', 'changefreq'=>'monthly'];
}

// Pages Liens et Articles (liste)
$urls[] = ['loc'=>SITE_URL.'/liens.php', 'priority'=>'0.4', 'changefreq'=>'yearly'];
$urls[] = ['loc'=>SITE_URL.'/articles.php', 'priority'=>'0.7', 'changefreq'=>'weekly'];

// Articles publiés
foreach (articles_liste() as $art) {
    $urls[] = [
        'loc'=>url_article($art['slug']),
        'priority'=>'0.6',
        'changefreq'=>'monthly',
        'lastmod'=>!empty($art['modifie_le']) ? substr($art['modifie_le'],0,10) : null,
    ];
}

// Témoignages publiés
$rows = db()->query("SELECT slug, date_pub FROM temoignages WHERE statut='publie' AND est_formation=0 ORDER BY date_pub DESC")->fetchAll();
foreach ($rows as $r) {
    $urls[] = [
        'loc'=>url_temoignage($r['slug']),
        'priority'=>'0.6',
        'changefreq'=>'yearly',
        'lastmod'=>$r['date_pub'] ? substr($r['date_pub'],0,10) : null,
    ];
}

echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
foreach ($urls as $u) {
    echo "  <url>\n";
    echo "    <loc>".htmlspecialchars($u['loc'])."</loc>\n";
    if (!empty($u['lastmod'])) echo "    <lastmod>".$u['lastmod']."</lastmod>\n";
    echo "    <changefreq>".$u['changefreq']."</changefreq>\n";
    echo "    <priority>".$u['priority']."</priority>\n";
    echo "  </url>\n";
}
echo '</urlset>';
