<?php
require_once __DIR__ . '/includes/functions.php';
header('Content-Type: application/xml; charset=utf-8');

$db = getDB();
$url = rtrim(SITE_URL, '/');

$pagesRows = $db->query("SELECT slug, updated_at, og_image FROM pages")->fetchAll();
$pagesBySlug = [];
foreach ($pagesRows as $p) {
    $pagesBySlug[$p['slug']] = $p;
}

$staticPages = [
    '/'               => [1.0, 'weekly'],
    'about'           => [0.8, 'monthly'],
    'course-offline'  => [0.9, 'monthly'],
    'course-online'   => [0.9, 'monthly'],
    'recorded-classes'=> [0.9, 'monthly'],
    'fee-structure'   => [0.9, 'monthly'],
    'registration'    => [0.9, 'monthly'],
    'syllabus'        => [0.8, 'monthly'],
    'test-series'     => [0.8, 'monthly'],
    'study-material'  => [0.8, 'monthly'],
    'contact'         => [0.7, 'monthly'],
    'blog'            => [0.7, 'weekly'],
    'why-choose-us'   => [0.7, 'monthly'],
    'mission-vision'  => [0.7, 'monthly'],
    'admission-procedure' => [0.7, 'monthly'],
    'payment-options' => [0.7, 'monthly'],
    'top-scorers'     => [0.7, 'monthly'],
    'results'         => [0.7, 'monthly'],
    'pyq-papers'      => [0.7, 'monthly'],
    'paper-solutions' => [0.7, 'monthly'],
    'learning-resources' => [0.7, 'monthly'],
    'assignments'     => [0.7, 'monthly'],
    'demo-lectures'   => [0.7, 'monthly'],
    'faq'             => [0.7, 'monthly'],
    'video-gallery'   => [0.7, 'monthly'],
    'privacy-policy'  => [0.3, 'yearly'],
    'terms'           => [0.3, 'yearly'],
    'refund-policy'   => [0.3, 'yearly'],
];

$logoUrl = getSetting('logo_url', '/uploads/logo.png');

function pageLoc(string $slug): string {
    global $url;
    if ($slug === '/') return $url;
    if ($slug === 'about') return $url . '/about.php';
    return $url . '/pages/' . $slug . '.php';
}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xhtml="http://www.w3.org/1999/xhtml"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <?php foreach ($staticPages as $slug => [$priority, $changefreq]):
    $dbPage = $pagesBySlug[$slug] ?? null;
    $lastmod = $dbPage ? ($dbPage['updated_at'] ?? date('Y-m-d')) : date('Y-m-d');
    $ogImage = $dbPage ? ($dbPage['og_image'] ?? '') : '';
  ?>
  <url>
    <loc><?=e(pageLoc($slug))?></loc>
    <priority><?=$priority?></priority>
    <changefreq><?=$changefreq?></changefreq>
    <lastmod><?=e($lastmod)?></lastmod>
    <?php if ($slug === '/'): ?>
    <image:image>
      <image:loc><?=e($url . $logoUrl)?></image:loc>
      <image:title><?=e(SITE_NAME)?> Logo</image:title>
    </image:image>
    <?php endif; ?>
    <?php if ($ogImage): ?>
    <image:image>
      <image:loc><?=e(str_starts_with($ogImage, 'http') ? $ogImage : $url . $ogImage)?></image:loc>
    </image:image>
    <?php endif; ?>
  </url>
  <?php endforeach; ?>

  <?php foreach ($pagesRows as $p):
    if (isset($staticPages[$p['slug']])) continue;
  ?>
  <url>
    <loc><?=e(pageLoc($p['slug']))?></loc>
    <priority>0.7</priority>
    <changefreq>monthly</changefreq>
    <lastmod><?=e($p['updated_at'] ?? date('Y-m-d'))?></lastmod>
    <?php if ($p['og_image']): ?>
    <image:image>
      <image:loc><?=e(str_starts_with($p['og_image'], 'http') ? $p['og_image'] : $url . $p['og_image'])?></image:loc>
    </image:image>
    <?php endif; ?>
  </url>
  <?php endforeach; ?>

  <?php
  $posts = $db->query("SELECT slug, published_at, updated_at, featured_image FROM blog_posts WHERE status='published' ORDER BY published_at DESC")->fetchAll();
  foreach ($posts as $p):
  ?>
  <url>
    <loc><?=e($url . '/blog/' . $p['slug'])?></loc>
    <priority>0.5</priority>
    <changefreq>monthly</changefreq>
    <lastmod><?=e(($p['updated_at'] ?? $p['published_at']) ?: date('Y-m-d'))?></lastmod>
    <?php if ($p['featured_image']): ?>
    <image:image>
      <image:loc><?=e(str_starts_with($p['featured_image'], 'http') ? $p['featured_image'] : $url . $p['featured_image'])?></image:loc>
    </image:image>
    <?php endif; ?>
  </url>
  <?php endforeach; ?>
</urlset>
