<?php
// ── Auto-generated sitemap.xml
// Access: https://hetverhuurloket.nl/sitemap.xml

require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');

$baseUrl = 'https://hetverhuurloket.nl';
$cats    = getCategories();

// Fetch all items
try {
    $db   = getDB();
    $stmt = $db->query("SELECT id, title, updated_at FROM items ORDER BY updated_at DESC LIMIT 1000");
    $items = $stmt->fetchAll();
} catch (Exception $e) {
    $items = [];
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    <!-- Homepage -->
    <url>
        <loc><?= $baseUrl ?>/</loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>

    <!-- Search -->
    <url>
        <loc><?= $baseUrl ?>/search.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>

    <!-- All items -->
    <url>
        <loc><?= $baseUrl ?>/items/category.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>

    <!-- Category pages -->
    <?php foreach ($cats as $slug => $cat): ?>
    <url>
        <loc><?= $baseUrl ?>/items/category.php?cat=<?= $slug ?></loc>
        <changefreq>daily</changefreq>
        <priority>0.85</priority>
        <lastmod><?= date('Y-m-d') ?></lastmod>
    </url>
    <?php endforeach; ?>

    <!-- Auth pages -->
    <url>
        <loc><?= $baseUrl ?>/auth/register.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <url>
        <loc><?= $baseUrl ?>/auth/login.php</loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

    <!-- Individual item pages -->
    <?php foreach ($items as $item): ?>
    <url>
        <loc><?= $baseUrl ?>/items/view.php?id=<?= (int)$item['id'] ?></loc>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
        <lastmod><?= date('Y-m-d', strtotime($item['updated_at'])) ?></lastmod>
    </url>
    <?php endforeach; ?>

</urlset>
