<?php
/**
 * Pin Status API - Get current pin states
 */

header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

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

try {
    $database = new Database();
    $db = $database->getConnection();
    
    $device_id = $_GET['device_id'] ?? null;
    
    if (!$device_id) {
        http_response_code(400);
        echo json_encode(['error' => 'device_id is required']);
        exit;
    }
    
    // Get all pins with their current states
    $query = "SELECT *, current_state as state FROM device_pins WHERE device_id = ? ORDER BY pin_gpio";
    
    $stmt = $db->prepare($query);
    $stmt->execute([$device_id]);
    $pins = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo json_encode(['success' => true, 'pins' => $pins]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}
?>
