<?php
/**
 * Akdeniz Epilepsi Cerrahisi Derneği
 * Dinamik XML Sitemap
 * 
 * Bu dosya .xml uzantılı ama PHP ile işlenir.
 * .htaccess'e şu kuralı ekleyin:
 *   RewriteRule ^sitemap\.xml$ sitemap.xml [L,T=application/xml]
 * 
 * VEYA dosyayı sitemap.php olarak yükleyip robots.txt'te
 *   Sitemap: https://bilgeyazar.com/akdenizepilepsi/sitemap.php
 * şeklinde belirtin.
 */
require_once __DIR__ . '/config.php';

header('Content-Type: application/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';

$base = rtrim(SITE_URL, '/');
$today = date('Y-m-d');

// ═══════════════════════════════════
// STATİK SAYFALAR
// ═══════════════════════════════════
$static_pages = [
    // [path, priority, changefreq]
    ['',                    '1.0',  'daily'],
    ['hakkimizda.php',      '0.8',  'monthly'],
    ['yonetim-kurulu.php',  '0.7',  'monthly'],
    ['tuzuk.php',           '0.6',  'yearly'],
    ['etkinlikler.php',     '0.9',  'weekly'],
    ['akademik-kutuphane.php', '0.8', 'weekly'],
    ['sss.php',             '0.7',  'monthly'],
    ['iletisim.php',        '0.7',  'monthly'],
    ['hasta-rehberi.php',   '0.8',  'monthly'],
    ['aidat-odeme.php',     '0.6',  'monthly'],
    ['uyelik.php',          '0.7',  'monthly'],
    ['kvkk.php',            '0.4',  'yearly'],
    ['gizlilik.php',        '0.4',  'yearly'],
    ['cerez.php',           '0.4',  'yearly'],
];

// ═══════════════════════════════════
// DİNAMİK İÇERİKLER (DB'den)
// ═══════════════════════════════════
$events = [];
$articles = [];

try {
    // Etkinlikler
    $stmt = $pdo->query("SELECT id, updated_at FROM events ORDER BY event_date DESC");
    $events = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // Makaleler
    $stmt = $pdo->query("SELECT id, updated_at FROM articles WHERE access_level = 'open' ORDER BY created_at DESC");
    $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    // DB bağlantı hatası olursa sadece statik sayfaları göster
}
?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

<?php foreach ($static_pages as [$path, $priority, $freq]): ?>
    <url>
        <loc><?= $base . '/' . $path ?></loc>
        <lastmod><?= $today ?></lastmod>
        <changefreq><?= $freq ?></changefreq>
        <priority><?= $priority ?></priority>
    </url>
<?php endforeach; ?>

<?php foreach ($events as $event): ?>
    <url>
        <loc><?= $base ?>/etkinlik-detay.php?id=<?= $event['id'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($event['updated_at'] ?? $today)) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
<?php endforeach; ?>

<?php foreach ($articles as $article): ?>
    <url>
        <loc><?= $base ?>/makale-detay.php?id=<?= $article['id'] ?></loc>
        <lastmod><?= date('Y-m-d', strtotime($article['updated_at'] ?? $today)) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
<?php endforeach; ?>

</urlset>