<?php
/**
 * Add pin_mode column to device_pins table
 * To differentiate between INPUT and OUTPUT pins
 */

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

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    echo "<h2>Adding pin_mode column to device_pins...</h2>";
    
    // Check if column exists
    $check_query = "SHOW COLUMNS FROM device_pins LIKE 'pin_mode'";
    $check_stmt = $db->query($check_query);
    
    if ($check_stmt->rowCount() > 0) {
        echo "<p style='color: orange;'>✓ Column 'pin_mode' already exists!</p>";
    } else {
        // Add pin_mode column
        $alter_query = "ALTER TABLE device_pins 
                       ADD COLUMN pin_mode ENUM('OUTPUT', 'INPUT') DEFAULT 'OUTPUT' 
                       AFTER pin_type";
        $db->exec($alter_query);
        echo "<p style='color: green;'>✓ Column 'pin_mode' added successfully!</p>";
    }
    
    // Update existing pins based on pin_type
    $update_query = "UPDATE device_pins 
                    SET pin_mode = CASE 
                        WHEN pin_type IN ('sensor', 'temperature', 'humidity', 'motion', 'light') THEN 'INPUT'
                        ELSE 'OUTPUT'
                    END";
    $db->exec($update_query);
    echo "<p style='color: green;'>✓ Updated existing pins based on type!</p>";
    
    echo "<h3>Current pins configuration:</h3>";
    $pins_query = "SELECT device_id, pin_name, pin_type, pin_mode, pin_gpio FROM device_pins ORDER BY device_id, pin_gpio";
    $pins_stmt = $db->query($pins_query);
    $pins = $pins_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<table border='1' cellpadding='10' style='border-collapse: collapse;'>";
    echo "<tr style='background: #f3f4f6;'><th>Device</th><th>Pin Name</th><th>Type</th><th>Mode</th><th>GPIO</th></tr>";
    foreach ($pins as $pin) {
        $mode_color = $pin['pin_mode'] === 'OUTPUT' ? '#10b981' : '#3b82f6';
        $mode_icon = $pin['pin_mode'] === 'OUTPUT' ? '🔌' : '📊';
        echo "<tr>";
        echo "<td>{$pin['device_id']}</td>";
        echo "<td>{$pin['pin_name']}</td>";
        echo "<td>{$pin['pin_type']}</td>";
        echo "<td style='color: $mode_color; font-weight: bold;'>$mode_icon {$pin['pin_mode']}</td>";
        echo "<td>GPIO{$pin['pin_gpio']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    echo "<br><div style='background: #dbeafe; border-left: 4px solid #3b82f6; padding: 15px; margin: 20px 0;'>";
    echo "<h3 style='margin-top: 0;'>📝 ملاحظة:</h3>";
    echo "<p><strong>OUTPUT (مخرجات):</strong> يمكن التحكم فيها (تشغيل/إيقاف) - مثل: Relay, LED, Motor</p>";
    echo "<p><strong>INPUT (مدخلات):</strong> للقراءة فقط (عرض القيمة) - مثل: Sensor, Temperature, Motion</p>";
    echo "</div>";
    
    echo "<p style='color: green; font-weight: bold;'>✅ All done!</p>";
    echo "<p><a href='../admin/iot/pins.php'>← Back to Pin Management</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error: " . $e->getMessage() . "</p>";
}
?>
