<?php
// /public/sitemap.xml
declare(strict_types=1);

// Kanonik host ve HTTPS zorunluluğu
$CANONICAL_HOST = 'skorburada.com';
if (($_SERVER['HTTP_HOST'] ?? '') !== $CANONICAL_HOST) {
  header('Location: https://' . $CANONICAL_HOST . '/sitemap.xml', true, 301);
  exit;
}
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
  header('Location: https://' . $CANONICAL_HOST . '/sitemap.xml', true, 301);
  exit;
}

// Gzip + cache
if (function_exists('ob_gzhandler')) ob_start('ob_gzhandler');
header('Content-Type: application/xml; charset=UTF-8');
header('X-Robots-Tag: noindex, follow');
header('Cache-Control: public, max-age=3600, stale-while-revalidate=60');

$now = gmdate('c');

// Ana parçalar
$parts = [
  ['loc' => 'https://skorburada.com/sitemaps/sitemap-static.xml',  'lastmod' => $now],
  ['loc' => 'https://skorburada.com/sitemaps/sitemap-ligler.xml',  'lastmod' => $now],
  ['loc' => 'https://skorburada.com/sitemaps/sitemap-teams.xml',   'lastmod' => $now],
  ['loc' => 'https://skorburada.com/sitemaps/sitemap-players.xml', 'lastmod' => $now],
];

// Son 12 ay: maç ve haber sitemap’leri (ay bazlı)
for ($i = 0; $i < 12; $i++) {
  $t  = strtotime("-$i month");
  $y  = date('Y', $t);
  $m  = date('m', $t);
  $firstDay = gmdate('c', strtotime(date('Y-m-01 00:00:00', $t)));

  $parts[] = ['loc' => "https://skorburada.com/sitemaps/sitemap-maclar.php?y=$y&m=$m", 'lastmod' => $firstDay];
  $parts[] = ['loc' => "https://skorburada.com/sitemaps/sitemap-news.php?y=$y&m=$m",   'lastmod' => $firstDay];
}

// Bas
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($parts as $p): ?>
  <sitemap>
    <loc><?= htmlspecialchars($p['loc'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></loc>
    <lastmod><?= $p['lastmod'] ?></lastmod>
  </sitemap>
<?php endforeach; ?>
</sitemapindex>
