<?php
header('Content-Type: application/xml; charset=utf-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

// جلب جميع المنتجات من قاعدة البيانات
$stmt = $pdo->query("SELECT id, slug, updated_at FROM products WHERE active = 1");
$products = $stmt->fetchAll();
foreach ($products as $p) {
    $slug = !empty($p['slug']) ? $p['slug'] : 'product?id='.$p['id'];
    echo '<url>';
    echo '<loc>https://sbrtradimp.com/product/'.$slug.'</loc>';
    echo '<lastmod>'.date('Y-m-d', strtotime($p['updated_at'])).'</lastmod>';
    echo '<priority>0.8</priority>';
    echo '</url>';
}

// جلب الأخبار
$stmt = $pdo->query("SELECT id, created_at FROM news ORDER BY created_at DESC");
$news = $stmt->fetchAll();
foreach ($news as $n) {
    echo '<url>';
    echo '<loc>https://sbrtradimp.com/news/'.$n['id'].'</loc>';
    echo '<lastmod>'.date('Y-m-d', strtotime($n['created_at'])).'</lastmod>';
    echo '<priority>0.6</priority>';
    echo '</url>';
}

// الصفحات الثابتة
$static = ['', 'about.html', 'products.php', '#contact'];
foreach ($static as $page) {
    echo '<url><loc>https://sbrtradimp.com/'.$page.'</loc><priority>0.5</priority></url>';
}
echo '</urlset>';
?>