<?php
// Upload to beriya.latestechn.com/ root (not api folder)
// Google visits: beriya.latestechn.com/sitemap.php

require_once 'api/config.php';
$db = getDB();
$db->set_charset('utf8mb4');

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$base = 'https://beriya.latestechn.com';

// Static pages
$static = ['', '/books', '/courses', '/bible', '/upload'];
foreach ($static as $path) {
    echo "<url><loc>$base$path</loc><priority>1.0</priority><changefreq>daily</changefreq></url>";
}

// All approved books
$r = $db->query("SELECT id, title FROM books WHERE approved=1");
while ($row = $r->fetch_assoc()) {
    $id    = $row['id'];
    $title = htmlspecialchars($row['title']);
    echo "<url><loc>$base/book/$id</loc><priority>0.8</priority><changefreq>weekly</changefreq></url>";
}

// All approved courses
$r = $db->query("SELECT id, title FROM courses WHERE approved=1");
while ($row = $r->fetch_assoc()) {
    $id = $row['id'];
    echo "<url><loc>$base/course/$id</loc><priority>0.8</priority><changefreq>weekly</changefreq></url>";
}

// Bible chapters that have verses
$r = $db->query("SELECT DISTINCT book_id, chapter FROM bible_verses WHERE language_code='eng' ORDER BY book_id, chapter");
while ($row = $r->fetch_assoc()) {
    $book = $row['book_id'];
    $ch   = $row['chapter'];
    echo "<url><loc>$base/bible/$book/$ch</loc><priority>0.7</priority><changefreq>monthly</changefreq></url>";
}

echo '</urlset>';
$db->close();
?>