import {
  ndmClient,
  userClient,
  type ImportMsg,
  type NdmAlarmHostPageQuery,
  type NdmAlarmHostResultVO,
  type NdmAlarmHostSaveVO,
  type NdmAlarmHostUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import { unwrapResponse } from '@/utils';

export const pageAlarmHostApi = async (pageQuery: PageParams<NdmAlarmHostPageQuery>, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost/page`;
  const resp = await client.post<PageResult<NdmAlarmHostResultVO>>(endpoint, pageQuery, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const detailAlarmHostApi = async (id: string, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost/detail`;
  const resp = await client.get<NdmAlarmHostResultVO>(endpoint, { params: { id }, signal });
  const data = unwrapResponse(resp);
  return data;
};

export const saveAlarmHostApi = async (saveVO: NdmAlarmHostSaveVO, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost`;
  const resp = await client.post<NdmAlarmHostResultVO>(endpoint, saveVO, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const updateAlarmHostApi = async (updateVO: NdmAlarmHostUpdateVO, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost`;
  const resp = await client.put<NdmAlarmHostResultVO>(endpoint, updateVO, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const deleteAlarmHostApi = async (ids: string[], options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost`;
  const resp = await client.delete<boolean>(endpoint, ids, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const exportAlarmHostApi = async (pageQuery: PageParams<NdmAlarmHostPageQuery>, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost/defaultExportByTemplate`;
  const resp = await client.post<Blob>(endpoint, pageQuery, { responseType: 'blob', retRaw: true, signal });
  const data = unwrapResponse(resp);
  return data;
};

export const importAlarmHostApi = async (file: File, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmAlarmHost/importReturnMsg`;
  const formData = new FormData();
  formData.append('file', file);
  const resp = await client.post<ImportMsg>(endpoint, formData, { signal, upload: true });
  const data = unwrapResponse(resp);
  return data;
};
