<?xml version="1.0" encoding="UTF-8"?>
<?php require_once __DIR__ . '/includes/bootstrap.php'; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
$pages = [
    SITE_URL . '/index.php',
    SITE_URL . '/posts.php',
    SITE_URL . '/about.php',
    SITE_URL . '/contact.php',
    SITE_URL . '/privacy-policy.php',
];

foreach (all_categories() as $category) {
    $pages[] = SITE_URL . '/posts.php?category=' . urlencode((string) ($category['slug'] ?? ''));
}

$recentPosts = db()->query('SELECT slug FROM posts WHERE status = "published" ORDER BY published_at DESC LIMIT 200')->fetchAll();

foreach ($pages as $page) {
    echo "  <url><loc>" . e($page) . "</loc></url>\n";
}

foreach ($recentPosts as $post) {
    $slug = (string) ($post['slug'] ?? '');
    if ($slug === '') {
        continue;
    }
    echo "  <url><loc>" . e(SITE_URL . '/post.php?slug=' . urlencode($slug)) . "</loc></url>\n";
}
?>
</urlset>
