import {
  ndmClient,
  userClient,
  type ImportMsg,
  type NdmMediaServerPageQuery,
  type NdmMediaServerResultVO,
  type NdmMediaServerSaveVO,
  type NdmMediaServerUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import { unwrapResponse, unwrapVoidResponse } from '@/utils';

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

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

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

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

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

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

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