import {
  ndmClient,
  userClient,
  type ImportMsg,
  type NdmDecoderPageQuery,
  type NdmDecoderResultVO,
  type NdmDecoderSaveVO,
  type NdmDecoderUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import { unwrapResponse, unwrapVoidResponse } from '@/utils';

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

export const detailDecoderApi = 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/ndmDecoder/detail`;
  const resp = await client.get<NdmDecoderResultVO>(endpoint, { params: { id }, signal });
  const data = unwrapResponse(resp);
  return data;
};

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

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

export const deleteDecoderApi = 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/ndmDecoder`;
  const resp = await client.delete<boolean>(endpoint, ids, { signal });
  const data = unwrapResponse(resp);
  return data;
};

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

export const importDecoderApi = 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/ndmDecoder/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;
};

export const probeDecoderApi = 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/ndmDecoder/probeByIds`;
  const resp = await client.post<void>(endpoint, ids, { signal });
  unwrapVoidResponse(resp);
};
