<?php
/**
 * sitemap.xml — Auto-generated XML Sitemap
 * Place this file in your project ROOT (same level as index.php)
 * Access: https://vbhv.site/sitemap.xml
 */
require_once 'admin/includes/config.php';

$baseUrl  = rtrim(SITE_PUBLIC_URL, '/');
$today    = date('Y-m-d');

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

  <!-- ── Static Pages ──────────────────────── -->
  <url>
    <loc><?= $baseUrl ?>/index.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>1.0</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/courses.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>0.9</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/blog.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/others.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/about.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/contact.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/disclaimer.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.3</priority>
  </url>
  <url>
    <loc><?= $baseUrl ?>/privacy.php</loc>
    <lastmod><?= $today ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.3</priority>
  </url>

<?php
$db = db();

// ── Dynamic Course Pages ──────────────────────
try {
  $courses = $db->query(
    "SELECT slug, banner_image, title, updated_at
     FROM courses WHERE is_active=1 ORDER BY updated_at DESC"
  )->fetchAll();

  foreach ($courses as $c):
    $lastmod = date('Y-m-d', strtotime($c['updated_at']));
    $url     = $baseUrl . '/course-detail.php?slug=' . rawurlencode($c['slug']);
?>
  <url>
    <loc><?= htmlspecialchars($url) ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
    <?php if ($c['banner_image']): ?>
    <image:image>
      <image:loc><?= htmlspecialchars($baseUrl . '/admin/' . ltrim($c['banner_image'], '/')) ?></image:loc>
      <image:title><?= htmlspecialchars($c['title']) ?></image:title>
    </image:image>
    <?php endif; ?>
  </url>
<?php endforeach; } catch (Exception $e) { /* silently skip if DB unavailable */ } ?>

<?php
// ── Dynamic Blog Pages ────────────────────────
try {
  $blogs = $db->query(
    "SELECT slug, banner_image, title, updated_at
     FROM blog_posts WHERE is_active=1 ORDER BY updated_at DESC"
  )->fetchAll();

  foreach ($blogs as $b):
    $lastmod = date('Y-m-d', strtotime($b['updated_at']));
    $url     = $baseUrl . '/blog-detail.php?id=' . rawurlencode($b['slug']);
?>
  <url>
    <loc><?= htmlspecialchars($url) ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
    <?php if ($b['banner_image']): ?>
    <image:image>
      <image:loc><?= htmlspecialchars($baseUrl . '/admin/' . ltrim($b['banner_image'], '/')) ?></image:loc>
      <image:title><?= htmlspecialchars($b['title']) ?></image:title>
    </image:image>
    <?php endif; ?>
  </url>
<?php endforeach; } catch (Exception $e) { /* silently skip */ } ?>

<?php
try {
  $others = $db->query(
    "SELECT slug, banner_image, title, updated_at
     FROM others WHERE is_active=1 ORDER BY updated_at DESC"
  )->fetchAll();

  foreach ($others as $o):
    $lastmod = date('Y-m-d', strtotime($o['updated_at']));
    $url     = $baseUrl . '/other-detail.php?slug=' . rawurlencode($o['slug']);
?>
  <url>
    <loc><?= htmlspecialchars($url) ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
    <?php if ($o['banner_image']): ?>
    <image:image>
      <image:loc><?= htmlspecialchars($baseUrl . '/admin/' . ltrim($o['banner_image'], '/')) ?></image:loc>
      <image:title><?= htmlspecialchars($o['title']) ?></image:title>
    </image:image>
    <?php endif; ?>
  </url>
<?php endforeach; } catch (Exception $e) { /* silently skip */ } ?>

</urlset>
