<?php
// إضافة قوالب المتحكمات (Arduino & ESP) لنظام المقايسة

require_once '../config/database.php';

$database = new Database();
$conn = $database->getConnection();

echo "<h2>🎛️ إضافة قوالب المتحكمات</h2>";

try {
    // قوالب المتحكمات
    $controllers = [
        // Arduino
        [
            'device_name' => 'Arduino Mega 2560',
            'device_type' => 'arduino_mega',
            'category' => 'control',
            'description' => 'متحكم Arduino Mega 2560 - 54 منفذ رقمي، 16 منفذ تناظري، مثالي للمشاريع الكبيرة',
            'default_price' => 450.00,
            'installation_cost' => 100.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'Arduino Uno R3',
            'device_type' => 'arduino_uno',
            'category' => 'control',
            'description' => 'متحكم Arduino Uno R3 - 14 منفذ رقمي، 6 منافذ تناظرية، مناسب للمشاريع المتوسطة',
            'default_price' => 250.00,
            'installation_cost' => 80.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'Arduino Nano',
            'device_type' => 'arduino_nano',
            'category' => 'control',
            'description' => 'متحكم Arduino Nano - صغير الحجم، مثالي للمشاريع المدمجة',
            'default_price' => 150.00,
            'installation_cost' => 50.00,
            'is_active' => 1
        ],
        
        // ESP Modules
        [
            'device_name' => 'ESP32 DevKit',
            'device_type' => 'esp32',
            'category' => 'control',
            'description' => 'متحكم ESP32 مع WiFi و Bluetooth - معالج ثنائي النواة، مثالي لمشاريع IoT',
            'default_price' => 200.00,
            'installation_cost' => 80.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'ESP8266 NodeMCU',
            'device_type' => 'esp8266',
            'category' => 'control',
            'description' => 'متحكم ESP8266 مع WiFi - اقتصادي وقوي لمشاريع IoT',
            'default_price' => 120.00,
            'installation_cost' => 60.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'ESP32-CAM',
            'device_type' => 'esp32_cam',
            'category' => 'security',
            'description' => 'ESP32 مع كاميرا مدمجة - للمراقبة والتصوير عن بعد',
            'default_price' => 180.00,
            'installation_cost' => 100.00,
            'is_active' => 1
        ],
        
        // Relay Modules
        [
            'device_name' => 'وحدة ريلاي 1 قناة',
            'device_type' => 'relay_1ch',
            'category' => 'control',
            'description' => 'وحدة ريلاي قناة واحدة 5V - للتحكم في جهاز واحد',
            'default_price' => 35.00,
            'installation_cost' => 20.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'وحدة ريلاي 4 قنوات',
            'device_type' => 'relay_4ch',
            'category' => 'control',
            'description' => 'وحدة ريلاي 4 قنوات 5V - للتحكم في 4 أجهزة',
            'default_price' => 80.00,
            'installation_cost' => 40.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'وحدة ريلاي 8 قنوات',
            'device_type' => 'relay_8ch',
            'category' => 'control',
            'description' => 'وحدة ريلاي 8 قنوات 5V - للتحكم في 8 أجهزة',
            'default_price' => 140.00,
            'installation_cost' => 60.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'وحدة ريلاي 16 قناة',
            'device_type' => 'relay_16ch',
            'category' => 'control',
            'description' => 'وحدة ريلاي 16 قناة 5V - للمشاريع الكبيرة',
            'default_price' => 250.00,
            'installation_cost' => 100.00,
            'is_active' => 1
        ],
        
        // Power Supplies
        [
            'device_name' => 'محول طاقة 5V 2A',
            'device_type' => 'power_5v_2a',
            'category' => 'other',
            'description' => 'محول طاقة 5 فولت 2 أمبير - للمتحكمات الصغيرة',
            'default_price' => 50.00,
            'installation_cost' => 20.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'محول طاقة 12V 5A',
            'device_type' => 'power_12v_5a',
            'category' => 'other',
            'description' => 'محول طاقة 12 فولت 5 أمبير - للمشاريع المتوسطة',
            'default_price' => 120.00,
            'installation_cost' => 30.00,
            'is_active' => 1
        ],
        
        // Breadboards & Cables
        [
            'device_name' => 'لوحة تجارب Breadboard',
            'device_type' => 'breadboard',
            'category' => 'other',
            'description' => 'لوحة تجارب للتوصيلات المؤقتة',
            'default_price' => 30.00,
            'installation_cost' => 0.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'أسلاك توصيل Jumper Wires',
            'device_type' => 'jumper_wires',
            'category' => 'other',
            'description' => 'مجموعة أسلاك توصيل ملونة (40 قطعة)',
            'default_price' => 25.00,
            'installation_cost' => 0.00,
            'is_active' => 1
        ],
        [
            'device_name' => 'كابل USB للبرمجة',
            'device_type' => 'usb_cable',
            'category' => 'other',
            'description' => 'كابل USB لبرمجة Arduino و ESP',
            'default_price' => 20.00,
            'installation_cost' => 0.00,
            'is_active' => 1
        ]
    ];
    
    $added = 0;
    $skipped = 0;
    
    foreach ($controllers as $controller) {
        // التحقق من عدم وجود القالب مسبقاً
        $check_query = "SELECT id FROM iot_device_templates 
                       WHERE device_name = :device_name AND device_type = :device_type";
        $check_stmt = $conn->prepare($check_query);
        $check_stmt->bindParam(':device_name', $controller['device_name']);
        $check_stmt->bindParam(':device_type', $controller['device_type']);
        $check_stmt->execute();
        
        if ($check_stmt->rowCount() > 0) {
            echo "<p style='color: orange;'>⚠️ تم تخطي: {$controller['device_name']} (موجود مسبقاً)</p>";
            $skipped++;
            continue;
        }
        
        // إضافة القالب
        $insert_query = "INSERT INTO iot_device_templates 
                        (device_name, device_type, category, description, default_price, installation_cost, is_active) 
                        VALUES 
                        (:device_name, :device_type, :category, :description, :default_price, :installation_cost, :is_active)";
        
        $insert_stmt = $conn->prepare($insert_query);
        $insert_stmt->bindParam(':device_name', $controller['device_name']);
        $insert_stmt->bindParam(':device_type', $controller['device_type']);
        $insert_stmt->bindParam(':category', $controller['category']);
        $insert_stmt->bindParam(':description', $controller['description']);
        $insert_stmt->bindParam(':default_price', $controller['default_price']);
        $insert_stmt->bindParam(':installation_cost', $controller['installation_cost']);
        $insert_stmt->bindParam(':is_active', $controller['is_active']);
        
        if ($insert_stmt->execute()) {
            echo "<p style='color: green;'>✅ تمت إضافة: {$controller['device_name']} - {$controller['default_price']} ج.م</p>";
            $added++;
        }
    }
    
    echo "<hr>";
    echo "<h3>📊 الملخص:</h3>";
    echo "<p>✅ تمت إضافة: <strong>{$added}</strong> قالب</p>";
    echo "<p>⚠️ تم تخطي: <strong>{$skipped}</strong> قالب (موجود مسبقاً)</p>";
    echo "<p style='color: green; font-weight: bold; font-size: 18px;'>🎉 تم إضافة قوالب المتحكمات بنجاح!</p>";
    echo "<p><a href='../admin/iot-estimation/templates/devices.php' style='padding: 10px 20px; background: #667eea; color: white; text-decoration: none; border-radius: 8px; display: inline-block; margin-top: 10px;'>عرض جميع القوالب</a></p>";
    
} catch (PDOException $e) {
    echo "<p style='color: red;'>❌ خطأ: " . $e->getMessage() . "</p>";
}
?>

<style>
body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    padding: 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
}
h2, h3 {
    color: #333;
    background: white;
    padding: 15px;
    border-radius: 10px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
p {
    background: white;
    padding: 10px 15px;
    margin: 5px 0;
    border-radius: 8px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
</style>
