<?php
header('Content-Type: application/xml; charset=UTF-8');
require_once __DIR__ . '/includes/db.php';

$host = 'http://' . $_SERVER['HTTP_HOST'] . '/kgc';
$db = getDB();

// Get all projects
$projects = $db->query("SELECT id, updated_at FROM projects ORDER BY updated_at DESC")->fetchAll(PDO::FETCH_ASSOC);
?><?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Main pages -->
    <url>
        <loc><?= $host ?></loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= $host ?>/projects</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- Dynamic project pages -->
    <?php foreach($projects as $p): ?>
    <url>
        <loc><?= $host ?>/projects?status=all&cat=<?= urlencode($p['category'] ?? 'all') ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
</urlset>