<?php
require 'admin/config.php';

// Устанавливаем заголовок XML
header('Content-Type: application/xml; charset=utf-8');

// Отключаем вывод ошибок для чистого XML
error_reporting(0);
ini_set('display_errors', 0);

// Получаем всех артистов
$stmt = $pdo->query("
    SELECT id, slug, updated_at 
    FROM artists 
    ORDER BY name
");
$artists = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Получаем дату последнего обновления релизов
$stmt = $pdo->query("
    SELECT MAX(updated_at) as last_update 
    FROM releases 
    WHERE release_date <= CURDATE()
");
$last_release = $stmt->fetch(PDO::FETCH_ASSOC);
$lastmod_date = $last_release['last_update'] ?? date('Y-m-d');

// Формируем XML
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Главная страница -->
    <url>
        <loc>https://blitztime.ru/</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Страница всех релизов -->
    <url>
        <loc>https://blitztime.ru/new</loc>
        <lastmod><?php echo $lastmod_date; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>0.9</priority>
    </url>
    
    <!-- Страницы артистов (используем красивые URL) -->
    <?php foreach ($artists as $artist): ?>
    <url>
        <loc>https://blitztime.ru/artist/<?php echo htmlspecialchars($artist['slug']); ?></loc>
        <lastmod><?php echo !empty($artist['updated_at']) ? date('Y-m-d', strtotime($artist['updated_at'])) : date('Y-m-d'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Популярные релизы (якорные ссылки на странице артиста) -->
    <?php
    $stmt = $pdo->query("
        SELECT r.id, r.slug, a.slug as artist_slug, r.updated_at, r.play_count
        FROM releases r
        LEFT JOIN artists a ON r.artist_id = a.id
        WHERE r.release_date <= CURDATE() 
        AND r.mp3 IS NOT NULL 
        AND r.mp3 != ''
        ORDER BY r.play_count DESC
        LIMIT 50
    ");
    $top_releases = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($top_releases as $release): 
    ?>
    <url>
        <loc>https://blitztime.ru/artist/<?php echo htmlspecialchars($release['artist_slug']); ?>#release-<?php echo $release['id']; ?></loc>
        <lastmod><?php echo !empty($release['updated_at']) ? date('Y-m-d', strtotime($release['updated_at'])) : date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>
    
    <!-- Если есть страница "О проекте" -->
    <!--
    <url>
        <loc>https://blitztime.ru/about</loc>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    -->
</urlset>