<?php
/**
 * Sitemap الكامل لموقع ikhwantais.com
 * يُولّد ديناميكياً من قاعدة البيانات + صفحات ثابتة
 *
 * يفتح من: https://ikhwantais.com/sitemap.php
 */

define('ACCESS_ALLOWED', true);
require_once __DIR__ . '/includes/config.php';

// فرض index, follow
header_remove('X-Robots-Tag');
header('X-Robots-Tag: index, follow, max-image-preview:large');
header('Content-Type: application/xml; charset=utf-8');

$base = rtrim(SITE_URL, '/');
$urls = [];

function add_url(&$urls, $loc, $lastmod = null, $priority = '0.8', $changefreq = 'weekly') {
    $urls[] = [
        'loc'        => $loc,
        'lastmod'    => $lastmod ?: date('Y-m-d'),
        'priority'   => $priority,
        'changefreq' => $changefreq,
    ];
}

// الرئيسية + اللغات
add_url($urls, $base . '/',         date('Y-m-d'), '1.0', 'daily');
add_url($urls, $base . '/?lang=ar', date('Y-m-d'), '0.9', 'daily');
add_url($urls, $base . '/?lang=en', date('Y-m-d'), '0.9', 'daily');
add_url($urls, $base . '/?lang=zh', date('Y-m-d'), '0.9', 'daily');

// صفحات ثابتة
add_url($urls, $base . '/about.php',                 date('Y-m-d'), '0.9', 'monthly');
add_url($urls, $base . '/contact.php',               date('Y-m-d'), '0.9', 'monthly');
add_url($urls, $base . '/news.php',                  date('Y-m-d'), '0.9', 'daily');
add_url($urls, $base . '/pages/services.php',        date('Y-m-d'), '0.8', 'monthly');
add_url($urls, $base . '/pages/products_groups.php', date('Y-m-d'), '0.9', 'weekly');
add_url($urls, $base . '/pages/projects_type.php',   date('Y-m-d'), '0.8', 'weekly');
add_url($urls, $base . '/pages/gallerys.php',        date('Y-m-d'), '0.7', 'monthly');
add_url($urls, $base . '/quote.php',                 date('Y-m-d'), '0.7', 'monthly');

// المنتجات من قاعدة البيانات
try {
    $products = db_all('products', ['status' => 1], 'updated_at DESC', 500);
    foreach ($products as $p) {
        $url = !empty($p['slug'])
            ? $base . '/pages/product.php?slug=' . urlencode($p['slug'])
            : $base . '/pages/product.php?id=' . (int)$p['id'];
        $lastmod = !empty($p['updated_at']) ? date('Y-m-d', strtotime($p['updated_at'])) : null;
        add_url($urls, $url, $lastmod, '0.7', 'weekly');
    }
} catch (Exception $e) {}

// المشاريع
try {
    $projects = db_all('projects', ['status' => 1], 'updated_at DESC', 200);
    foreach ($projects as $p) {
        $url = !empty($p['slug'])
            ? $base . '/pages/project.php?slug=' . urlencode($p['slug'])
            : $base . '/pages/project.php?id=' . (int)$p['id'];
        $lastmod = !empty($p['updated_at']) ? date('Y-m-d', strtotime($p['updated_at'])) : null;
        add_url($urls, $url, $lastmod, '0.8', 'monthly');
    }
} catch (Exception $e) {}

// الأخبار
try {
    $news = db_all('news', ['status' => 1], 'published_at DESC', 100);
    foreach ($news as $n) {
        $url = $base . '/news.php?id=' . (int)$n['id'];
        $lastmod = !empty($n['updated_at']) ? date('Y-m-d', strtotime($n['updated_at'])) : null;
        add_url($urls, $url, $lastmod, '0.6', 'monthly');
    }
} catch (Exception $e) {}

// الإخراج
$out  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$out .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach ($urls as $u) {
    $out .= "  <url>\n";
    $out .= "    <loc>"        . htmlspecialchars($u['loc'], ENT_XML1, 'UTF-8') . "</loc>\n";
    $out .= "    <lastmod>"    . $u['lastmod']    . "</lastmod>\n";
    $out .= "    <changefreq>" . $u['changefreq'] . "</changefreq>\n";
    $out .= "    <priority>"   . $u['priority']   . "</priority>\n";
    $out .= "  </url>\n";
}
$out .= '</urlset>';

if (ob_get_level() > 0) { ob_end_clean(); }
echo $out;
