<?php
while (ob_get_level()) {
    ob_end_clean();
}

header('Content-Type: application/xml; charset=UTF-8', true);
header('X-Robots-Tag: noindex', true);
header('Cache-Control: public, max-age=3600', true);

$base = 'https://aurumphp.my.id';

$pages = [
    ['path' => 'index.html', 'loc' => '/', 'changefreq' => 'weekly', 'priority' => '1.0', 'hreflang' => true],
    ['path' => 'fitur.html', 'loc' => '/fitur.html', 'changefreq' => 'monthly', 'priority' => '0.9', 'hreflang' => true],
    ['path' => 'event.html', 'loc' => '/event.html', 'changefreq' => 'weekly', 'priority' => '0.9', 'hreflang' => true],
    ['path' => 'contact.php', 'loc' => '/contact.php', 'changefreq' => 'monthly', 'priority' => '0.8', 'hreflang' => true],
    ['path' => 'affiliate.html', 'loc' => '/affiliate.html', 'changefreq' => 'monthly', 'priority' => '0.8', 'hreflang' => true],
    ['path' => 'investor.html', 'loc' => '/investor.html', 'changefreq' => 'monthly', 'priority' => '0.6', 'hreflang' => true],
    ['path' => 'changelog.html', 'loc' => '/changelog.html', 'changefreq' => 'weekly', 'priority' => '0.6', 'hreflang' => false],
    ['path' => 'flow.html', 'loc' => '/flow.html', 'changefreq' => 'monthly', 'priority' => '0.5', 'hreflang' => false],
    ['path' => 'terms.html', 'loc' => '/terms.html', 'changefreq' => 'yearly', 'priority' => '0.3', 'hreflang' => false],
    ['path' => 'privacy.html', 'loc' => '/privacy.html', 'changefreq' => 'yearly', 'priority' => '0.3', 'hreflang' => false],
    ['path' => 'refund.html', 'loc' => '/refund.html', 'changefreq' => 'yearly', 'priority' => '0.3', 'hreflang' => false],
    ['path' => 'application_helpers_reference_pdf.html', 'loc' => '/application_helpers_reference_pdf.html', 'changefreq' => 'monthly', 'priority' => '0.6', 'hreflang' => false],
    ['path' => 'aurum-user-guide-en.html', 'loc' => '/aurum-user-guide-en.html', 'changefreq' => 'monthly', 'priority' => '0.8', 'hreflang' => false],
    ['path' => 'forum/topics.php', 'loc' => '/forum/topics.php', 'changefreq' => 'daily', 'priority' => '0.7', 'hreflang' => false],
];

function pageLastmod($path)
{
    $file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $path);
    if (is_file($file)) {
        return gmdate('Y-m-d', filemtime($file));
    }

    return gmdate('Y-m-d');
}

function xmlEscape($value)
{
    return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n";
echo '        xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";

foreach ($pages as $page) {
    $url = $base . $page['loc'];
    $lastmod = pageLastmod($page['path']);

    echo "  <url>\n";
    echo '    <loc>' . xmlEscape($url) . "</loc>\n";
    echo '    <lastmod>' . xmlEscape($lastmod) . "</lastmod>\n";
    echo '    <changefreq>' . xmlEscape($page['changefreq']) . "</changefreq>\n";
    echo '    <priority>' . xmlEscape($page['priority']) . "</priority>\n";

    if (!empty($page['hreflang'])) {
        $langEn = $url . (strpos($page['loc'], '?') !== false ? '&' : '?') . 'lang=en';
        echo '    <xhtml:link rel="alternate" hreflang="id" href="' . xmlEscape($url) . '"/>' . "\n";
        echo '    <xhtml:link rel="alternate" hreflang="en" href="' . xmlEscape($langEn) . '"/>' . "\n";
        echo '    <xhtml:link rel="alternate" hreflang="x-default" href="' . xmlEscape($url) . '"/>' . "\n";
    }

    echo "  </url>\n";
}

echo "</urlset>\n";
