<?php
// sitemap.php
require_once 'includes/functions.php';
require_once 'includes/database.php';

header('Content-Type: application/xml; charset=utf-8');

$baseUrl = 'https://' . $_SERVER['HTTP_HOST'];
$events = EventsDB::getUpcoming();
$boutiques = BoutiquesDB::getAll();
$categories = CategoriesDB::getAll();

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Pages principales -->
    <url>
        <loc><?= $baseUrl ?>/</loc>
        <lastmod><?= date('c') ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/boutiques.php</loc>
        <lastmod><?= date('c') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    
    <!-- Événements -->
    <?php foreach ($events as $event): ?>
    <url>
        <loc><?= $baseUrl ?>/event.php?id=<?= $event['id'] ?></loc>
        <lastmod><?= date('c', strtotime($event['updated_at'] ?? $event['created_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Boutiques -->
    <?php foreach ($boutiques as $boutique): ?>
    <url>
        <loc><?= $baseUrl ?>/boutique.php?id=<?= $boutique['id'] ?></loc>
        <lastmod><?= date('c', strtotime($boutique['updated_at'] ?? $boutique['created_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Pages par catégorie -->
    <?php foreach ($categories as $category): ?>
    <url>
        <loc><?= $baseUrl ?>/?category=<?= $category['id'] ?></loc>
        <lastmod><?= date('c') ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
</urlset>