<?php
header("Content-Type: application/xml; charset=UTF-8");

$entries = [];
require_once __DIR__ . "/explainer-cache.php";
foreach (stooonks_explainer_cache_all() as $cached) {
    $ticker = strtoupper(trim((string)($cached["ticker"] ?? "")));
    if ($ticker === "" || !preg_match('/^[A-Z0-9][A-Z0-9.-]{0,31}$/', $ticker)) {
        continue;
    }
    $key = $ticker;
    if (!isset($entries[$key])) {
        $entries[$key] = [
            "url" => "https://stooonks.com/" . rawurlencode($ticker),
            "lastmod" => date("Y-m-d", strtotime((string)($cached["last_researched_at"] ?? "")) ?: time())
        ];
    }
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
echo '<url><loc>https://stooonks.com/</loc></url>';

foreach ($entries as $entry) {
    echo "<url>";
    echo "<loc>" . htmlspecialchars($entry["url"], ENT_XML1, "UTF-8") . "</loc>";
    echo "<lastmod>" . htmlspecialchars($entry["lastmod"], ENT_XML1, "UTF-8") . "</lastmod>";
    echo "</url>";
}

echo "</urlset>";
?>
