<?php
require __DIR__ . '/includes/bootstrap.php';
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=3600');

function sx($value) {
    return htmlspecialchars((string) $value, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}

function sitemap_lastmod(array $files) {
    $mtime = 0;
    foreach ($files as $file) {
        if (is_file($file)) $mtime = max($mtime, (int) filemtime($file));
    }
    return gmdate('Y-m-d', $mtime ?: time());
}

$coreLastmod = sitemap_lastmod([
    __FILE__,
    __DIR__ . '/config/site.php',
    __DIR__ . '/includes/header.php',
    __DIR__ . '/includes/footer.php',
]);
$toolsLastmod = sitemap_lastmod([
    __FILE__,
    __DIR__ . '/config/tools.php',
    __DIR__ . '/tools/tool.php',
]);
$locationsLastmod = sitemap_lastmod([
    __FILE__,
    __DIR__ . '/config/locations.php',
    __DIR__ . '/includes/seo.php',
    __DIR__ . '/config/location-content.json',
]);

$urls = [
    ['/', '1.0', 'weekly', $coreLastmod],
    ['/tools', '0.9', 'weekly', $toolsLastmod],
    ['/about', '0.6', 'monthly', $coreLastmod],
    ['/architecture', '0.6', 'monthly', $coreLastmod],
    ['/privacy', '0.4', 'monthly', $coreLastmod],
    ['/terms', '0.3', 'monthly', $coreLastmod],
    ['/disclaimer', '0.3', 'monthly', $coreLastmod],
    ['/contact', '0.5', 'monthly', $coreLastmod],
    ['/locations', '0.7', 'monthly', $locationsLastmod],
    ['/guides', '0.8', 'weekly', $coreLastmod],
    ['/editorial-policy', '0.4', 'monthly', $coreLastmod],
    ['/cookies', '0.3', 'monthly', $coreLastmod],
];

foreach (all_tools() as $slug => $tool) {
    $urls[] = [
        '/tools/' . rawurlencode($slug),
        !empty($tool['popular']) ? '0.9' : '0.8',
        'monthly',
        $toolsLastmod,
    ];
}


$guidesLastmod = sitemap_lastmod([__FILE__, __DIR__ . '/config/guides.php', __DIR__ . '/config/guides-extra.json', __DIR__ . '/guides/guide.php']);
foreach (all_guides() as $slug => $guide) {
    $urls[] = ['/guides/' . rawurlencode($slug), '0.7', 'monthly', $guidesLastmod];
}

foreach (seo_hub_indexable_location_entries() as $location) {
    $urls[] = [
        seo_location_hub_url($location),
        $location['level'] === 'country' ? '0.7' : ($location['level'] === 'region' ? '0.65' : '0.6'),
        'monthly',
        $locationsLastmod,
        true,
    ];
}

// Tool + location variants are intentionally not sitemap URLs. They 301 to the
// substantive location hub, while canonical tool pages remain location-neutral.

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $entry) {
    [$path, $priority, $freq, $lastmod] = $entry;
    $isAbsolute = !empty($entry[4]);
    $loc = $isAbsolute ? $path : absolute_url($path);
    echo "  <url>\n";
    echo '    <loc>' . sx($loc) . "</loc>\n";
    echo '    <lastmod>' . sx($lastmod) . "</lastmod>\n";
    echo '    <changefreq>' . sx($freq) . "</changefreq>\n";
    echo '    <priority>' . sx($priority) . "</priority>\n";
    echo "  </url>\n";
}
echo "</urlset>\n";
