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

$base  = 'https://eroticmassageprague1.cz';
$today = date('Y-m-d');

$pages = [
  ['loc' => '/',               'priority' => '1.0',  'changefreq' => 'daily'],
  ['loc' => '/masseuses.php',  'priority' => '0.9',  'changefreq' => 'weekly'],
  ['loc' => '/services.php',   'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/pricing.php',    'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/booking.php',    'priority' => '0.9',  'changefreq' => 'weekly'],
  ['loc' => '/reviews.php',    'priority' => '0.8',  'changefreq' => 'weekly'],
  ['loc' => '/blog.php',       'priority' => '0.8',  'changefreq' => 'weekly'],
  ['loc' => '/spaces.php',     'priority' => '0.7',  'changefreq' => 'monthly'],
  ['loc' => '/about.php',      'priority' => '0.6',  'changefreq' => 'monthly'],
  ['loc' => '/contact.php',    'priority' => '0.7',  'changefreq' => 'monthly'],
  ['loc' => '/massage-old-town-prague.php',         'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/massage-charles-bridge-prague.php',   'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/massage-wenceslas-square-prague.php', 'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/massage-prague-city-centre.php',      'priority' => '0.9',  'changefreq' => 'monthly'],
  ['loc' => '/masseuse-linda.php',  'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-alina.php',  'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-mia.php',    'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-sofia.php',  'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-emma.php',   'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-nina.php',   'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-laura.php',  'priority' => '0.8', 'changefreq' => 'weekly'],
  ['loc' => '/masseuse-kate.php',   'priority' => '0.8', 'changefreq' => 'weekly'],
];

/* ── Dynamically add published blog posts ───────────── */
try {
  $blog_posts = db()->query("SELECT slug, updated_at FROM blog_posts WHERE is_published=1 ORDER BY published_at DESC")->fetchAll();
  foreach ($blog_posts as $bp) {
    $pages[] = [
      'loc'        => '/blog.php?slug=' . urlencode($bp['slug']),
      'priority'   => '0.75',
      'changefreq' => 'monthly',
      'lastmod'    => $bp['updated_at'] ? date('Y-m-d', strtotime($bp['updated_at'])) : $today,
    ];
  }
} catch (Throwable $e) { /* DB not ready on first deploy — skip blog entries */ }
?>
<?= '<?xml version="1.0" encoding="UTF-8"?>' ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as $p):
  $lastmod = $p['lastmod'] ?? $today;
?>
  <url>
    <loc><?= $base . htmlspecialchars($p['loc']) ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <changefreq><?= $p['changefreq'] ?></changefreq>
    <priority><?= $p['priority'] ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
