<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";

$static = [
    ['/', '1.0', 'weekly', date('Y-m-d')],
    ['/about', '0.8', 'monthly', date('Y-m-d')],
    ['/trade', '0.9', 'monthly', date('Y-m-d')],
    ['/gallery', '0.7', 'weekly', date('Y-m-d')],
    ['/news', '0.8', 'daily', date('Y-m-d')],
    ['/contact', '0.6', 'monthly', date('Y-m-d')],
    ['/admission', '0.9', 'weekly', date('Y-m-d')],
    ['/placement', '0.7', 'monthly', date('Y-m-d')],
    ['/downloads', '0.6', 'weekly', date('Y-m-d')],
];

try {
    $trades = DB::fetchAll("SELECT slug, updated_at FROM trades WHERE is_active=1");
    $news   = DB::fetchAll("SELECT slug, updated_at FROM news_events WHERE is_published=1 ORDER BY published_at DESC LIMIT 100");
} catch(Exception){ $trades=[]; $news=[]; }
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($static as [$path,$pri,$freq,$date]): ?>
  <url>
    <loc><?= SITE_URL . $path ?></loc>
    <lastmod><?= $date ?></lastmod>
    <changefreq><?= $freq ?></changefreq>
    <priority><?= $pri ?></priority>
  </url>
<?php endforeach; ?>
<?php foreach ($trades as $t): ?>
  <url>
    <loc><?= SITE_URL ?>/trade?slug=<?= e($t['slug']) ?></loc>
    <lastmod><?= $t['updated_at'] ? date('Y-m-d',strtotime($t['updated_at'])) : date('Y-m-d') ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
<?php endforeach; ?>
<?php foreach ($news as $n): ?>
  <url>
    <loc><?= SITE_URL ?>/news?slug=<?= e($n['slug']) ?></loc>
    <lastmod><?= $n['updated_at'] ? date('Y-m-d',strtotime($n['updated_at'])) : date('Y-m-d') ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.6</priority>
  </url>
<?php endforeach; ?>
</urlset>
