<?php
/**
 * Dynamic XML Sitemap Generator
 * Generates sitemap.xml for search engines
 * 
 * Usage: https://yourdomain.com/sitemap.php
 * Or configure .htaccess to serve at /sitemap.xml
 */

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex'); // Prevent sitemap itself from being indexed

// ============================================================================
// 🔧 CONFIGURATION
// ============================================================================
$baseUrl = 'https://yourdomain.com';
$lastModFrequency = 'weekly';
$priorityHome = 1.0;
$prioritySection = 0.8;
$priorityContent = 0.6;

// ============================================================================
// 🗄️ DATA SOURCES (replace with database queries in production)
// ============================================================================

// Simulate database connection
function getAlbums(): array {
    return [
        ['slug' => 'pbx-1', 'title' => 'PBX 1', 'year' => 2018, 'lastmod' => '2026-04-01'],
        ['slug' => 'moosetape', 'title' => 'Moosetape', 'year' => 2021, 'lastmod' => '2026-04-15'],
        ['slug' => 'no-name', 'title' => 'No Name', 'year' => 2021, 'lastmod' => '2026-03-20'],
        ['slug' => 'snitches-get-stitches', 'title' => 'Snitches Get Stitches', 'year' => 2020, 'lastmod' => '2026-02-10'],
    ];
}

function getTracks(): array {
    return [
        ['id' => 1, 'slug' => 'so-high', 'title' => 'So High', 'album' => 'PBX 1', 'lastmod' => '2026-04-20'],
        ['id' => 11, 'slug' => '295', 'title' => '295', 'album' => 'Moosetape', 'lastmod' => '2026-04-25'],
        ['id' => 12, 'slug' => 'the-last-ride', 'title' => 'The Last Ride', 'album' => 'Moosetape', 'lastmod' => '2026-04-22'],
        // Add more tracks...
    ];
}

function getStaticPages(): array {
    return [
        ['path' => '/', 'changefreq' => 'weekly', 'priority' => $priorityHome],
        ['path' => '/?action=biography', 'changefreq' => 'monthly', 'priority' => $prioritySection],
        ['path' => '/?action=discography', 'changefreq' => 'weekly', 'priority' => $prioritySection],
        ['path' => '/?action=lyrics', 'changefreq' => 'daily', 'priority' => $prioritySection],
        ['path' => '/privacy', 'changefreq' => 'yearly', 'priority' => 0.3],
        ['path' => '/terms', 'changefreq' => 'yearly', 'priority' => 0.3],
    ];
}

// ============================================================================
// 🛠️ HELPER FUNCTIONS
// ============================================================================

function escapeXml(string $text): string {
    return htmlspecialchars($text, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}

function formatDate(string $date): string {
    return date('Y-m-d', strtotime($date));
}

// ============================================================================
// 📄 GENERATE XML
// ============================================================================

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' .
     'xmlns:xhtml="http://www.w3.org/1999/xhtml" ' .
     'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" ' .
     'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">' . "\n";

// 1. Static pages
foreach (getStaticPages() as $page) {
    echo "  <url>\n";
    echo "    <loc>" . escapeXml($baseUrl . $page['path']) . "</loc>\n";
    echo "    <lastmod>" . formatDate(date('Y-m-d')) . "</lastmod>\n";
    echo "    <changefreq>" . escapeXml($page['changefreq']) . "</changefreq>\n";
    echo "    <priority>" . number_format($page['priority'], 1) . "</priority>\n";
    echo "  </url>\n";
}

// 2. Album pages
foreach (getAlbums() as $album) {
    $url = $baseUrl . '/?action=album&slug=' . urlencode($album['slug']);
    echo "  <url>\n";
    echo "    <loc>" . escapeXml($url) . "</loc>\n";
    echo "    <lastmod>" . formatDate($album['lastmod']) . "</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>" . number_format($prioritySection, 1) . "</priority>\n";
    
    // Optional: Add image sitemap extension
    echo "    <image:image>\n";
    echo "      <image:loc>" . escapeXml($baseUrl . '/images/albums/' . $album['slug'] . '.webp') . "</image:loc>\n";
    echo "      <image:caption>" . escapeXml($album['title']) . "</image:caption>\n";
    echo "    </image:image>\n";
    
    echo "  </url>\n";
}

// 3. Track pages
foreach (getTracks() as $track) {
    $url = $baseUrl . '/?action=track&track=' . $track['id'];
    echo "  <url>\n";
    echo "    <loc>" . escapeXml($url) . "</loc>\n";
    echo "    <lastmod>" . formatDate($track['lastmod']) . "</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>" . number_format($priorityContent, 1) . "</priority>\n";
    echo "  </url>\n";
}

echo "</urlset>\n";

// ============================================================================
// 📊 LOG GENERATION (optional)
// ============================================================================
// file_put_contents('/var/log/sitemap_generation.log', 
//     date('c') . " - Sitemap generated\n", FILE_APPEND);
?>