<?xml version="1.0" encoding="UTF-8"?>
<?php
// Dynamic sitemap — served via PHP so blog posts are included
header('Content-Type: application/xml; charset=UTF-8');
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';
$base = SITE_URL;

$static = [
  ['loc' => '/',                 'priority' => '1.0', 'changefreq' => 'weekly'],
  ['loc' => '/services.php',     'priority' => '0.9', 'changefreq' => 'monthly'],
  ['loc' => '/how-it-works.php', 'priority' => '0.8', 'changefreq' => 'monthly'],
  ['loc' => '/for-practices.php','priority' => '0.8', 'changefreq' => 'monthly'],
  ['loc' => '/for-payers.php',   'priority' => '0.8', 'changefreq' => 'monthly'],
  ['loc' => '/contact.php',      'priority' => '0.7', 'changefreq' => 'yearly'],
  ['loc' => '/blog/',            'priority' => '0.8', 'changefreq' => 'weekly'],
];

$posts = db()->query("SELECT slug, updated_at FROM posts WHERE status = 'published' ORDER BY created_at DESC")->fetchAll();
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach($static as $s): ?>
  <url>
    <loc><?= htmlspecialchars($base . $s['loc'], ENT_XML1) ?></loc>
    <priority><?= $s['priority'] ?></priority>
    <changefreq><?= $s['changefreq'] ?></changefreq>
  </url>
<?php endforeach; ?>
<?php foreach($posts as $post): ?>
  <url>
    <loc><?= htmlspecialchars($base . '/blog/' . $post['slug'], ENT_XML1) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($post['updated_at'])) ?></lastmod>
    <changefreq>yearly</changefreq>
    <priority>0.6</priority>
  </url>
<?php endforeach; ?>
</urlset>
