<?php
header("Content-Type: application/xml; charset=utf-8");
require_once './config/database.php';

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  <!-- Homepage -->
  <url>
    <loc>https://www.tumiza.ug/</loc>
    <lastmod><?= date('Y-m-d') ?></lastmod>
    <priority>1.00</priority>
    <changefreq>daily</changefreq>
  </url>

  <!-- Static Pages -->
  <url>
    <loc>https://www.tumiza.ug/about.php</loc>
    <lastmod><?= date('Y-m-d', strtotime('-30 days')) ?></lastmod>
    <priority>0.6</priority>
    <changefreq>monthly</changefreq>
  </url>

  <url>
    <loc>https://www.tumiza.ug/contact.php</loc>
    <lastmod><?= date('Y-m-d', strtotime('-30 days')) ?></lastmod>
    <priority>0.6</priority>
    <changefreq>monthly</changefreq>
  </url>

<?php
// Dynamic Product Pages
$stmt = $pdo->prepare("SELECT id, updated_at FROM products WHERE is_active = 1 ORDER BY updated_at DESC LIMIT 1000");
$stmt->execute();
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($products as $product):
    $url = 'https://www.tumiza.ug/product-details.php?id=' . $product['id'];
    $lastmod = date('Y-m-d', strtotime($product['updated_at']));
?>
  <url>
    <loc><?= $url ?></loc>
    <lastmod><?= $lastmod ?></lastmod>
    <priority>0.80</priority>
    <changefreq>weekly</changefreq>
  </url>
<?php endforeach; ?>

</urlset>
