<?php
/**
 * /sitemap.php — SITEMAP XML DYNAMIQUE
 *
 * Génère un sitemap XML standard à partir des helpers du site.
 * Accessible via /sitemap.xml grâce à une règle .htaccess :
 *   RewriteRule ^sitemap\.xml$ /sitemap.php [L]
 *
 * Inclut :
 *  - Pages statiques principales (home, piliers, légales)
 *  - Tous les clubs (par région et par fiche)
 *  - Toutes les raquettes (catégories + fiches détaillées)
 *  - Toutes les techniques (catégories + fiches)
 */

require_once __DIR__ . '/config.php';

header('Content-Type: application/xml; charset=utf-8');

// Helpers
function sitemap_url(string $loc, ?string $lastmod = null, string $changefreq = 'weekly', string $priority = '0.5'): string
{
    $xml = "  <url>\n";
    $xml .= "    <loc>" . htmlspecialchars($loc, ENT_QUOTES | ENT_XML1, 'UTF-8') . "</loc>\n";
    if ($lastmod) {
        $xml .= "    <lastmod>" . htmlspecialchars($lastmod, ENT_QUOTES | ENT_XML1, 'UTF-8') . "</lastmod>\n";
    }
    $xml .= "    <changefreq>{$changefreq}</changefreq>\n";
    $xml .= "    <priority>{$priority}</priority>\n";
    $xml .= "  </url>\n";
    return $xml;
}

$today = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// ---------------------------------------------------------------------
// 1. PAGES STATIQUES PRINCIPALES
// ---------------------------------------------------------------------
echo sitemap_url(SITE_URL . '/',                    $today, 'daily',   '1.0');
echo sitemap_url(SITE_URL . '/raquettes/',          $today, 'weekly',  '0.9');
echo sitemap_url(SITE_URL . '/techniques/',         $today, 'weekly',  '0.9');
echo sitemap_url(SITE_URL . '/regles/',             $today, 'monthly', '0.7');
echo sitemap_url(SITE_URL . '/clubs/',              $today, 'weekly',  '0.9');
echo sitemap_url(SITE_URL . '/clubs/belgique/',     $today, 'weekly',  '0.8');
echo sitemap_url(SITE_URL . '/clubs/france/',       $today, 'weekly',  '0.8');
echo sitemap_url(SITE_URL . '/actualites/',         $today, 'daily',   '0.8');
echo sitemap_url(SITE_URL . '/lexique/',            $today, 'monthly', '0.6');
echo sitemap_url(SITE_URL . '/mentions-legales/',   $today, 'yearly',  '0.2');
echo sitemap_url(SITE_URL . '/confidentialite/',    $today, 'yearly',  '0.2');
echo sitemap_url(SITE_URL . '/contact/',            $today, 'yearly',  '0.3');

// ---------------------------------------------------------------------
// 2. CLUBS — Régions + fiches individuelles
// ---------------------------------------------------------------------
$regions = [];
foreach (clubs_all() as $club) {
    $regions[$club['region_slug']] = true;
}
foreach (array_keys($regions) as $region_slug) {
    echo sitemap_url(SITE_URL . region_url($region_slug), $today, 'weekly', '0.7');
}
foreach (clubs_all() as $club) {
    echo sitemap_url(SITE_URL . club_url($club), $today, 'monthly', '0.6');
}

// ---------------------------------------------------------------------
// 3. RAQUETTES — Catégories + fiches
// ---------------------------------------------------------------------
// Pages catégorielles : marques
foreach (rackets_brands() as $brand) {
    echo sitemap_url(SITE_URL . brand_url($brand['slug']), $today, 'weekly', '0.7');
}
// Niveaux
foreach (['debutant', 'intermediaire', 'confirme', 'pro'] as $lvl) {
    echo sitemap_url(SITE_URL . level_url($lvl), $today, 'weekly', '0.7');
}
// Styles
foreach (['controle', 'puissance', 'polyvalente'] as $st) {
    echo sitemap_url(SITE_URL . style_url($st), $today, 'weekly', '0.7');
}
// Fiches individuelles
foreach (rackets_all() as $r) {
    $lastmod = !empty($r['tested_at']) ? date('Y-m-d', strtotime($r['tested_at'])) : $today;
    echo sitemap_url(SITE_URL . racket_url($r), $lastmod, 'monthly', '0.7');
}

// ---------------------------------------------------------------------
// 4. TECHNIQUES — Fiches individuelles
// ---------------------------------------------------------------------
foreach (techniques_all() as $t) {
    $lastmod = !empty($t['updated_at']) ? date('Y-m-d', strtotime($t['updated_at'])) : $today;
    $priority = !empty($t['pillar']) ? '0.8' : '0.6';
    echo sitemap_url(SITE_URL . technique_url($t), $lastmod, 'monthly', $priority);
}

echo '</urlset>' . "\n";
