<?php
// Génération dynamique du sitemap
header('Content-Type: application/xml; charset=utf-8');
define('BASE_PATH', __DIR__);
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/includes/functions.php';

$db = Database::getInstance();
$products   = $db->fetchAll('SELECT slug, updated_at FROM products WHERE is_active = 1');
$categories = $db->fetchAll('SELECT slug FROM categories WHERE is_active = 1');

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url><loc><?= BASE_URL ?>/</loc><changefreq>weekly</changefreq><priority>1.0</priority></url>
    <url><loc><?= BASE_URL ?>/shop</loc><changefreq>daily</changefreq><priority>0.9</priority></url>
    <url><loc><?= BASE_URL ?>/quote</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
    <url><loc><?= BASE_URL ?>/about</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <url><loc><?= BASE_URL ?>/faq</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <url><loc><?= BASE_URL ?>/contact</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
    <?php foreach ($categories as $cat): ?>
    <url><loc><?= BASE_URL ?>/category/<?= e($cat['slug']) ?></loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
    <?php endforeach; ?>
    <?php foreach ($products as $p): ?>
    <url>
        <loc><?= BASE_URL ?>/product/<?= e($p['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>
</urlset>
