<?php
/**
 * sitemap.xml - نقشه سایت داینامیک برای پاسارگاد RP
 * خودکار تمام لینک‌های مهم رو شامل می‌شه
 */

// ===== تنظیمات اولیه =====
header('Content-Type: application/xml; charset=utf-8');

// ===== بارگذاری کانفیگ =====
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/functions.php';

// ===== تنظیمات پایه =====
$siteUrl = SITE_URL;
$now = date('Y-m-d');

// ===== دریافت لینک‌های داینامیک =====
$allNews = getAllNews(); // از functions.php
$allGallery = getGallery(); // از functions.php

// ===== ساختار URLها =====
$urls = [
    // ===== صفحات ثابت =====
    ['loc' => $siteUrl . '/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => $siteUrl . '/forum.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['loc' => $siteUrl . '/leaderboard.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['loc' => $siteUrl . '/online.php', 'priority' => '0.8', 'changefreq' => 'always'],
    ['loc' => $siteUrl . '/ticket.php', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['loc' => $siteUrl . '/upload.php', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['loc' => $siteUrl . '/streamer.php', 'priority' => '0.6', 'changefreq' => 'daily'],
    ['loc' => $siteUrl . '/live.php', 'priority' => '0.8', 'changefreq' => 'always'],
    ['loc' => $siteUrl . '/dashboard', 'priority' => '0.5', 'changefreq' => 'weekly'],
    ['loc' => $siteUrl . '/login.php', 'priority' => '0.4', 'changefreq' => 'yearly'],
];

// ===== اخبار =====
foreach ($allNews as $news) {
    $urls[] = [
        'loc' => $siteUrl . '/index.php#news-' . $news['id'],
        'priority' => '0.8',
        'changefreq' => 'weekly',
        'lastmod' => date('Y-m-d', strtotime($news['updated_at'] ?? $news['created_at'] ?? 'now'))
    ];
}

// ===== گالری =====
foreach ($allGallery as $item) {
    $urls[] = [
        'loc' => $siteUrl . '/index.php#gallery-' . $item['id'],
        'priority' => '0.6',
        'changefreq' => 'monthly',
        'lastmod' => date('Y-m-d', strtotime($item['created_at'] ?? 'now'))
    ];
}

// ===== ساختار نقشه سایت =====
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";

foreach ($urls as $url) {
    echo '  <url>' . "\n";
    echo '    <loc>' . htmlspecialchars($url['loc']) . '</loc>' . "\n";
    echo '    <lastmod>' . ($url['lastmod'] ?? $now) . '</lastmod>' . "\n";
    echo '    <changefreq>' . ($url['changefreq'] ?? 'daily') . '</changefreq>' . "\n";
    echo '    <priority>' . ($url['priority'] ?? '0.5') . '</priority>' . "\n";
    echo '  </url>' . "\n";
}

echo '</urlset>';
?>