import {
  ndmClient,
  userClient,
  type ImportMsg,
  type NdmKeyboardPageQuery,
  type NdmKeyboardResultVO,
  type NdmKeyboardSaveVO,
  type NdmKeyboardUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import { unwrapResponse } from '@/utils';

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

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

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

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

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

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

export const importKeyboardApi = 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/ndmKeyboard/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;
};
