<?php
require_once __DIR__ . '/config.php';
header('Content-Type: application/xml; charset=utf-8');

$newsFile = __DIR__ . '/content/news.json';
$news     = file_exists($newsFile) ? (json_decode(file_get_contents($newsFile), true) ?? []) : [];
$now      = time();

// Nur veröffentlichte News
$news = array_filter($news, fn($n) => !empty($n['publishedAt']) && strtotime($n['publishedAt']) <= $now);

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://megasaves.de/news</loc>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
<?php foreach ($news as $n):
  $slug    = htmlspecialchars($n['slug'] ?? '');
  $lastmod = date('Y-m-d', strtotime($n['publishedAt']));
  if (!$slug) continue;
?>
  <url>
    <loc>https://megasaves.de/news/<?= $slug ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
<?php endforeach; ?>
</urlset>
