<?php
// Auto-generated sitemap.xml
require __DIR__ . '/blog/config.php';

header('Content-Type: application/xml; charset=utf-8');

$db = getDB();
$posts = $db->query("SELECT slug, created_at FROM posts WHERE published = 1 ORDER BY created_at DESC");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc><?= SITE_URL ?></loc>
    <priority>1.0</priority>
    <changefreq>weekly</changefreq>
  </url>
  <url>
    <loc><?= BLOG_URL ?></loc>
    <priority>0.9</priority>
    <changefreq>daily</changefreq>
  </url>
<?php while ($post = $posts->fetch_assoc()): ?>
  <url>
    <loc><?= BLOG_URL . '/' . htmlspecialchars($post['slug']) ?></loc>
    <priority>0.8</priority>
    <lastmod><?= date('c', strtotime($post['created_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
  </url>
<?php endwhile; ?>
</urlset>
