<?php
declare(strict_types=1);

require_once __DIR__ . '/inc/bootstrap.php';
require_once __DIR__ . '/inc/seo.php';

$base = rtrim((string) ($config['site']['base_url'] ?? ''), '/');
if ($base === '') {
    $base = 'https://' . ($_SERVER['HTTP_HOST'] ?? 'localhost');
}

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

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

$now = gmdate('Y-m-d');
foreach (seo_sitemap_entries() as $entry) {
    $loc = $base . ($entry['loc'] === '/' ? '/' : $entry['loc']);
    echo "  <url>\n";
    echo '    <loc>' . htmlspecialchars($loc, ENT_XML1) . "</loc>\n";
    echo "    <lastmod>{$now}</lastmod>\n";
    echo '    <changefreq>' . htmlspecialchars($entry['changefreq'], ENT_XML1) . "</changefreq>\n";
    echo '    <priority>' . htmlspecialchars($entry['priority'], ENT_XML1) . "</priority>\n";
    echo "  </url>\n";
}

echo "</urlset>\n";