<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';

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

$site_url  = rtrim(ayar('site_url', 'https://erdagyapi.com'), '/');
$site_adi  = ayar('site_adi', 'Erdağ Yapı Dekorasyon');
$site_desc = ayar('site_aciklama', 'İstanbul\'da profesyonel duvar kağıdı, dekorasyon ve tadilat hizmetleri.');

$db = DB::get();
$yazilar = $db->query(
    "SELECT id, baslik, slug, ozet, gorsel, created_at, updated_at
     FROM blog_yazilari
     WHERE durum = 'yayinda'
     ORDER BY created_at DESC
     LIMIT 20"
)->fetchAll();

$son_guncelleme = !empty($yazilar) ? date('c', strtotime($yazilar[0]['updated_at'] ?? $yazilar[0]['created_at'])) : date('c');

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title><?= htmlspecialchars($site_adi, ENT_XML1, 'UTF-8') ?></title>
  <subtitle><?= htmlspecialchars($site_desc, ENT_XML1, 'UTF-8') ?></subtitle>
  <link href="<?= htmlspecialchars($site_url . '/feed-atom.xml', ENT_XML1, 'UTF-8') ?>" rel="self"/>
  <link href="<?= htmlspecialchars($site_url, ENT_XML1, 'UTF-8') ?>"/>
  <id><?= htmlspecialchars($site_url . '/feed-atom.xml', ENT_XML1, 'UTF-8') ?></id>
  <updated><?= $son_guncelleme ?></updated>
  <generator>Erdağ Yapı CMS</generator>
  <?php foreach ($yazilar as $y): ?>
  <entry>
    <title><?= htmlspecialchars($y['baslik'], ENT_XML1, 'UTF-8') ?></title>
    <link href="<?= htmlspecialchars($site_url . '/blog/' . $y['slug'], ENT_XML1, 'UTF-8') ?>"/>
    <id><?= htmlspecialchars($site_url . '/blog/' . $y['slug'], ENT_XML1, 'UTF-8') ?></id>
    <published><?= date('c', strtotime($y['created_at'])) ?></published>
    <updated><?= date('c', strtotime($y['updated_at'] ?? $y['created_at'])) ?></updated>
    <author><name><?= htmlspecialchars($site_adi, ENT_XML1, 'UTF-8') ?></name></author>
    <?php if ($y['ozet']): ?>
    <summary><?= htmlspecialchars($y['ozet'], ENT_XML1, 'UTF-8') ?></summary>
    <?php endif; ?>
  </entry>
  <?php endforeach; ?>
</feed>
