import { ndmClient, userClient, type ClientChannel, type NdmNvrResultVO, type NdmRecordCheck, type Station } from '@/apis';
import { unwrapResponse } from '@/utils';
import dayjs from 'dayjs';

export const getChannelListApi = async (ndmNvr: NdmNvrResultVO, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmRecordCheck/getChannelList`;
  const resp = await client.post<ClientChannel[]>(endpoint, { code: ndmNvr.gbCode, time: '' }, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const getRecordCheckApi = async (ndmNvr: NdmNvrResultVO, lastDays: number, gbCodeList: 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/ndmRecordCheck/getRecordCheckByParentId`;
  const endDateTime = dayjs();
  const startDateTime = endDateTime.subtract(lastDays, 'day');
  const start = startDateTime.format('YYYY-MM-DD');
  const end = endDateTime.format('YYYY-MM-DD');
  const parentId = ndmNvr.gbCode;
  const resp = await client.post<NdmRecordCheck[]>(endpoint, { start, end, parentId, gbCodeList }, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const reloadRecordCheckApi = async (channel: ClientChannel, dayOffset: number, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmRecordCheck/reloadRecordCheckByGbId`;
  const resp = await client.post<boolean>(endpoint, { ...channel, dayOffset }, { signal });
  const data = unwrapResponse(resp);
  return data;
};

export const reloadAllRecordCheckApi = async (dayOffset: number, options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmRecordCheck/reloadAllRecordCheck`;
  const resp = await client.post<boolean>(endpoint, dayOffset, { signal });
  const [err, data] = resp;
  if (err) throw err;
  if (!data) throw new Error(`${data}`);
  return data;
};

export const batchExportRecordCheckApi = async (params: { checkDuration: number; gapSeconds: number; stationCode: Station['code'][] }, options?: { signal?: AbortSignal }) => {
  const { signal } = options ?? {};
  const { checkDuration, gapSeconds, stationCode } = params;
  const client = userClient;
  const endpoint = `/api/ndm/ndmRecordCheck/batchExportByTemplate`;
  const resp = await client.post<Blob>(endpoint, { checkDuration, gapSeconds, stationCode }, { responseType: 'blob', retRaw: true, signal });
  const data = unwrapResponse(resp);
  return data;
};
