<?php
header('Content-Type: application/xml; charset=UTF-8');

$allCities = json_decode(file_get_contents(__DIR__ . '/data/cities_france.json'), true);
$departments = json_decode(file_get_contents(__DIR__ . '/data/departments_france.json'), true);
$entreprises = json_decode(file_get_contents(__DIR__ . '/data/entreprises.json'), true);

$mancheId = null;
foreach ($departments as $dept) {
    if ($dept['code'] === '50') { $mancheId = $dept['id']; break; }
}

$mancheCities = [];
foreach ($allCities as $city) {
    if ($city['department_id'] === $mancheId) {
        $mancheCities[] = $city;
    }
}

$baseUrl = 'https://panneau-solaire-cherbourg.fr';
$today = date('Y-m-d');

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?= $baseUrl ?>/</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/villes</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/installateurs</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/aides-subventions</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/guide-solaire</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/devis</loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php foreach ($mancheCities as $city) : ?>
    <url>
        <loc><?= $baseUrl ?>/panneau-solaire-<?= htmlspecialchars($city['slug']) ?></loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    <?php foreach ($entreprises as $ent) : ?>
    <url>
        <loc><?= $baseUrl ?>/installateur/<?= htmlspecialchars($ent['slug']) ?></loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
</urlset>