import {
  ndmClient,
  userClient,
  type NdmPermissionPageQuery,
  type NdmPermissionResultVO,
  type NdmPermissionSaveVO,
  type NdmPermissionUpdateVO,
  type PageParams,
  type PageResult,
  type Station,
} from '@/apis';
import type { PermissionTypeEnum } from '@/enums';
import { unwrapResponse } from '@/utils';

export const permissionTypesApi = async (options?: { stationCode?: Station['code']; signal?: AbortSignal }) => {
  const { stationCode, signal } = options ?? {};
  const client = stationCode ? ndmClient : userClient;
  const prefix = stationCode ? `/${stationCode}` : '';
  const endpoint = `${prefix}/api/ndm/ndmPermission/types`;
  const resp = await client.get<PermissionTypeEnum>(endpoint, { signal });
  const data = unwrapResponse(resp);
  return data;
};

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

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

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

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

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

export const modifyPermissionApi = async (params: { employeeId: string; saveList: NdmPermissionSaveVO[]; removeList: 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/ndmPermission/modify`;
  const resp = await client.post<boolean>(endpoint, params, { signal });
  const result = unwrapResponse(resp);
  return result;
};
