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

$db = getDB();
$base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Homepage -->
    <url>
        <loc><?= $base_url ?>/</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Main Pages -->
    <url>
        <loc><?= $base_url ?>/struktur.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?= $base_url ?>/berita.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?= $base_url ?>/pengumuman.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>
    
    <url>
        <loc><?= $base_url ?>/galeri.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <url>
        <loc><?= $base_url ?>/kontak.php</loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    
    <?php
    // Dynamic news pages
    $stmt = $db->query("SELECT id, updated_at FROM berita WHERE status = 'published' ORDER BY created_at DESC");
    $berita_list = $stmt->fetchAll();
    
    foreach ($berita_list as $berita):
    ?>
    <url>
        <loc><?= $base_url ?>/berita-detail.php?id=<?= $berita['id'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($berita['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
    
    <?php
    // Dynamic pages
    $stmt = $db->query("SELECT slug, updated_at FROM halaman WHERE status = 'published'");
    $halaman_list = $stmt->fetchAll();
    
    foreach ($halaman_list as $halaman):
    ?>
    <url>
        <loc><?= $base_url ?>/halaman.php?slug=<?= $halaman['slug'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($halaman['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    <?php endforeach; ?>
</urlset>