<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc><?= BASEURL ?></loc>
        <lastmod><?= date('Y-m-d') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <?php
    // Include database connection
    require_once '../app/core/Database.php';
    require_once '../app/config/config.php';
    
    try {
        $db = new Database();
        
        // Get all published events
        $db->query("SELECT id, title, event_date, updated_at FROM events WHERE status = 'published' ORDER BY event_date DESC LIMIT 1000");
        $events = $db->resultSet();
        
        foreach ($events as $event) {
            $slug = strtolower(str_replace([' ', '_'], '-', $event['title']));
            $lastmod = date('Y-m-d', strtotime($event['updated_at']));
            echo "<url>\n";
            echo "    <loc>" . BASEURL . "/event/{$event['id']}/{$slug}</loc>\n";
            echo "    <lastmod>{$lastmod}</lastmod>\n";
            echo "    <changefreq>weekly</changefreq>\n";
            echo "    <priority>0.8</priority>\n";
            echo "</url>\n";
        }
        
        // Get all categories
        $db->query("SELECT name FROM event_categories ORDER BY name");
        $categories = $db->resultSet();
        
        foreach ($categories as $category) {
            $slug = strtolower(str_replace([' ', '_'], '-', $category['name']));
            echo "<url>\n";
            echo "    <loc>" . BASEURL . "/category/{$slug}</loc>\n";
            echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
            echo "    <changefreq>weekly</changefreq>\n";
            echo "    <priority>0.6</priority>\n";
            echo "</url>\n";
        }
        
    } catch (Exception $e) {
        // Handle error silently for sitemap
    }
    ?>
</urlset>