<?php
header('Content-Type: application/xml; charset=utf-8');
include 'inc/db.php';

$base = 'https://ddseries.my.id';

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc><?= $base ?>/</loc>
    <changefreq>daily</changefreq>
    <priority>1.00</priority>
  </url>
  <url>
    <loc><?= $base ?>/category?status=Ongoing</loc>
    <changefreq>daily</changefreq>
    <priority>0.70</priority>
  </url>
  <url>
    <loc><?= $base ?>/category?status=Complete</loc>
    <changefreq>daily</changefreq>
    <priority>0.70</priority>
  </url>
  <url>
    <loc><?= $base ?>/schedule</loc>
    <changefreq>daily</changefreq>
    <priority>0.60</priority>
  </url>
  <url>
    <loc><?= $base ?>/jadwal_tv</loc>
    <changefreq>weekly</changefreq>
    <priority>0.50</priority>
  </url>
<?php
$query = $conn->query("
    SELECT slug, update_date, created_at
    FROM series
    WHERE slug IS NOT NULL AND slug != ''
    ORDER BY update_date DESC
");
while ($row = $query->fetch_assoc()):
    $lastmod = $row['update_date'] ?: $row['created_at'];
    $lastmod = $lastmod ? date('c', strtotime($lastmod)) : null;
?>
  <url>
    <loc><?= $base ?>/detail/<?= htmlspecialchars($row['slug']) ?></loc>
<?php if ($lastmod): ?>
    <lastmod><?= $lastmod ?></lastmod>
<?php endif; ?>
    <changefreq>weekly</changefreq>
    <priority>0.80</priority>
  </url>
<?php endwhile; ?>
</urlset>
