<?php
require_once __DIR__ . '/config/config.php';
header('Content-Type: application/xml; charset=utf-8');

$db = getDB();
$posts   = $db->query("SELECT slug, published_at FROM posts WHERE status='published' ORDER BY published_at DESC")->fetchAll();
$sectors = $db->query("SELECT slug, created_at FROM sectors WHERE is_active=1")->fetchAll();
$products= $db->query("SELECT slug, created_at FROM products WHERE is_active=1")->fetchAll();

$baseUrl = 'https://www.frisatogo.com';
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- Pages statiques -->
  <url><loc><?= $baseUrl ?>/</loc><changefreq>weekly</changefreq><priority>1.0</priority></url>
  <url><loc><?= $baseUrl ?>/pages/about.php</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
  <url><loc><?= $baseUrl ?>/pages/sectors.php</loc><changefreq>weekly</changefreq><priority>0.9</priority></url>
  <url><loc><?= $baseUrl ?>/pages/blog.php</loc><changefreq>daily</changefreq><priority>0.8</priority></url>
  <url><loc><?= $baseUrl ?>/pages/shop.php</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
  <url><loc><?= $baseUrl ?>/pages/contact.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
  <url><loc><?= $baseUrl ?>/pages/impact.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
  <url><loc><?= $baseUrl ?>/pages/appointment.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>

  <!-- Secteurs -->
  <?php foreach ($sectors as $s): ?>
  <url>
    <loc><?= $baseUrl ?>/pages/sector.php?slug=<?= urlencode($s['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($s['created_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <?php endforeach; ?>

  <!-- Articles -->
  <?php foreach ($posts as $p): ?>
  <url>
    <loc><?= $baseUrl ?>/pages/post.php?slug=<?= urlencode($p['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($p['published_at'])) ?></lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.7</priority>
  </url>
  <?php endforeach; ?>

  <!-- Produits -->
  <?php foreach ($products as $p): ?>
  <url>
    <loc><?= $baseUrl ?>/pages/product.php?slug=<?= urlencode($p['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($p['created_at'])) ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
  <?php endforeach; ?>

</urlset>