<?php
header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: max-age=86400'); // Cache for 24 hours

require 'db.php';

// Start XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">

<?php
$baseUrl = 'https://experiencellandudno.com';
$pages = [];

// Static pages (highest priority and frequency)
$staticPages = [
    '/' => ['priority' => '1.0', 'changefreq' => 'weekly'],
    '/tours.php' => ['priority' => '0.9', 'changefreq' => 'weekly'],
    '/whatson.php' => ['priority' => '0.9', 'changefreq' => 'daily'],
    '/about.php' => ['priority' => '0.7', 'changefreq' => 'monthly'],
    '/contact.php' => ['priority' => '0.7', 'changefreq' => 'monthly'],
    '/faq.php' => ['priority' => '0.6', 'changefreq' => 'monthly'],
];

foreach ($staticPages as $page => $config) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . $page) . "</loc>\n";
    echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "    <priority>" . $config['priority'] . "</priority>\n";
    echo "    <changefreq>" . $config['changefreq'] . "</changefreq>\n";
    echo "  </url>\n";
}

// Dynamic tours
try {
    $stmt = $pdo->query("SELECT id, created_at, updated_at, main_image FROM tours WHERE is_active = 1 ORDER BY updated_at DESC");
    $tours = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($tours as $tour) {
        $lastMod = $tour['updated_at'] ?? $tour['created_at'] ?? date('Y-m-d');
        if (strpos($lastMod, ' ') !== false) {
            $lastMod = substr($lastMod, 0, 10);
        }
        
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($baseUrl . '/tour.php?id=' . $tour['id']) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($lastMod) . "</lastmod>\n";
        echo "    <priority>0.8</priority>\n";
        echo "    <changefreq>weekly</changefreq>\n";
        
        if ($tour['main_image']) {
            echo "    <image:image>\n";
            echo "      <image:loc>" . htmlspecialchars($baseUrl . '/uploads/' . $tour['main_image']) . "</image:loc>\n";
            echo "    </image:image>\n";
        }
        
        echo "  </url>\n";
    }
} catch (Exception $e) {
    // Log error but continue
}

// Dynamic events
try {
    $stmt = $pdo->query("SELECT id, start_date, created_at, updated_at, image FROM events WHERE status = 'published' ORDER BY updated_at DESC LIMIT 500");
    $events = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($events as $event) {
        $lastMod = $event['updated_at'] ?? $event['created_at'] ?? date('Y-m-d');
        if (strpos($lastMod, ' ') !== false) {
            $lastMod = substr($lastMod, 0, 10);
        }
        
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($baseUrl . '/event.php?id=' . $event['id']) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($lastMod) . "</lastmod>\n";
        echo "    <priority>0.7</priority>\n";
        echo "    <changefreq>daily</changefreq>\n";
        
        if ($event['image']) {
            echo "    <image:image>\n";
            echo "      <image:loc>" . htmlspecialchars($baseUrl . '/uploads/' . $event['image']) . "</image:loc>\n";
            echo "    </image:image>\n";
        }
        
        echo "  </url>\n";
    }
} catch (Exception $e) {
    // Log error but continue
}
?>
</urlset>
