<?php
// sitemap.php — dynamic XML sitemap for supermindeducation.com
// Requires: dp.php (must set $conn = new mysqli(...))
// Output: XML (UTF-8), gzip-friendly, safe if DB is down

header('Content-Type: application/xml; charset=UTF-8');
@ini_set('display_errors', 0);
@ini_set('zlib.output_compression', 0);
@ini_set('default_charset', 'UTF-8');

require_once __DIR__ . '/dp.php'; // mysqli $conn

// ---------- Helpers ----------
function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
function isoDate($ts = null){
  if (!$ts) return date('Y-m-d');
  if (ctype_digit((string)$ts)) return date('Y-m-d', (int)$ts);
  $t = strtotime($ts);
  return $t ? date('Y-m-d', $t) : date('Y-m-d');
}
function base_url(){
  $https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || ($_SERVER['SERVER_PORT'] ?? '') == 443;
  $scheme = $https ? 'https://' : 'http://';
  $host = $_SERVER['HTTP_HOST'] ?? 'supermindeducation.com';
  return rtrim($scheme . $host, '/');
}
$BASE = base_url();

// ---------- Static pages (add any others you want indexed) ----------
$static = [
  ['/',                          '1.0'],
  ['/register.php',              '0.9'],
  ['/student_login.php',         '0.9'],
  ['/login.php',                 '0.9'],   // teacher login
  ['/student_dashboard.php',     '0.6'],
  ['/teacher_dashboard.php',     '0.6'],
  ['/news.php',                  '0.8'],
  ['/contact.php',               '0.7'],
  ['/about.php',                 '0.6'],

  // ✅ APK download page
  ['/download.php',          '0.8'],
];

// ---------- Start XML ----------
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php
// Output static URLs
$today = isoDate();
foreach ($static as [$path, $priority]){
  echo "  <url>\n";
  echo "    <loc>" . h($BASE . $path) . "</loc>\n";
  echo "    <lastmod>{$today}</lastmod>\n";
  echo "    <priority>{$priority}</priority>\n";
  echo "  </url>\n";
}

// ---------- Dynamic: Lessons (watch pages) ----------
try {
  // If your lessons table/columns differ, adjust the query/column names.
  // Common patterns in your project: lessons(id, updated_at|created_at, is_public|is_paid, deleted_at)
  $sql = "SELECT id, 
                 COALESCE(updated_at, created_at, NOW()) AS mtime
          FROM lessons
          WHERE (deleted_at IS NULL OR deleted_at = '0000-00-00 00:00:00')
          ORDER BY id DESC
          LIMIT 50000";
  if ($res = $conn->query($sql)) {
    while ($row = $res->fetch_assoc()) {
      $loc  = $BASE . '/watch.php?id=' . (int)$row['id'];
      $date = isoDate($row['mtime']);
      echo "  <url>\n";
      echo "    <loc>" . h($loc) . "</loc>\n";
      echo "    <lastmod>{$date}</lastmod>\n";
      echo "    <priority>0.8</priority>\n";
      echo "  </url>\n";
    }
    $res->free();
  }
} catch (Throwable $e) {
  // If the table doesn't exist yet, just skip silently.
}

// ---------- Dynamic: News posts ----------
try {
  // If your table is named differently (e.g., announcements), change here.
  // Expected columns: id, updated_at/created_at
  $sql = "SELECT id, COALESCE(updated_at, created_at, NOW()) AS mtime
          FROM news
          WHERE (is_deleted IS NULL OR is_deleted = 0)
          ORDER BY id DESC
          LIMIT 50000";
  if ($res = $conn->query($sql)) {
    while ($row = $res->fetch_assoc()) {
      $loc  = $BASE . '/news.php?id=' . (int)$row['id'];
      $date = isoDate($row['mtime']);
      echo "  <url>\n";
      echo "    <loc>" . h($loc) . "</loc>\n";
      echo "    <lastmod>{$date}</lastmod>\n";
      echo "    <priority>0.7</priority>\n";
      echo "  </url>\n";
    }
    $res->free();
  }
} catch (Throwable $e) {
  // Skip if table not present
}

// ---------- Dynamic: Announcements (optional) ----------
try {
  $sql = "SELECT id, COALESCE(updated_at, created_at, NOW()) AS mtime
          FROM announcements
          ORDER BY id DESC
          LIMIT 50000";
  if ($res = $conn->query($sql)) {
    while ($row = $res->fetch_assoc()) {
      $loc  = $BASE . '/announcement.php?id=' . (int)$row['id'];
      $date = isoDate($row['mtime']);
      echo "  <url>\n";
      echo "    <loc>" . h($loc) . "</loc>\n";
      echo "    <lastmod>{$date}</lastmod>\n";
      echo "    <priority>0.6</priority>\n";
      echo "  </url>\n";
    }
    $res->free();
  }
} catch (Throwable $e) {
  // Skip if table not present
}
?>
</urlset>