<?php
// sitemap.xml — Sitemap dynamique généré depuis les datasets
header('Content-Type: application/xml; charset=utf-8');

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

$regions     = json_decode(file_get_contents(__DIR__ . '/data/regions.json'),     true) ?? [];
$departments = json_decode(file_get_contents(__DIR__ . '/data/departments.json'), true) ?? [];
$cities      = json_decode(file_get_contents(__DIR__ . '/data/cities.json'),      true) ?? [];

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

// Static pages
$statics = [
  ['/', '1.0', 'daily'],
  ['/devis', '0.9', 'weekly'],
  ['/extension-de-maison', '0.9', 'weekly'],
];
foreach ($statics as $s) {
  echo "  <url>\n";
  echo "    <loc>" . $base . htmlspecialchars($s[0]) . "</loc>\n";
  echo "    <lastmod>$today</lastmod>\n";
  echo "    <changefreq>{$s[2]}</changefreq>\n";
  echo "    <priority>{$s[1]}</priority>\n";
  echo "  </url>\n";
}

// Regions
foreach ($regions as $r) {
  echo "  <url>\n";
  echo "    <loc>" . $base . "/extension-de-maison/" . htmlspecialchars($r['slug']) . "</loc>\n";
  echo "    <lastmod>$today</lastmod>\n";
  echo "    <changefreq>monthly</changefreq>\n";
  echo "    <priority>0.8</priority>\n";
  echo "  </url>\n";
}

// Departments
foreach ($departments as $d) {
  echo "  <url>\n";
  echo "    <loc>" . $base . "/extension-de-maison/" . htmlspecialchars($d['slug']) . "</loc>\n";
  echo "    <lastmod>$today</lastmod>\n";
  echo "    <changefreq>monthly</changefreq>\n";
  echo "    <priority>0.7</priority>\n";
  echo "  </url>\n";
}

// Cities (limit to 50 000 URLs per sitemap spec — on les inclut tous)
foreach ($cities as $c) {
  echo "  <url>\n";
  echo "    <loc>" . $base . "/extension-de-maison/" . htmlspecialchars($c['slug']) . "</loc>\n";
  echo "    <lastmod>$today</lastmod>\n";
  echo "    <changefreq>monthly</changefreq>\n";
  echo "    <priority>0.6</priority>\n";
  echo "  </url>\n";
}

echo '</urlset>';