import axios from 'axios'

// 创建axios实例
const apiClient = axios.create({
  baseURL: '/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// API服务对象
const apiService = {
  // 串口相关API
  serial: {
    // 获取可用串口列表
    getPorts: () => apiClient.get('/serial/ports'),
    
    // 连接串口
    connect: (config) => apiClient.post('/serial/connect', config),
    
    // 断开串口
    disconnect: () => apiClient.post('/serial/disconnect'),
    
    // 获取串口状态
    getStatus: () => apiClient.get('/serial/status'),
    
    // 发送数据到串口
    sendData: (message) => apiClient.post('/serial/send', { message })
  },
  
  // MQTT相关API
  mqtt: {
    // 连接MQTT
    connect: (config) => apiClient.post('/mqtt/connect', config),

    // 断开MQTT
    disconnect: () => apiClient.post('/mqtt/disconnect'),

    // 获取MQTT状态
    getStatus: () => apiClient.get('/mqtt/status'),

    // 发布消息
    publish: (topic, message) => apiClient.post('/mqtt/publish', { topic, message }),

    // 订阅主题
    subscribe: (topics) => apiClient.post('/mqtt/subscribe', { topics }),

    // 发送DTU发现广播 (7.14)
    broadcastDiscover: (customerId) => apiClient.post('/mqtt/broadcast_discover', { customer_id: customerId }),

    // 发送DTU批量配置广播 (7.15)
    broadcastConfig: (customerId, config) => apiClient.post('/mqtt/broadcast_config', { customer_id: customerId, config })
  },
  
  // 数据相关API
  data: {
    // 获取串口数据
    getSerialData: () => apiClient.get('/data/serial'),
    
    // 获取MQTT数据
    getMQTTData: () => apiClient.get('/data/mqtt')
  },
  
  // Modbus API
  modbus: {
    getAntennaAddresses: () => apiClient.get('/modbus/antenna_addresses'),
    readAntenna: (params) => apiClient.post('/modbus/read_antenna', params),
    readRegisters: (params) => apiClient.post('/modbus/read_registers', params),
    writeRegister: (params) => apiClient.post('/modbus/write_register', params),
    setRgbLed: (params) => apiClient.post('/modbus/set_rgb_led', params),
    setAllLeds: (params) => apiClient.post('/modbus/set_all_leds', params),
    getLedStatus: () => apiClient.get('/modbus/led_status'),
    scan: (params) => apiClient.post('/modbus/scan', params),
    broadcastQuery: (params) => apiClient.post('/modbus/broadcast_query', params),
    autoConfigure: (params) => apiClient.post('/modbus/auto_configure', params),
    getStoredDevices: () => apiClient.get('/modbus/stored_devices'),
    addStoredDevice: (params) => apiClient.post('/modbus/stored_devices', params),
  },
  
  // 转发配置API
  forward: {
    // 设置转发配置
    setConfig: (config) => apiClient.post('/forward/config', config),

    // 获取转发状态
    getStatus: () => apiClient.get('/forward/status')
  },

  // DTU配置API
  dtu: {
    // 获取DTU配置
    getConfig: () => apiClient.get('/dtu/config'),

    // 更新DTU配置
    updateConfig: (config) => apiClient.post('/dtu/config', config),

    // 手动触发DTU注册
    register: () => apiClient.post('/dtu/register'),

    // 获取DTU状态
    getStatus: () => apiClient.get('/dtu/status'),

    // 发送控制命令
    sendControl: (command) => apiClient.post('/dtu/control', command),

    // 获取OTA升级状态
    getOtaStatus: () => apiClient.get('/dtu/ota_status'),

    // 检测固件信息
    detectFirmware: (url) => apiClient.post('/dtu/ota_detect', { url }),

    // 触发OTA升级
    triggerOtaUpgrade: (data) => apiClient.post('/dtu/ota_upgrade', data)
  },

  // 端口状态API
  port: {
    // 获取所有端口状态
    getStatus: () => apiClient.get('/port/status'),

    // 获取端口事件历史
    getEvents: (limit = 100) => apiClient.get('/port/events', { params: { limit } }),

    // 清除端口事件历史
    clearEvents: () => apiClient.post('/port/clear_events')
  },

  // 面板状态API
  panel: {
    // 获取所有面板状态
    getStatus: () => apiClient.get('/panel/status')
  },

  // 传感器API
  sensor: {
    // 获取环境传感器数据
    getEnvData: () => apiClient.get('/sensor/env'),

    // 获取/设置告警阈值
    getThreshold: () => apiClient.get('/sensor/threshold'),
    setThreshold: (threshold) => apiClient.post('/sensor/threshold', threshold),

    // 获取历史数据
    getHistory: (range = '1h') => apiClient.get('/sensor/history', { params: { range } })
  }
}

export default apiService