<?php

declare(strict_types=1);

require_once __DIR__ . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'ServiceApp.php';

header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=86400');

$base = service_site_base_url();

// Public, indexable URLs only. Tool/result pages that require authentication
// or are per-user are intentionally excluded.
$urls = [
    ['loc' => '/', 'changefreq' => 'daily', 'priority' => '1.0', 'source' => 'index.php'],
    ['loc' => '/tao-anh-thong-tin-lien-quan', 'changefreq' => 'weekly', 'priority' => '0.9', 'source' => 'seo-page.php'],
    ['loc' => '/mau-poster-lien-quan', 'changefreq' => 'weekly', 'priority' => '0.8', 'source' => 'seo-page.php'],
    ['loc' => '/huong-dan-ghep-anh-lien-quan', 'changefreq' => 'monthly', 'priority' => '0.8', 'source' => 'seo-page.php'],
    ['loc' => '/check-skin-lien-quan', 'changefreq' => 'weekly', 'priority' => '0.9', 'source' => 'seo-page.php'],
];

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $url) {
    $loc = $base . '/' . ltrim((string) $url['loc'], '/');
    if ($url['loc'] === '/') {
        $loc = $base . '/';
    }
    $sourcePath = __DIR__ . DIRECTORY_SEPARATOR . (string) $url['source'];
    $sourceModifiedAt = is_file($sourcePath) ? filemtime($sourcePath) : false;
    $lastmod = gmdate('Y-m-d', $sourceModifiedAt !== false ? $sourceModifiedAt : time());
    echo "  <url>\n";
    echo '    <loc>' . htmlspecialchars($loc, ENT_XML1 | ENT_QUOTES, 'UTF-8') . "</loc>\n";
    echo '    <lastmod>' . $lastmod . "</lastmod>\n";
    echo '    <changefreq>' . htmlspecialchars((string) $url['changefreq'], ENT_XML1, 'UTF-8') . "</changefreq>\n";
    echo '    <priority>' . htmlspecialchars((string) $url['priority'], ENT_XML1, 'UTF-8') . "</priority>\n";
    echo "  </url>\n";
}
echo '</urlset>' . "\n";
