<?php
$base = '/home/tranhiep/tads.ctv.hanoi.vn';
require_once $base . '/api/config.php';
require_once $base . '/api/db.php';

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

$stmt = db()->query("
    SELECT id, slug, name, updated_at
    FROM heritage_sites
    WHERE status = 'active'
    ORDER BY updated_at DESC
");
$sites = $stmt->fetchAll();

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

// Trang chủ
echo "  <url>\n";
echo "    <loc>https://tads.ctv.hanoi.vn/home.html</loc>\n";
echo "    <changefreq>daily</changefreq>\n";
echo "    <priority>1.0</priority>\n";
echo "  </url>\n";

// Từng điểm đến
foreach ($sites as $site) {
    $slug = $site['slug'] ?: $site['id'];
    $updated = date('Y-m-d', strtotime($site['updated_at']));
    echo "  <url>\n";
    echo "    <loc>https://tads.ctv.hanoi.vn/" . htmlspecialchars($slug) . "</loc>\n";
    echo "    <lastmod>$updated</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.8</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>';
