<?php
header('Content-Type: application/xml; charset=utf-8');
require_once 'config.php';

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 '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';

// Games
$stmt = $pdo->query("SELECT slug, updated_at FROM games WHERE status = 'active' ORDER BY id DESC");
while ($row = $stmt->fetch()) {
    echo '<url>';
    echo '<loc>' . SITE_URL . '/play-online/game/' . htmlspecialchars($row['slug']) . '</loc>';
    echo '<lastmod>' . date('Y-m-d', strtotime($row['updated_at'])) . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

// Categories
$stmt = $pdo->query("SELECT slug FROM categories WHERE status = 'active'");
while ($row = $stmt->fetch()) {
    echo '<url>';
    echo '<loc>' . SITE_URL . '/' . htmlspecialchars($row['slug']) . '</loc>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.7</priority>';
    echo '</url>';
}

echo '</urlset>';
?>