<?php
// ============================================================
// DFILANDIA - Sitemap del portal principal
// URL: dfilandia.com/sitemap-portal.xml
// ============================================================
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/helpers.php';

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

$negocios = fetchAll("
    SELECT n.subdominio, n.nombre, n.tipo,
           n.created_at as lastmod
    FROM negocios n
    WHERE n.activo=1 AND (n.proximamente=0 OR n.proximamente IS NULL)
    ORDER BY n.nombre ASC
");

$fecha_hoy = date('Y-m-d');

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

  <!-- Portal principal -->
  <url>
    <loc>https://dfilandia.com</loc>
    <lastmod><?= $fecha_hoy ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
    <xhtml:link rel="alternate" hreflang="es" href="https://dfilandia.com"/>
    <xhtml:link rel="alternate" hreflang="en" href="https://dfilandia.com?lang=en"/>
  </url>

  <!-- Páginas estáticas -->
  <?php
  $paginas = [
      ['url' => '/hoteles.php',      'freq' => 'daily',   'pri' => '0.9'],
      ['url' => '/restaurantes.php', 'freq' => 'daily',   'pri' => '0.9'],
      ['url' => '/cafes.php',        'freq' => 'daily',   'pri' => '0.8'],
      ['url' => '/experiencias.php', 'freq' => 'weekly',  'pri' => '0.8'],
      ['url' => '/mapa.php',         'freq' => 'weekly',  'pri' => '0.7'],
      ['url' => '/galeria.php',      'freq' => 'weekly',  'pri' => '0.7'],
      ['url' => '/planes.php',       'freq' => 'monthly', 'pri' => '0.6'],
      ['url' => '/registrar.php',    'freq' => 'monthly', 'pri' => '0.6'],
      ['url' => '/contacto.php',     'freq' => 'monthly', 'pri' => '0.5'],
  ];
  foreach ($paginas as $p): ?>
  <url>
    <loc>https://dfilandia.com<?= $p['url'] ?></loc>
    <lastmod><?= $fecha_hoy ?></lastmod>
    <changefreq><?= $p['freq'] ?></changefreq>
    <priority><?= $p['pri'] ?></priority>
  </url>
  <?php endforeach; ?>

  <!-- Páginas de cada negocio -->
  <?php foreach ($negocios as $n):
    $lastmod = $n['lastmod'] ? $n['lastmod'] ? date('Y-m-d', strtotime($n['lastmod'])) : $fecha_hoy : $fecha_hoy;
    $prioridad = in_array($n['tipo'], ['hotel','restaurante','cafe']) ? '0.8' : '0.7';
  ?>
  <url>
    <loc>https://<?= sanitize($n['subdominio']) ?>.dfilandia.com</loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority><?= $prioridad ?></priority>
    <xhtml:link rel="alternate" hreflang="es" href="https://<?= sanitize($n['subdominio']) ?>.dfilandia.com"/>
    <xhtml:link rel="alternate" hreflang="en" href="https://<?= sanitize($n['subdominio']) ?>.dfilandia.com?lang=en"/>
  </url>
  <?php endforeach; ?>

</urlset>