<?php

declare(strict_types=1);

if (ob_get_level()) {
    ob_end_clean();
}

header('Content-Type: application/xml; charset=UTF-8');
header('Cache-Control: public, max-age=3600');
header('X-Robots-Tag: noindex');

require_once __DIR__ . '/config/config.php';

$base = rtrim(APP_URL, '/');

$lastmod = date('c');
try {
    $mx = db()->query("SELECT MAX(updated_at) FROM servers WHERE is_active = 1")->fetchColumn();
    if ($mx) {
        $lastmod = date('c', strtotime((string)$mx));
    }
} catch (Throwable $e) {}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc><?= htmlspecialchars($base, ENT_XML1) ?>/sitemap.xml</loc>
        <lastmod><?= $lastmod ?></lastmod>
    </sitemap>
    <sitemap>
        <loc><?= htmlspecialchars($base, ENT_XML1) ?>/sitemap-news.xml</loc>
        <lastmod><?= $lastmod ?></lastmod>
    </sitemap>
</sitemapindex>
