<?php
header('Content-Type: application/xml; charset=utf-8');

require_once 'config/config.php';

// Get base URL
$baseUrl = getBaseUrl();

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// Home page
echo "  <url>\n";
echo "    <loc>{$baseUrl}/</loc>\n";
echo "    <changefreq>daily</changefreq>\n";
echo "    <priority>1.0</priority>\n";
echo "  </url>\n";

// Main pages
$mainPages = [
    '/towns.php' => ['Towns', 'weekly', '0.9'],
    '/businesses.php' => ['Businesses', 'daily', '0.9'],
    '/events.php' => ['Events', 'daily', '0.8'],
    '/jobs.php' => ['Jobs', 'daily', '0.8'],
];

foreach ($mainPages as $url => $info) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}{$url}</loc>\n";
    echo "    <changefreq>{$info[1]}</changefreq>\n";
    echo "    <priority>{$info[2]}</priority>\n";
    echo "  </url>\n";
}

// Support pages
$supportPages = $db->fetchAll("SELECT slug FROM support_pages WHERE status = 'active'");
foreach ($supportPages as $page) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}/support/?page={$page['slug']}</loc>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>0.5</priority>\n";
    echo "  </url>\n";
}

// Towns
$towns = $db->fetchAll("SELECT slug, updated_at FROM towns ORDER BY name ASC");
foreach ($towns as $town) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}/town/{$town['slug']}</loc>\n";
    echo "    <lastmod>" . date('Y-m-d', strtotime($town['updated_at'])) . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.8</priority>\n";
    echo "  </url>\n";
}

// Active businesses
$businesses = $db->fetchAll("SELECT b.slug, t.slug as town_slug, b.updated_at 
                            FROM businesses b 
                            JOIN towns t ON b.town_id = t.id 
                            WHERE b.status = 'approved' 
                            ORDER BY b.updated_at DESC 
                            LIMIT 1000");
foreach ($businesses as $business) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}/business/{$business['town_slug']}/{$business['slug']}</loc>\n";
    echo "    <lastmod>" . date('Y-m-d', strtotime($business['updated_at'])) . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
}

// Active events
$events = $db->fetchAll("SELECT e.slug, t.slug as town_slug, e.updated_at 
                        FROM events e 
                        JOIN towns t ON e.town_id = t.id 
                        WHERE e.status = 'approved' AND e.end_date >= CURDATE()
                        ORDER BY e.start_date ASC 
                        LIMIT 500");
foreach ($events as $event) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}/event/{$event['town_slug']}/{$event['slug']}</loc>\n";
    echo "    <lastmod>" . date('Y-m-d', strtotime($event['updated_at'])) . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.6</priority>\n";
    echo "  </url>\n";
}

// Active jobs
$jobs = $db->fetchAll("SELECT j.slug, t.slug as town_slug, j.updated_at 
                      FROM jobs j 
                      JOIN towns t ON j.town_id = t.id 
                      WHERE j.status = 'approved' AND j.expires_at >= NOW()
                      ORDER BY j.created_at DESC 
                      LIMIT 500");
foreach ($jobs as $job) {
    echo "  <url>\n";
    echo "    <loc>{$baseUrl}/job/{$job['town_slug']}/{$job['slug']}</loc>\n";
    echo "    <lastmod>" . date('Y-m-d', strtotime($job['updated_at'])) . "</lastmod>\n";
    echo "    <changefreq>daily</changefreq>\n";
    echo "    <priority>0.6</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>';
?> 