<?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.title, a.slug, a.published_at,
            c.slug AS cat_slug, c.name AS cat_name
     FROM scf_articles a
     JOIN scf_categories c ON a.category_id = c.id
     WHERE a.status = 'published'
       AND a.published_at >= DATE_SUB(NOW(), INTERVAL 48 HOUR)
     ORDER BY a.published_at DESC
     LIMIT 1000"
)->fetchAll();

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
  <?php foreach ($articles as $a): ?>
  <url>
    <loc><?= $base ?>/<?= e($a['cat_slug']) ?>/<?= e($a['id']) ?>-<?= e($a['slug']) ?></loc>
    <news:news>
      <news:publication>
        <news:name>SC no Feed</news:name>
        <news:language>pt</news:language>
      </news:publication>
      <news:publication_date><?= date('c', strtotime($a['published_at'])) ?></news:publication_date>
      <news:title><?= e($a['title']) ?></news:title>
    </news:news>
  </url>
  <?php endforeach; ?>
</urlset>
