<?php
// sitemap.xml — gerado dinamicamente
header('Content-Type: application/xml; charset=utf-8');
require_once __DIR__ . '/includes/functions.php';
$db   = getDB();
$base = rtrim(SITE_URL, '/');

try {
    $produtos = $db->query("SELECT slug, atualizado_em FROM produtos WHERE ativo=1 ORDER BY ordem, id DESC")->fetchAll();
} catch (Exception $e) {
    $produtos = $db->query("SELECT slug FROM produtos WHERE ativo=1 ORDER BY id DESC")->fetchAll();
}
?>
<?= '<?xml version="1.0" encoding="UTF-8"?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- Páginas estáticas -->
  <url><loc><?= $base ?></loc><changefreq>weekly</changefreq><priority>1.0</priority></url>
  <url><loc><?= $base ?>/produtos</loc><changefreq>weekly</changefreq><priority>0.9</priority></url>
  <url><loc><?= $base ?>/aempresa</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
  <url><loc><?= $base ?>/seja-um-representante</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
  <url><loc><?= $base ?>/trabalhe-conosco</loc><changefreq>monthly</changefreq><priority>0.5</priority></url>
  <url><loc><?= $base ?>/prazos-de-fabricao-e-entrega</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
  <url><loc><?= $base ?>/troca-e-garantia</loc><changefreq>monthly</changefreq><priority>0.5</priority></url>
  <url><loc><?= $base ?>/politica-e-privacidade</loc><changefreq>yearly</changefreq><priority>0.3</priority></url>

  <!-- Produtos dinâmicos -->
  <?php foreach ($produtos as $p):
    $data = $p['atualizado_em'] ?? date('Y-m-d');
    $data = substr($data, 0, 10);
  ?>
  <url>
    <loc><?= $base ?>/produto?slug=<?= urlencode($p['slug']) ?></loc>
    <lastmod><?= $data ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <?php endforeach; ?>

</urlset>
