<?php
// sitemap.php
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/database.php';

header('Content-Type: application/xml');

$db = Database::getInstance();

$urls = [];

// Pages statiques
$staticPages = ['', 'shop', 'about', 'how-it-works', 'faq', 'contact', 'legal', 'privacy'];
foreach ($staticPages as $page) {
    $urls[] = [
        'loc' => SITE_URL . ($page ? $page . '.php' : ''),
        'changefreq' => 'weekly',
        'priority' => $page === '' ? '1.0' : '0.8'
    ];
}

// Produits
$products = $db->fetchAll("SELECT slug FROM products WHERE is_active = 1");
foreach ($products as $product) {
    $urls[] = [
        'loc' => SITE_URL . 'product.php?slug=' . $product['slug'],
        'changefreq' => 'weekly',
        'priority' => '0.7'
    ];
}

// Catégories
$categories = $db->fetchAll("SELECT id FROM categories WHERE is_active = 1");
foreach ($categories as $category) {
    $urls[] = [
        'loc' => SITE_URL . 'shop.php?category=' . $category['id'],
        'changefreq' => 'weekly',
        'priority' => '0.6'
    ];
}

// Générer le XML
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $url) {
    echo '<url>' . "\n";
    echo '  <loc>' . htmlspecialchars($url['loc']) . '</loc>' . "\n";
    echo '  <changefreq>' . $url['changefreq'] . '</changefreq>' . "\n";
    echo '  <priority>' . $url['priority'] . '</priority>' . "\n";
    echo '</url>' . "\n";
}
echo '</urlset>';