import {
  ndmClient,
  userClient,
  type ImportMsg,
  type NdmVideoServerPageQuery,
  type NdmVideoServerResultVO,
  type NdmVideoServerSaveVO,
  type NdmVideoServerUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import { unwrapResponse, unwrapVoidResponse } from '@/utils';

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

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

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

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

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

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

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