<?php
// sitemap.php
require_once 'config.php';

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

$base_url = "https://" . DOMAIN_NAME;

// Pages statiques
$pages = [
    '' => 'daily',
    'book' => 'weekly', 
    'blog' => 'weekly',
    'terms' => 'monthly',
    'privacy' => 'monthly',
    'contact' => 'monthly'
];

// Articles du blog
$articles = [];
if (file_exists('blog.json')) {
    $articles_data = json_decode(file_get_contents('blog.json'), true) ?? [];
    foreach ($articles_data as $article) {
        $articles[] = 'blog?article=' . $article['id'];
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

foreach ($pages as $page => $changefreq) {
    echo '<url>';
    echo '<loc>' . $base_url . '/' . $page . '</loc>';
    echo '<changefreq>' . $changefreq . '</changefreq>';
    echo '<priority>' . ($page === '' ? '1.0' : '0.8') . '</priority>';
    echo '</url>';
}

foreach ($articles as $article) {
    echo '<url>';
    echo '<loc>' . $base_url . '/' . $article . '</loc>';
    echo '<changefreq>monthly</changefreq>';
    echo '<priority>0.6</priority>';
    echo '</url>';
}

echo '</urlset>';
?>
