<?php
header('Content-Type: application/xml');
require_once 'config.php';
require_once 'includes/database.php';

$pdo = getDB();

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// Homepage
echo '<url>';
echo '<loc>' . SITE_URL . '/</loc>';
echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
echo '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';

// Static Pages
$pages = ['about', 'blog', 'disclaimer', 'terms', 'privacy', 'contact'];
foreach ($pages as $page) {
    echo '<url>';
    echo '<loc>' . SITE_URL . '/' . $page . '</loc>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '<changefreq>monthly</changefreq>';
    echo '<priority>0.7</priority>';
    echo '</url>';
}

// Categories
$categories = ['latest-news', 'highlights', 'match-fixtures'];
foreach ($categories as $cat) {
    echo '<url>';
    echo '<loc>' . SITE_URL . '/category/' . $cat . '</loc>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '<changefreq>daily</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

// News Articles
$stmt = $pdo->query("SELECT slug, updated_at FROM news ORDER BY updated_at DESC LIMIT 1000");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<url>';
    echo '<loc>' . SITE_URL . '/news/' . $row['slug'] . '</loc>';
    echo '<lastmod>' . date('Y-m-d', strtotime($row['updated_at'])) . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.6</priority>';
    echo '</url>';
}

echo '</urlset>';
?>