<?php
require_once __DIR__ . '/config/database.php';
$db = getDB();
$siteUrl = 'http://localhost/projects/res';

$articles = $db->query("SELECT slug, updated_at FROM articles WHERE status='published' ORDER BY updated_at DESC")->fetchAll();
$categories = $db->query("SELECT slug FROM categories ORDER BY name ASC")->fetchAll();

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc><?= $siteUrl ?>/index.php</loc>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc><?= $siteUrl ?>/about.php</loc>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc><?= $siteUrl ?>/contact.php</loc>
    <changefreq>monthly</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc><?= $siteUrl ?>/privacy.php</loc>
    <changefreq>monthly</changefreq>
    <priority>0.3</priority>
  </url>
  <?php foreach ($categories as $c): ?>
  <url>
    <loc><?= $siteUrl ?>/category.php?slug=<?= $c['slug'] ?></loc>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
  <?php endforeach; ?>
  <?php foreach ($articles as $a): ?>
  <url>
    <loc><?= $siteUrl ?>/article.php?slug=<?= $a['slug'] ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($a['updated_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <?php endforeach; ?>
</urlset>
