<?php
require_once 'config/database.php';

$database = new Database();
$db = $database->getConnection();

// Get all branches
$branches_query = "SELECT id FROM branches WHERE is_active = 1";
$branches_stmt = $db->prepare($branches_query);
$branches_stmt->execute();
$all_branches = $branches_stmt->fetchAll(PDO::FETCH_COLUMN);

echo "<h2>تحديث بيانات الخدمات</h2>";
echo "<p>عدد الفروع المتاحة: " . count($all_branches) . "</p>";
echo "<hr>";

// Get all services
$services_query = "SELECT id, name, category, price FROM beauty_services";
$services_stmt = $db->prepare($services_query);
$services_stmt->execute();
$services = $services_stmt->fetchAll(PDO::FETCH_ASSOC);

$success_count = 0;
$error_count = 0;

foreach ($services as $service) {
    try {
        // تحديد المخزون حسب نوع الخدمة
        $is_limited_stock = 0;
        $stock_quantity = 0;
        
        // الخدمات التي لها مخزون محدود (المنتجات)
        if (stripos($service['name'], 'بروتين') !== false || 
            stripos($service['name'], 'كفيار') !== false ||
            stripos($service['name'], 'صبغة') !== false) {
            $is_limited_stock = 1;
            $stock_quantity = rand(10, 50); // مخزون عشوائي بين 10-50
        }
        
        // تحديد الشحن المجاني
        $is_free_shipping = 1; // معظم الخدمات شحن مجاني
        
        // تحديد إذا كان منتج طبي
        $is_medicinal = 0;
        $medicinal_leaflet = '';
        
        if (stripos($service['name'], 'درما بن') !== false || 
            stripos($service['name'], 'ميزوثرابي') !== false) {
            $is_medicinal = 1;
            $medicinal_leaflet = 'يُنصح باستشارة الطبيب قبل الاستخدام. قد يسبب احمرار مؤقت. تجنب التعرض للشمس بعد الجلسة.';
        }
        
        // تحديد الفروع المتاحة (كل الخدمات متاحة في كل الفروع)
        $available_branches = json_encode($all_branches);
        
        // تحديد الأنواع المتاحة حسب الخدمة
        $variations = [];
        
        if (stripos($service['name'], 'صبغة') !== false) {
            $variations = ['types' => ['أسود', 'بني', 'كستنائي', 'أحمر', 'بلوند']];
        } elseif (stripos($service['name'], 'رموش') !== false) {
            $variations = ['types' => ['طبيعي', 'كثيف', 'طويل']];
        } elseif (stripos($service['name'], 'فيك نيلز') !== false) {
            $variations = ['types' => ['أحمر', 'وردي', 'بيج', 'فرنش', 'عنابي']];
        } elseif (stripos($service['name'], 'بروتين') !== false || stripos($service['name'], 'كفيار') !== false) {
            $variations = ['types' => ['شعر قصير', 'شعر متوسط', 'شعر طويل']];
        }
        
        $variations_json = !empty($variations) ? json_encode($variations) : json_encode([]);
        
        // تحديث الخدمة
        $update_query = "UPDATE beauty_services SET 
            available_branches = ?,
            stock_quantity = ?,
            is_limited_stock = ?,
            is_free_shipping = ?,
            is_medicinal = ?,
            medicinal_leaflet = ?,
            variations = ?
            WHERE id = ?";
        
        $update_stmt = $db->prepare($update_query);
        $update_stmt->execute([
            $available_branches,
            $stock_quantity,
            $is_limited_stock,
            $is_free_shipping,
            $is_medicinal,
            $medicinal_leaflet,
            $variations_json,
            $service['id']
        ]);
        
        $success_count++;
        
        $stock_info = $is_limited_stock ? " (مخزون: $stock_quantity)" : " (غير محدود)";
        $branches_info = " - متاح في " . count($all_branches) . " فرع";
        $variations_info = !empty($variations) ? " - " . count($variations['types']) . " نوع" : "";
        
        echo "✅ تم تحديث: <strong>{$service['name']}</strong>{$stock_info}{$branches_info}{$variations_info}<br>";
        
    } catch (Exception $e) {
        $error_count++;
        echo "❌ خطأ في تحديث: {$service['name']} - {$e->getMessage()}<br>";
    }
}

echo "<br><hr><br>";
echo "<h2>النتيجة النهائية:</h2>";
echo "<p style='color: green; font-size: 18px;'>✅ تم تحديث {$success_count} خدمة بنجاح</p>";
if ($error_count > 0) {
    echo "<p style='color: red; font-size: 18px;'>❌ فشل تحديث {$error_count} خدمة</p>";
}

echo "<br><h3>الإحصائيات:</h3>";

// إحصائيات المخزون
$limited_stock_query = "SELECT COUNT(*) FROM beauty_services WHERE is_limited_stock = 1";
$limited_stock_count = $db->query($limited_stock_query)->fetchColumn();
echo "<p>📦 خدمات بمخزون محدود: <strong>{$limited_stock_count}</strong></p>";

// إحصائيات المنتجات الطبية
$medicinal_query = "SELECT COUNT(*) FROM beauty_services WHERE is_medicinal = 1";
$medicinal_count = $db->query($medicinal_query)->fetchColumn();
echo "<p>💊 منتجات طبية: <strong>{$medicinal_count}</strong></p>";

// إحصائيات الأنواع
$variations_query = "SELECT COUNT(*) FROM beauty_services WHERE variations != '[]' AND variations IS NOT NULL";
$variations_count = $db->query($variations_query)->fetchColumn();
echo "<p>🎨 خدمات بأنواع متعددة: <strong>{$variations_count}</strong></p>";

echo "<br><a href='admin/services/index.php' style='padding: 10px 20px; background: #E57393; color: white; text-decoration: none; border-radius: 5px;'>عرض الخدمات</a>";
?>
