<?php
require_once __DIR__ . '/../app/helpers.php';

set_security_headers();
header('Content-Type: application/xml; charset=UTF-8');

$pages = public_pages();
foreach ($pages as &$page) {
    $filePath = __DIR__ . $page['path'];
    if ($page['path'] === '/') {
        $filePath = __DIR__ . '/index.php';
    } elseif (str_starts_with($page['path'], '/services/')) {
        $filePath = __DIR__ . '/service-page.php';
    } else {
        $filePath = __DIR__ . $page['path'] . '.php';
    }
    $page['lastmod'] = file_exists($filePath)
        ? gmdate('Y-m-d', filemtime($filePath))
        : gmdate('Y-m-d');
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $page): ?>
    <url>
        <loc><?= h(site_url($page['path'])) ?></loc>
        <lastmod><?= h($page['lastmod']) ?></lastmod>
    </url>
<?php endforeach; ?>
</urlset>
