<?php
/**
 * EchoNovel - Dynamic Sitemap XML Generator
 * Works for both .xml and .php access
 */
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/data/novels.php';

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

$siteUrl = SITE_URL;

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

    <!-- Home Page -->
    <url>
        <loc><?= $siteUrl ?>/?page=home</loc>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>

    <!-- Explore -->
    <url>
        <loc><?= $siteUrl ?>/?page=explore</loc>
        <priority>0.9</priority>
        <changefreq>weekly</changefreq>
    </url>

    <!-- Login / Register -->
    <url>
        <loc><?= $siteUrl ?>/?page=login</loc>
        <priority>0.3</priority>
        <changefreq>monthly</changefreq>
    </url>
    <url>
        <loc><?= $siteUrl ?>/?page=register</loc>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>

    <!-- Search -->
    <url>
        <loc><?= $siteUrl ?>/?page=search</loc>
        <priority>0.5</priority>
        <changefreq>weekly</changefreq>
    </url>

    <?php foreach ($novels as $novel): ?>
    <url>
        <loc><?= $siteUrl ?>/?page=detail&amp;id=<?= $novel['id'] ?></loc>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
        <image:image>
            <image:loc><?= htmlspecialchars($novel['cover']) ?></image:loc>
            <image:title><?= htmlspecialchars($novel['title']) ?></image:title>
        </image:image>
    </url>
    <?php endforeach; ?>

</urlset>
