<?php
/**
 * Sitemap XML Dinámico
 */
header('Content-Type: application/xml; charset=utf-8');

require_once __DIR__ . '/../app/core/Database.php';
require_once __DIR__ . '/../app/config/config.php';

$db = Database::getInstance()->getConnection();

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">
    <url>
        <loc><?= APP_URL ?>/</loc>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
    </url>
    <url>
        <loc><?= APP_URL ?>/servicios</loc>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= APP_URL ?>/portafolio</loc>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
    </url>
    <url>
        <loc><?= APP_URL ?>/contacto</loc>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php
    // Servicios
    $services = $db->query("SELECT slug, updated_at FROM services WHERE is_active = 1 AND deleted_at IS NULL")->fetchAll();
    foreach($services as $s):
    ?>
    <url>
        <loc><?= APP_URL ?>/servicios/<?= htmlspecialchars($s['slug']) ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <?php if($s['updated_at']): ?><lastmod><?= date('Y-m-d', strtotime($s['updated_at'])) ?></lastmod><?php endif; ?>
    </url>
    <?php endforeach; ?>
    <?php
    // Proyectos
    $projects = $db->query("SELECT slug, updated_at FROM projects WHERE is_active = 1 AND deleted_at IS NULL")->fetchAll();
    foreach($projects as $p):
    ?>
    <url>
        <loc><?= APP_URL ?>/portafolio/<?= htmlspecialchars($p['slug']) ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <?php if($p['updated_at']): ?><lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod><?php endif; ?>
    </url>
    <?php endforeach; ?>
</urlset>
