<?php
require_once __DIR__ . '/includes/bootstrap.php';
header('Content-Type: application/xml; charset=utf-8');
$baseUrl = 'http://www.hubachiropractics.or.tz';
$pages = [
    ['loc' => '/', 'priority' => '1.0', 'freq' => 'weekly'],
    ['loc' => '/about.php', 'priority' => '0.9', 'freq' => 'monthly'],
    ['loc' => '/services.php', 'priority' => '0.9', 'freq' => 'weekly'],
    ['loc' => '/assessments.php', 'priority' => '0.8', 'freq' => 'monthly'],
    ['loc' => '/team.php', 'priority' => '0.7', 'freq' => 'monthly'],
    ['loc' => '/wellness.php', 'priority' => '0.7', 'freq' => 'monthly'],
    ['loc' => '/facilities.php', 'priority' => '0.6', 'freq' => 'monthly'],
    ['loc' => '/contact.php', 'priority' => '0.8', 'freq' => 'monthly'],
    ['loc' => '/blog.php', 'priority' => '0.7', 'freq' => 'weekly'],
    ['loc' => '/faqs.php', 'priority' => '0.6', 'freq' => 'monthly'],
    ['loc' => '/gallery.php', 'priority' => '0.5', 'freq' => 'monthly'],
    ['loc' => '/book-appointment.php', 'priority' => '0.9', 'freq' => 'monthly'],
    ['loc' => '/privacy-policy.php', 'priority' => '0.3', 'freq' => 'yearly'],
    ['loc' => '/terms.php', 'priority' => '0.3', 'freq' => 'yearly'],
    ['loc' => '/medical-disclaimer.php', 'priority' => '0.3', 'freq' => 'yearly'],
];
$db = Database::getInstance();
$services = $db->fetchAll("SELECT id, slug FROM services WHERE is_active = 1");
$blogPosts = $db->fetchAll("SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY published_at DESC LIMIT 50");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $page): ?>
  <url>
    <loc><?= e($baseUrl . $page['loc']) ?></loc>
    <changefreq><?= $page['freq'] ?></changefreq>
    <priority><?= $page['priority'] ?></priority>
  </url>
<?php endforeach; ?>
<?php foreach ($services as $svc): ?>
  <url>
    <loc><?= e($baseUrl . '/service.php?id=' . $svc['id']) ?></loc>
    <changefreq>monthly</changefreq>
    <priority>0.6</priority>
  </url>
<?php endforeach; ?>
<?php foreach ($blogPosts as $bp): ?>
  <url>
    <loc><?= e($baseUrl . '/blog-post.php?slug=' . $bp['slug']) ?></loc>
    <lastmod><?= e($bp['updated_at'] ?? date('Y-m-d')) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.5</priority>
  </url>
<?php endforeach; ?>
</urlset>
