<?php
// Unified sitemap for Google Search Console
require __DIR__.'/config.php';

header('Content-Type: application/xml; charset=utf-8');
header('X-Robots-Tag: noindex');

$base = 'https://parsdigital.net';
$date = date('Y-m-d');

try {
    $db = new PDO("mysql:host=localhost;port=3306;dbname=".DB_NAME.";charset=utf8mb4", DB_USER, DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->exec("SET NAMES utf8mb4");
} catch (PDOException $e) { die('DB error'); }

$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// 1. Main pages
$mainPages = [
    ['loc' => $base . '/', 'priority' => '1.0', 'freq' => 'weekly'],
    ['loc' => $base . '/auto/', 'priority' => '0.9', 'freq' => 'weekly'],
    ['loc' => $base . '/tech/', 'priority' => '0.9', 'freq' => 'weekly'],
    ['loc' => $base . '/shop/', 'priority' => '0.8', 'freq' => 'daily'],
    ['loc' => $base . '/product', 'priority' => '0.7', 'freq' => 'weekly'],
];
foreach ($mainPages as $p) {
    $xml .= '<url><loc>' . $p['loc'] . '</loc><lastmod>' . $date . '</lastmod><changefreq>' . $p['freq'] . '</changefreq><priority>' . $p['priority'] . '</priority></url>' . "\n";
}

// 2. Categories from DB
$CATEGORIES = ['هوشمند سازی و کنترل از وب و اینترنت', 'محصولات امنیتی', 'الکترونیک و الکتریک', 'نرم افزار و سخت افزار', 'محصولات مرتبط با خودرو'];
foreach ($CATEGORIES as $cat) {
    $slug = rawurlencode(trim(preg_replace('/\s+/u', '-', preg_replace('/[^\p{L}0-9\s-]/u', '', $cat))));
    $xml .= '<url><loc>' . $base . '/category/' . $slug . '/</loc><lastmod>' . $date . '</lastmod><changefreq>weekly</changefreq><priority>0.7</priority></url>' . "\n";
}

// 3. Brand + category combos
$brandCats = [
    'auto' => ['محصولات مرتبط با خودرو'],
    'tech' => ['هوشمند سازی و کنترل از وب و اینترنت', 'محصولات امنیتی', 'الکترونیک و الکتریک', 'نرم افزار و سخت افزار'],
];
foreach ($brandCats as $brand => $cats) {
    foreach ($cats as $cat) {
        $slug = rawurlencode(trim(preg_replace('/\s+/u', '-', preg_replace('/[^\p{L}0-9\s-]/u', '', $cat))));
        $xml .= '<url><loc>' . $base . '/' . $brand . '/' . $slug . '/</loc><lastmod>' . $date . '</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url>' . "\n";
    }
}

// 4. Products
try {
    $prods = $db->query("SELECT id,title,brand_key,stock,price,category FROM products ORDER BY id DESC");
    if ($prods) {
        foreach ($prods as $p) {
            $id = (int)$p['id'];
            $title = trim($p['title'] ?? '');
            if (!$id || !$title) continue;
            $slug = trim(preg_replace('/\s+/u', '-', preg_replace('/[^\p{L}0-9\s-]/u', '', $title)));
            $slug = mb_substr($slug, 0, 120);
            $url = $base . '/product/' . rawurlencode($slug) . '-' . $id;
            $priority = '0.8';
            if (($p['stock'] ?? 0) <= 0) $priority = '0.3';
            elseif (($p['price'] ?? 0) > 0) $priority = '0.9';
            $xml .= '<url><loc>' . $url . '</loc><lastmod>' . $date . '</lastmod><changefreq>weekly</changefreq><priority>' . $priority . '</priority></url>' . "\n";
        }
    }
} catch (Exception $e) {}

$xml .= '</urlset>';
header('Content-Length: ' . strlen($xml));
echo $xml;
