<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
$baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost');
$db = getDB();
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url><loc><?= $baseUrl ?>/</loc><changefreq>daily</changefreq><priority>1.0</priority></url>
    <url><loc><?= $baseUrl ?>/about</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
    <url><loc><?= $baseUrl ?>/services</loc><changefreq>weekly</changefreq><priority>0.9</priority></url>
    <url><loc><?= $baseUrl ?>/workshops</loc><changefreq>weekly</changefreq><priority>0.9</priority></url>
    <url><loc><?= $baseUrl ?>/articles</loc><changefreq>daily</changefreq><priority>0.9</priority></url>
    <url><loc><?= $baseUrl ?>/sections</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <url><loc><?= $baseUrl ?>/contact</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <url><loc><?= $baseUrl ?>/gallery</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <url><loc><?= $baseUrl ?>/alumni</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <?php
    $galleryCategories = $db->query("SELECT slug FROM gallery_categories WHERE active = TRUE ORDER BY sort_order")->fetchAll();
    foreach ($galleryCategories as $gc): ?>
    <url><loc><?= $baseUrl ?>/gallery/<?= htmlspecialchars($gc['slug']) ?></loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
    <?php endforeach; ?>
    <?php
    $services = $db->query("SELECT slug, created_at FROM services WHERE active = TRUE ORDER BY sort_order")->fetchAll();
    foreach ($services as $s): ?>
    <url><loc><?= $baseUrl ?>/service/<?= htmlspecialchars($s['slug']) ?></loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
    <?php endforeach; ?>
    <?php
    $workshops = $db->query("SELECT slug, created_at FROM workshops WHERE active = TRUE ORDER BY sort_order")->fetchAll();
    foreach ($workshops as $w): ?>
    <url><loc><?= $baseUrl ?>/workshop/<?= htmlspecialchars($w['slug']) ?></loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
    <?php endforeach; ?>
    <?php
    $articles = $db->query("SELECT slug, updated_at FROM articles WHERE published = TRUE ORDER BY created_at DESC")->fetchAll();
    foreach ($articles as $a): ?>
    <url><loc><?= $baseUrl ?>/article/<?= htmlspecialchars($a['slug']) ?></loc><lastmod><?= date('Y-m-d', strtotime($a['updated_at'])) ?></lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <?php endforeach; ?>
    <?php
    $sections = $db->query("SELECT slug FROM sections WHERE active = TRUE ORDER BY sort_order")->fetchAll();
    foreach ($sections as $s): ?>
    <url><loc><?= $baseUrl ?>/section/<?= htmlspecialchars($s['slug']) ?></loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <?php endforeach; ?>
</urlset>
