import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { BCAgent, BCAgentParam } from '@/api/apps/bc/agent/model';
/**
 * 分页查询设备
 */
export async function pageBCAgent(params: BCAgentParam) {
  const res = await request.get<ApiResult<PageResult<BCAgent>>>(
    '/apps/bc-agent/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询设备列表
 */
export async function listBCAgent(params?: BCAgentParam) {
  const res = await request.get<ApiResult<BCAgent[]>>('/apps/bc-agent', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加设备
 */
export async function addBCAgent(data: BCAgent) {
  const res = await request.post<ApiResult<unknown>>('/apps/bc-agent', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改设备
 */
export async function updateBCAgent(data: BCAgent) {
  const res = await request.put<ApiResult<unknown>>('/apps/bc-agent', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 绑定设备
 */
export async function bindBCAgent(data: BCAgent) {
  const res = await request.put<ApiResult<unknown>>(
    '/apps/bc-agent/bind',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除设备
 */
export async function removeBCAgent(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/apps/bc-agent/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除设备
 */
export async function removeBatchBCAgent(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/apps/bc-agent/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
