<?php
/**
 * Dynamische Sitemap – wird von Google/Bing gecrawlt
 */
header('Content-Type: application/xml; charset=UTF-8');

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

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

// Sektionen laden (sichtbare als URL-Fragmente)
$sekt_raw = @file_get_contents($data_dir . 'sektionen.json');
$sekt     = $sekt_raw ? (json_decode($sekt_raw, true) ?? []) : [];
?>
<?= '<?xml version="1.0" encoding="UTF-8"?>' ?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- Startseite (enthält alle Sektionen als Anker) -->
  <url>
    <loc><?= htmlspecialchars($base . '/') ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>

<?php foreach ($sekt as $slug => $sec): ?>
<?php if (empty($sec['visible'])) continue; ?>
<?php if (in_array($slug, ['aktuelles'])) continue; ?>
  <url>
    <loc><?= htmlspecialchars($base . '/#' . $slug) ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
<?php endforeach; ?>

  <url>
    <loc><?= htmlspecialchars($base . '/#impressum') ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.3</priority>
  </url>
  <url>
    <loc><?= htmlspecialchars($base . '/#datenschutz') ?></loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.3</priority>
  </url>

</urlset>
