<?php
declare(strict_types=1);

while (ob_get_level() > 0) { ob_end_clean(); }
if (function_exists('header_remove')) { header_remove('Content-Type'); }
header('Content-Type: application/xml; charset=UTF-8', true);
header('X-Content-Type-Options: nosniff', true);

$dataFile = __DIR__ . '/data/mahalle-data.json';
$data = json_decode(file_get_contents($dataFile), true);
$total = isset($data['page_count']) ? (int) $data['page_count'] : 0;
if ($total <= 0 && is_array($data) && !empty($data['locations']) && !empty($data['services'])) {
    $total = count($data['locations']) * count($data['services']);
}

$perPage = 50000;
$maxPage = max(1, (int) ceil($total / $perPage));
$lastmod = '2026-06-26';

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo "<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
for ($p = 1; $p <= $maxPage; $p++) {
    echo "  <sitemap>\n";
    echo "    <loc>https://servisplus.net.tr/mahalle-sitemap-" . $p . ".xml</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "  </sitemap>\n";
}
echo "</sitemapindex>\n";
