<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <!-- Homepage -->
   <url>
      <loc>https://sportzlive.in/</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>1.0</priority>
   </url>

   <!-- Events Listing (KEEP if no clean /events page exists) -->
   <url>
      <loc>https://sportzlive.in/layouts/events_details.php?page=1&amp;type=all</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>0.9</priority>
   </url>

   <!-- Static Pages -->
   <url>
      <loc>https://sportzlive.in/training.php</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>0.7</priority>
   </url>

   <url>
      <loc>https://sportzlive.in/about_us.php</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>0.6</priority>
   </url>

   <url>
      <loc>https://sportzlive.in/privacy_policy.php</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>0.5</priority>
   </url>

   <url>
      <loc>https://sportzlive.in/refund_policy.php</loc>
      <lastmod><?php echo date('Y-m-d'); ?></lastmod>
      <priority>0.5</priority>
   </url>

   <!-- 🔥 Dynamic Event URLs -->
   <?php
   require_once("/apis/db.php");

   $events = DB::raw("SELECT slug, created_at FROM events WHERE is_active = 1");

   if($events){
      foreach($events as $event){
   ?>
      <url>
         <loc>https://sportzlive.in/events/<?php echo htmlspecialchars($event['slug']); ?></loc>
         <lastmod>
            <?php 
               echo !empty($event['created_at']) 
               ? date('Y-m-d', strtotime($event['created_at'])) 
               : date('Y-m-d'); 
            ?>
         </lastmod>
         <priority>0.8</priority>
      </url>
   <?php 
      }
   }
   ?>

</urlset>