<?php
// Dynamic sitemap for Google Search Console
require_once __DIR__ . '/includes/db.php';

$db   = db();
$site = 'https://meenakshikumkum.com';
$now  = date('Y-m-d');

$products   = $db->query("SELECT id, slug FROM products WHERE is_active=1")->fetchAll(PDO::FETCH_ASSOC);
$categories = $db->query("SELECT slug FROM categories")->fetchAll(PDO::FETCH_ASSOC);

header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

  <!-- Static pages -->
  <url><loc><?= $site ?>/</loc><changefreq>daily</changefreq><priority>1.0</priority><lastmod><?= $now ?></lastmod></url>
  <url><loc><?= $site ?>/categories.php</loc><changefreq>weekly</changefreq><priority>0.8</priority><lastmod><?= $now ?></lastmod></url>
  <url><loc><?= $site ?>/about.php</loc><changefreq>monthly</changefreq><priority>0.5</priority></url>
  <url><loc><?= $site ?>/contact.php</loc><changefreq>monthly</changefreq><priority>0.5</priority></url>

  <!-- Category pages -->
<?php foreach ($categories as $cat): ?>
  <url>
    <loc><?= $site ?>/category.php?slug=<?= urlencode($cat['slug']) ?></loc>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
    <lastmod><?= $now ?></lastmod>
  </url>
<?php endforeach; ?>

  <!-- Product pages -->
<?php foreach ($products as $p):
    $loc = $site . '/product.php?id=' . $p['id'];
    if (!empty($p['slug'])) $loc = $site . '/product.php?slug=' . urlencode($p['slug']);
?>
  <url>
    <loc><?= $loc ?></loc>
    <changefreq>weekly</changefreq>
    <priority>0.9</priority>
    <lastmod><?= $now ?></lastmod>
  </url>
<?php endforeach; ?>
</urlset>
