<?php
require_once __DIR__ . '/config/config.php';
header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');

$db   = db();
$base = 'https://santacatarinanofeed.com.br';

$articles = $db->query(
    "SELECT a.id, a.slug, a.published_at, a.updated_at,
            c.slug AS cat_slug
     FROM scf_articles a
     JOIN scf_categories c ON a.category_id = c.id
     WHERE a.status = 'published'
     ORDER BY a.published_at DESC
     LIMIT 1000"
)->fetchAll();

$categories = $db->query(
    "SELECT slug, updated_at FROM scf_categories WHERE is_active = 1"
)->fetchAll();

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <!-- Home -->
  <url>
    <loc><?= $base ?>/</loc>
    <changefreq>hourly</changefreq>
    <priority>1.0</priority>
  </url>
  <!-- Categorias -->
  <?php foreach ($categories as $cat): ?>
  <url>
    <loc><?= $base ?>/categoria/<?= e($cat['slug']) ?></loc>
    <changefreq>hourly</changefreq>
    <priority>0.8</priority>
  </url>
  <?php endforeach; ?>
  <!-- Matérias -->
  <?php foreach ($articles as $a): ?>
  <url>
    <loc><?= $base ?>/<?= e($a['cat_slug']) ?>/<?= e($a['id']) ?>-<?= e($a['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($a['updated_at'] ?? $a['published_at'])) ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
  <?php endforeach; ?>
</urlset>
