<?php
        require 'config.php';

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

        $scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
        $baseUrl = $scheme . '://' . $_SERVER['HTTP_HOST'];

        echo '<?xml version="1.0" encoding="UTF-8"?>';
        ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

    <!-- BLOG LISTADO -->
    <url>
        <loc><?= $baseUrl ?>/blog.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

    <?php
            // BLOG POSTS
            $posts = $conn->query("
            SELECT slug, updated_at, published_at
            FROM blog_posts
            WHERE status='published'
            ");
            while ($p = $posts->fetch_assoc()):
            $date = $p['updated_at'] ?: $p['published_at'];
            ?>
    <url>
        <loc><?= $baseUrl ?>/blog_detalle.php?slug=<?= htmlspecialchars($p['slug']) ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($date)) ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endwhile; ?>

    <!-- DIRECTORIO -->
    <url>
        <loc><?= $baseUrl ?>/directorio.php</loc>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>

</urlset>
