<?php

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

header('Content-Type: application/xml; charset=utf-8');

$staticPages = [
    '',
    'pages/turkey-to-russia.php',
    'pages/russia-to-turkey.php',
    'pages/istanbul-moscow.php',
    'pages/moscow-istanbul.php',
    'pages/documents.php',
    'pages/personal-items.php',
    'pages/groupage-cargo.php',
    'blog/'
];

$stmt = $pdo->query("
    SELECT slug, updated_at, created_at
    FROM articles
    WHERE status = 'published'
    ORDER BY created_at DESC
");

$articles = $stmt->fetchAll();

echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($staticPages as $page): ?>
  <url>
    <loc><?= e(currentUrl($page)) ?></loc>
    <changefreq>weekly</changefreq>
    <priority><?= $page === '' ? '1.0' : '0.8' ?></priority>
  </url>
<?php endforeach; ?>

<?php foreach ($articles as $article): ?>
  <url>
    <loc><?= e(currentUrl('blog/' . $article['slug'])) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($article['updated_at'] ?: $article['created_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
<?php endforeach; ?>
</urlset>