<?php
/**
 * Add WiFi columns to iot_settings table
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

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

echo "<!DOCTYPE html>
<html lang='ar' dir='rtl'>
<head>
    <meta charset='UTF-8'>
    <title>إضافة حقول WiFi</title>
    <style>
        * { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
        body { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; }
        .container { max-width: 800px; margin: 0 auto; background: white; border-radius: 20px; padding: 40px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
        .success { background: #10b981; color: white; padding: 15px; border-radius: 10px; margin: 10px 0; }
        .error { background: #ef4444; color: white; padding: 15px; border-radius: 10px; margin: 10px 0; }
        .info { background: #3b82f6; color: white; padding: 15px; border-radius: 10px; margin: 10px 0; }
        h1 { color: #1f2937; margin-bottom: 30px; }
        .btn { display: inline-block; padding: 12px 24px; background: linear-gradient(135deg, #667eea, #764ba2); color: white; text-decoration: none; border-radius: 10px; margin-top: 20px; font-weight: bold; }
    </style>
</head>
<body>
<div class='container'>";

echo "<h1>🔧 إضافة حقول WiFi لجدول iot_settings</h1>";

try {
    $database = new Database();
    $db = $database->getConnection();
    
    echo "<div class='info'>✅ تم الاتصال بقاعدة البيانات</div>";
    
    // Check if table exists
    $check_table = $db->query("SHOW TABLES LIKE 'iot_settings'");
    if ($check_table->rowCount() == 0) {
        throw new Exception('جدول iot_settings غير موجود! شغّل run_iot_setup.php أولاً');
    }
    
    // Get current columns
    $columns_query = $db->query("SHOW COLUMNS FROM iot_settings");
    $existing_columns = $columns_query->fetchAll(PDO::FETCH_COLUMN);
    
    $added = [];
    
    // Add wifi_ssid
    if (!in_array('wifi_ssid', $existing_columns)) {
        $db->exec("ALTER TABLE iot_settings ADD COLUMN wifi_ssid VARCHAR(100) DEFAULT 'YOUR_WIFI_NAME'");
        $added[] = 'wifi_ssid';
        echo "<div class='success'>✅ تم إضافة عمود wifi_ssid</div>";
    } else {
        echo "<div class='info'>⚠️ عمود wifi_ssid موجود بالفعل</div>";
    }
    
    // Add wifi_password
    if (!in_array('wifi_password', $existing_columns)) {
        $db->exec("ALTER TABLE iot_settings ADD COLUMN wifi_password VARCHAR(100) DEFAULT 'YOUR_WIFI_PASSWORD'");
        $added[] = 'wifi_password';
        echo "<div class='success'>✅ تم إضافة عمود wifi_password</div>";
    } else {
        echo "<div class='info'>⚠️ عمود wifi_password موجود بالفعل</div>";
    }
    
    // Add server_ip
    if (!in_array('server_ip', $existing_columns)) {
        $db->exec("ALTER TABLE iot_settings ADD COLUMN server_ip VARCHAR(50) DEFAULT 'localhost'");
        $added[] = 'server_ip';
        echo "<div class='success'>✅ تم إضافة عمود server_ip</div>";
    } else {
        echo "<div class='info'>⚠️ عمود server_ip موجود بالفعل</div>";
    }
    
    if (empty($added)) {
        echo "<div class='info'>ℹ️ جميع الأعمدة موجودة بالفعل - لا توجد تغييرات</div>";
    } else {
        echo "<div class='success'><strong>🎉 تم التحديث بنجاح!</strong><br>تم إضافة: " . implode(', ', $added) . "</div>";
    }
    
    echo "<a href='../admin/iot/arduino-code-generator.php' class='btn'>🚀 الذهاب إلى مولد الكود</a>";
    
} catch (Exception $e) {
    echo "<div class='error'>❌ خطأ: " . htmlspecialchars($e->getMessage()) . "</div>";
}

echo "</div></body></html>";
?>
