<?php
/**
 * Dynamic sitemap.xml generator
 * Access at: /sitemap.xml (via .htaccess rewrite or directly at /sitemap.php)
 */
require_once 'config/config.php';
require_once 'includes/db.php';
header('Content-Type: application/xml; charset=utf-8');

$products   = Database::fetchAll("SELECT slug, updated_at FROM products WHERE is_active=1");
$categories = Database::fetchAll("SELECT slug FROM categories WHERE is_active=1");

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url><loc><?= SITE_URL ?>/</loc><changefreq>daily</changefreq><priority>1.0</priority></url>
  <url><loc><?= SITE_URL ?>/shop.php</loc><changefreq>daily</changefreq><priority>0.9</priority></url>
  <url><loc><?= SITE_URL ?>/services.php</loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
  <url><loc><?= SITE_URL ?>/industries.php</loc><changefreq>weekly</changefreq><priority>0.7</priority></url>
  <url><loc><?= SITE_URL ?>/about.php</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
  <url><loc><?= SITE_URL ?>/gallery.php</loc><changefreq>weekly</changefreq><priority>0.6</priority></url>
  <url><loc><?= SITE_URL ?>/contact.php</loc><changefreq>monthly</changefreq><priority>0.7</priority></url>
  <url><loc><?= SITE_URL ?>/repair-request.php</loc><changefreq>monthly</changefreq><priority>0.6</priority></url>
  <?php foreach ($categories as $cat): ?>
  <url><loc><?= SITE_URL ?>/shop.php?category=<?= urlencode($cat['slug']) ?></loc><changefreq>weekly</changefreq><priority>0.8</priority></url>
  <?php endforeach; ?>
  <?php foreach ($products as $p): ?>
  <url>
    <loc><?= SITE_URL ?>/product-detail.php?slug=<?= urlencode($p['slug']) ?></loc>
    <lastmod><?= date('Y-m-d', strtotime($p['updated_at'])) ?></lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
  <?php endforeach; ?>
</urlset>
