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

$base_url = "https://".$_SERVER['HTTP_HOST'];

// 🎯 Database Connection for Sitemap
try {
    $db = new PDO("mysql:host=localhost;dbname=4umatka", "root", "");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("");
}
 
// 🔥 Static Pages (Jo fix rahenge - Homepage hata diya gaya hai)
$pages = [
    "/passing-record/all/" => "daily"
];

// 🔥 Dynamic Pages (Auto Detect from Database)
$stmt = $db->query("SELECT title FROM games WHERE is_active = 1 ORDER BY title ASC");
$active_markets = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($active_markets as $market) {
    // URL slug generate karna (e.g., "Milan Morning" -> "milan-morning")
    $slug = strtolower(str_replace(' ', '-', $market['title']));
    $dynamic_url = "/passing-record/all/" . $slug;
    
    // Array me add karna (Hourly frequency ke sath)
    $pages[$dynamic_url] = "hourly";
}

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

<?php foreach($pages as $page_url => $freq): ?>
    <url>
        <loc><?= htmlspecialchars($base_url . $page_url) ?></loc>
        <lastmod><?= date("Y-m-d") ?></lastmod>
        <changefreq><?= $freq ?></changefreq>
        <priority><?= ($page_url == "/passing-record/all/all") ? "1.0" : "0.8" ?></priority>
    </url>
<?php endforeach; ?>

</urlset>