<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'includes/functions.php';

// Get base URL
$base_url = getBaseUrl();

// Start XML output
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><?php echo $base_url; ?>/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>

    <!-- Static Pages -->
    <?php
    $pages = getAllPages();
    foreach ($pages as $page):
        if ($page['status'] === 'published'):
    ?>
    <url>
        <loc><?php echo $base_url; ?>/pages/<?php echo htmlspecialchars($page['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($page['updated_at'])); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php
        endif;
    endforeach;
    ?>

    <!-- Blog/Posts -->
    <?php
    $posts = getAllPosts();
    foreach ($posts as $post):
        if ($post['status'] === 'published'):
    ?>
    <url>
        <loc><?php echo $base_url; ?>/blog/<?php echo htmlspecialchars($post['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d', strtotime($post['updated_at'])); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php
        endif;
    endforeach;
    ?>

    <!-- Categories -->
    <?php
    $categories = getAllCategories();
    foreach ($categories as $category):
        if ($category['status'] === 'active'):
    ?>
    <url>
        <loc><?php echo $base_url; ?>/category/<?php echo htmlspecialchars($category['slug']); ?></loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.5</priority>
    </url>
    <?php
        endif;
    endforeach;
    ?>

    <!-- Blog Index -->
    <url>
        <loc><?php echo $base_url; ?>/blog.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.7</priority>
    </url>

    <!-- Fleet/Booking Pages -->
    <url>
        <loc><?php echo $base_url; ?>/fleet-selection.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

    <url>
        <loc><?php echo $base_url; ?>/booking.php</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>

</urlset>