import { requestClient } from '#/api/request';

export namespace DeviceApi {
  export interface Device {
    [key: string]: any;
    name: string;
    device_key: string;
    org_id: string;
    enabled: 0;
    online_status: 0;
    location: string;
    description: string;
    last_online_time: string;
    last_offline_time: string;
    created_at: string;
    updated_at: string;
  }
  export interface createDevice {
    description: string;
    org_id: string;
    name: string;
    device_key: string;
    location: string;
  }
  export interface getDeviceList {
    list: (Device & { id: number })[];
    total: number;
  }
  export interface deviceContrl {
    devices: {
      device_id: number;
      device_key: string;
    }[];
    org_id: string;
  }
  export interface deviceDisabled {
    ids: number[];
  }
}

/**
 * 获取设备列表
 * @param org_id 组织ID
 * @returns 设备列表和总数
 */
export const getDeviceList = (org_id: string) => {
  return requestClient.get<DeviceApi.getDeviceList>(
    '/device-management/devices',
    {
      params: {
        org_id,
      },
    },
  );
};

/**
 * 创建设备
 * @param data 设备信息
 * @returns 创建结果
 */
export const createDevice = async (data: DeviceApi.createDevice) =>
  requestClient.post('/device-management/devices', data);

/**
 * 更新设备信息
 * @param id 设备ID
 * @param data 更新的设备信息
 * @returns 更新结果
 */
export const updateDevice = async (id: number, data: DeviceApi.Device) =>
  requestClient.put(`/device-management/devices/${id}`, data);

/**
 * 删除设备
 * @param id 设备ID
 * @returns 删除结果
 */
export const deleteDevice = async (id: number) =>
  requestClient.delete(`/device-management/devices/`, {
    params: {
      ids: [id],
    },
  });

/**
 * 根据ID获取设备信息
 * @param id 设备ID
 * @returns 设备信息
 */
export const getDevice = async (id: number) =>
  requestClient.get<DeviceApi.Device>(`/device-management/devices/${id}`);

export const deviceControlOnline = async (
  status: boolean,
  data: DeviceApi.deviceContrl,
) => {
  return status
    ? requestClient.put(`/device-management/devices/status/online`, data)
    : requestClient.put(`/device-management/devices/status/offline`, data);
};

export const deviceDisabled = async (
  status: boolean,
  data: DeviceApi.deviceDisabled,
) => {
  return status
    ? requestClient.put(`/device-management/devices/status/enable`, data)
    : requestClient.put(`/device-management/devices/status/disable`, data);
};
