<?php
// ===== Nucleo Pyme 360° — Dynamic Sitemap =====
// Generates sitemap.xml with static pages + dynamic blog posts
header('Content-Type: application/xml; charset=utf-8');

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

$baseUrl = 'https://nucleopyme360.com';

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

// Static pages
$staticPages = [
    ['url' => '/', 'priority' => '1.0', 'changefreq' => 'weekly'],
    ['url' => '/blog/', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['url' => '/privacidad.html', 'priority' => '0.3', 'changefreq' => 'yearly'],
];

foreach ($staticPages as $page) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}{$page['url']}</loc>\n";
    echo "    <changefreq>{$page['changefreq']}</changefreq>\n";
    echo "    <priority>{$page['priority']}</priority>\n";
    echo "  </url>\n";
}

// Dynamic blog posts
try {
    $posts = getPublishedPosts();
    foreach ($posts as $post) {
        $lastmod = date('Y-m-d', strtotime($post['updated_at']));
        echo "  <url>\n";
        echo "    <loc>{$baseUrl}/blog/{$post['slug']}</loc>\n";
        echo "    <lastmod>{$lastmod}</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.6</priority>\n";
        echo "  </url>\n";
    }
} catch (Exception $e) {
    // If DB not available, just output static pages
}

echo '</urlset>';
