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

$pages = [
    ['loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => 'page/about', 'priority' => '0.7', 'changefreq' => 'monthly'],
    ['loc' => 'page/contact', 'priority' => '0.6', 'changefreq' => 'monthly'],
    ['loc' => 'page/privacy', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'page/terms', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'page/dmca', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'page/faq', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => 'blog', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => 'auth/login', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['loc' => 'auth/register', 'priority' => '0.3', 'changefreq' => 'yearly'],
];

try {
    $stmt = $pdo->query("SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY created_at DESC");
    $posts = $stmt->fetchAll();
    foreach ($posts as $p) {
        $pages[] = ['loc' => 'blog/' . $p['slug'], 'priority' => '0.6', 'changefreq' => 'monthly'];
    }
} catch (Exception $e) {}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $p): ?>
    <url>
        <loc><?= SITE_URL . htmlspecialchars($p['loc']) ?></loc>
        <priority><?= $p['priority'] ?></priority>
        <changefreq><?= $p['changefreq'] ?></changefreq>
    </url>
<?php endforeach; ?>
</urlset>
