<?php
/**
 * 动态生成网站地图
 */
header('Content-Type: application/xml');

require_once __DIR__ . '/includes/Database.php';
require_once __DIR__ . '/includes/SiteSettings.php';

Database::init();

$baseUrl = 'https://www.neurova.top';

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    <url>
        <loc><?php echo $baseUrl; ?></loc>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/docs</loc>
        <priority>0.9</priority>
        <changefreq>weekly</changefreq>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/releases</loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>

    <url>
        <loc><?php echo $baseUrl; ?>/admin</loc>
        <priority>0.1</priority>
        <changefreq>monthly</changefreq>
    </url>

    <?php
    // 文档页面
    $docs = Database::fetchAll("SELECT slug, updated_at FROM docs ORDER BY updated_at DESC");
    foreach ($docs as $doc) {
    ?>
    <url>
        <loc><?php echo $baseUrl; ?>/docs/<?php echo htmlspecialchars($doc['slug']); ?></loc>
        <lastmod><?php echo htmlspecialchars($doc['updated_at']); ?></lastmod>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <?php } ?>

    <?php
    // 版本页面
    $releases = Database::fetchAll("SELECT version, updated_at FROM releases ORDER BY updated_at DESC");
    foreach ($releases as $release) {
    ?>
    <url>
        <loc><?php echo $baseUrl; ?>/releases/<?php echo htmlspecialchars($release['version']); ?></loc>
        <lastmod><?php echo htmlspecialchars($release['updated_at']); ?></lastmod>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
    <?php } ?>

</urlset>