<?php
// Enable output buffering to capture and prevent any accidental whitespace leaks before headers
ob_start();

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

$dataFile = __DIR__ . "/data.json";
$data = file_exists($dataFile)
    ? json_decode(file_get_contents($dataFile), true)
    : [];

$baseUrl = "https://heroicgamez.com";

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

<!-- ===================== -->
<!-- MAIN PAGES            -->
<!-- ===================== -->

<url>
  <loc><?= $baseUrl ?>/</loc>
  <changefreq>daily</changefreq>
  <priority>1.0</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/knowledgebase</loc> <!-- Fixed trailing slash consistency -->
  <changefreq>daily</changefreq>
  <priority>0.8</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/dedicated-servers</loc>
  <changefreq>weekly</changefreq>
  <priority>0.9</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/comparison</loc>
  <changefreq>weekly</changefreq>
  <priority>0.8</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/vps</loc>
  <changefreq>weekly</changefreq>
  <priority>0.7</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/ping</loc>
  <changefreq>monthly</changefreq>
  <priority>0.6</priority>
</url>

<url>
  <loc><?= $baseUrl ?>/createyourown</loc>
  <changefreq>monthly</changefreq>
  <priority>0.7</priority>
</url>

<!-- ===================== -->
<!-- GAME PAGES            -->
<!-- ===================== -->

<?php
$gamePages = [
  '7dtd','ats','ark','arma3','beam','citadel','conan',
  'craftopia','cs2-csgo','dayz','discord','enshrd','5m',
  'garrysmod','hytale','returntomoria','mc','palworld',
  'pixark','portalknights','projectzomboid','redm','rust',
  'satisfactory','scum','sotf','squad','soulmask',
  'subnautica','terraria','unturned','valheim','vintagestory','windrose'
];

foreach ($gamePages as $g): ?>
<url>
  <loc><?= $baseUrl ?>/<?= $g ?></loc>
  <changefreq>weekly</changefreq>
  <priority>0.7</priority>
</url>
<?php endforeach; ?>

<!-- ===================== -->
<!-- KNOWLEDGEBASE ARTICLES-->
<!-- ===================== -->

<?php foreach ($data as $article): 
  // Ensure the dynamic slug string is safely encoded for strict XML standard URL values
  $cleanSlug = implode('/', array_map('urlencode', explode('/', $article['slug'])));
?>
<url>
  <loc><?= $baseUrl ?>/knowledgebase/article/<?= htmlspecialchars($cleanSlug, ENT_QUOTES, 'UTF-8') ?></loc>
  <changefreq>monthly</changefreq>
  <priority>0.6</priority>
</url>
<?php endforeach; ?>

</urlset>
<?php
// Flush output clean
ob_end_flush();
?>