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

$base = 'https://doomi.online';

// เพจคงที่
$static = [
  ['loc'=>"$base/",         'changefreq'=>'daily',   'priority'=>'1.0'],
  ['loc'=>"$base/cart",     'changefreq'=>'weekly',  'priority'=>'0.2'],
  ['loc'=>"$base/track",    'changefreq'=>'weekly',  'priority'=>'0.6'],
  ['loc'=>"$base/orders",   'changefreq'=>'weekly',  'priority'=>'0.1'],
];

// ดึงสินค้าล่าสุด (ปรับตาม schema DB ของคุณ)
$urls = $static;
$rows = dd_q("SELECT id, updated_at FROM products ORDER BY updated_at DESC LIMIT 2000")->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $r) {
  $urls[] = [
    'loc'        => "$base/product/{$r['id']}",
    'lastmod'    => date('c', strtotime($r['updated_at'] ?? 'now')),
    'changefreq' => 'weekly',
    'priority'   => '0.8'
  ];
}

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($urls as $u): ?>
  <url>
    <loc><?=htmlspecialchars($u['loc'], ENT_XML1)?></loc>
    <?php if(!empty($u['lastmod'])): ?><lastmod><?=$u['lastmod']?></lastmod><?php endif; ?>
    <changefreq><?=$u['changefreq']?></changefreq>
    <priority><?=$u['priority']?></priority>
  </url>
<?php endforeach; ?>
</urlset>
