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

$data_dir = __DIR__ . '/admin/data/';
$sett     = json_decode(@file_get_contents($data_dir . 'einstellungen.json'), true) ?? [];
$sekt     = json_decode(@file_get_contents($data_dir . 'sektionen.json'),     true) ?? [];
$seiten   = json_decode(@file_get_contents($data_dir . 'seiten.json'),        true) ?? [];

$base  = rtrim($sett['site_url'] ?? 'https://' . ($_SERVER['HTTP_HOST'] ?? ''), '/');
$today = date('Y-m-d');

/* Slugs die eigene PHP-Seiten haben (URL aus sektionen.json url-Feld) */
$extra_pages = [];
foreach ($sekt as $slug => $sec) {
    if (empty($sec['visible'])) continue;
    if (!empty($sec['url']) && substr($sec['url'], -4) === '.php') {
        $extra_pages[] = $sec['url'];
    }
}

/* Slugs die NICHT als eigene URL in der Sitemap auftauchen sollen */
$no_sitemap = ['service-cards','trust-bar','ablauf','aktuelles','downloads'];
?>
<?= '<?xml version="1.0" encoding="UTF-8"?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <url>
    <loc><?= htmlspecialchars($base . '/') ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>

<?php foreach ($extra_pages as $page): ?>
  <url>
    <loc><?= htmlspecialchars($base . '/' . $page) ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
<?php endforeach; ?>

</urlset>
