import { http } from '@/utils/http/axios';
import type {
  AddSensorGroupReq,
  BatchSaveSensor2GroupReq,
  DeleteSensorFromGroupReq,
  QueryDevicePropertiesHistoryReq,
  QueryDevicePropertiesHistoryRes,
  ProductionSensorGroup,
  QueryDeviceListReq,
  QueryDeviceListRes,
  SaveSensor2GroupReq,
  UpdateSensorGroupReq,
  UpdateSensorGroupOrderItem,
  UpdateSensorInGroupReq,
  UpdateSensorOrderItem,
} from './type';

/**
 * 查询传感器分组列表
 */
export function querySensorGroupListApi() {
  return http.request<ProductionSensorGroup[]>({
    url: '/sensor/querySensorGroupList',
    method: 'get',
  });
}

/**
 * 新建传感器分组
 */
export function addSensorGroupApi(data: AddSensorGroupReq) {
  return http.request<number>({
    url: '/sensor/addSensor',
    method: 'post',
    data,
  });
}

/**
 * 删除传感器分组
 */
export function deleteSensorGroupApi(id: number) {
  return http.request({
    url: `/sensor/deleteSensor?id=${id}`,
    method: 'delete',
  });
}

/**
 * 更新传感器分组
 */
export function updateSensorGroupApi(data: UpdateSensorGroupReq) {
  return http.request({
    url: '/sensor/updateSensor',
    method: 'post',
    data,
  });
}

/**
 * 单个添加传感器到分组
 */
export function saveSensor2GroupApi(data: SaveSensor2GroupReq) {
  return http.request({
    url: '/sensor/saveSensor2Group',
    method: 'post',
    data,
  });
}

/**
 * 批量添加传感器到分组
 */
export function batchSaveSensor2GroupApi(data: BatchSaveSensor2GroupReq) {
  return http.request({
    url: '/sensor/batchSaveSensor2Group',
    method: 'post',
    data,
  });
}

/**
 * 编辑分组内传感器（全量替换）
 */
export function updateSensorInGroupApi(data: UpdateSensorInGroupReq) {
  return http.request({
    url: '/sensor/updateSensorInGroup',
    method: 'post',
    data,
  });
}

/**
 * 删除分组内的传感器
 */
export function deleteSensorFromGroupApi(data: DeleteSensorFromGroupReq) {
  return http.request({
    url: `/sensor/deleteSensorFromGroup?groupId=${data.groupId}&deviceNo=${data.deviceNo}`,
    method: 'delete',
  });
}

/**
 * 修改传感器分组顺序
 */
export function updateSensorGroupOrderApi(data: UpdateSensorGroupOrderItem[]) {
  return http.request({
    url: '/sensor/updateSensorGroupOrder',
    method: 'post',
    data,
  });
}

/**
 * 修改分组内传感器顺序
 */
export function updateSensorOrderApi(data: UpdateSensorOrderItem[]) {
  return http.request({
    url: '/sensor/updateSensorOrder',
    method: 'post',
    data,
  });
}

/**
 * 弹窗查询可添加设备列表
 */
export function queryDeviceListApi(data: QueryDeviceListReq) {
  return http.request<QueryDeviceListRes | QueryDeviceListRes['records']>({
    url: '/sensor/queryDeviceList',
    method: 'post',
    data,
  });
}

/**
 * 查询传感器历史属性（温度/湿度/浓度）
 */
export function queryDevicePropertiesHistoryApi(data: QueryDevicePropertiesHistoryReq) {
  const historyApiUrl = `${window.location.protocol}//${window.location.hostname}`;
  return http.request<QueryDevicePropertiesHistoryRes>({
    url: `${historyApiUrl}/openapi/devices/properties/history`,
    method: 'post',
    data,
    headers: {
      Authorization: 'Basic ODM2NDIxOGUxNDE0NDQ3NTllNjY5Yjc5YmU2ZDQyNTI6YWJjZWNhZjg1N2I4NDljNDg3N2ZkNzRmZThkZDZkZTM=',
    },
  });
}
