<?php
// Outfits Check AI - Dynamic XML Sitemap

require_once __DIR__ . '/includes/config.php';

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

$db = Database::getInstance();
$posts = $db->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY updated_at DESC")->fetchAll();
$categories = $db->query("SELECT slug FROM blog_categories ORDER BY slug ASC")->fetchAll();

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?= BASE_URL ?></loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= BASE_URL ?>pricing.php</loc>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?= BASE_URL ?>blog.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    <url>
        <loc><?= BASE_URL ?>faq.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    
    <?php foreach ($categories as $cat): ?>
    <url>
        <loc><?= BASE_URL ?>blog-category.php?slug=<?= htmlspecialchars($cat['slug']) ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>

    <?php foreach ($posts as $p): ?>
    <url>
        <loc><?= BASE_URL ?>blog-post.php?slug=<?= htmlspecialchars($p['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
</urlset>
