import axiosInstance from '../../utils/axios';

export interface StatisticsQuery {
  startTime?: Date | string;
  endTime?: Date | string;
  type?: string;
  [key: string]: any;
}

/** 按物资柜的开关/异常等统计（行数据） */
export type CabinetStatRow = {
  cabinetName?: string;
  openCount?: number;
  misplaceCount?: number;
  openTimeoutCount?: number;
  [key: string]: any;
};

/** 按物资类型的借还/均摊时长等 */
export type MaterialsLoanStatRow = {
  materialsTypeName?: string;
  allCount?: number;
  returnCount?: number;
  timeoutCount?: number;
  averageTime?: number;
  [key: string]: any;
};

/** 特殊状态物资、更换统计等（按类型） */
export type MaterialsTypeCountRow = {
  materialsTypeName?: string;
  willExpireCount?: number;
  expiredCount?: number;
  badCount?: number;
  allCount?: number;
  expireCount?: number;
  [key: string]: any;
};

/** 每日借还 */
export type DayLoanStatRow = {
  day?: string;
  date?: string;
  allCount?: number;
  returnCount?: number;
  timeoutCount?: number;
  [key: string]: any;
};

export const materialStatisticsApi = {
  getMaterialInventory: (type: string) =>
    axiosInstance.get('/iscs/statistics-api/getMaterialInventory', { params: { type } }),
  getInventorySum: () => axiosInstance.get('/iscs/statistics-api/getInventorySum'),
  exportMaterialInventory: () =>
    axiosInstance.post('/iscs/statistics-api/exportMaterialInventory', undefined, {
      responseType: 'blob',
    }),
  exportBaseData: (params: Record<string, any>) =>
    axiosInstance.post('/iscs/statistics-api/exportBaseData', params, { responseType: 'blob' }),
  exportClaimAndReturn: (params: Record<string, any>) =>
    axiosInstance.post('/iscs/statistics-api/exportClaimAndReturn', params, { responseType: 'blob' }),
  getCabinetStatistics: (params: StatisticsQuery) =>
    axiosInstance.get<CabinetStatRow[] | unknown>('/iscs/statistics-api/getCabinetStatistics', { params }),
  getMaterialsLoanStatistics: (params: StatisticsQuery) =>
    axiosInstance.get<MaterialsLoanStatRow[] | unknown>('/iscs/statistics-api/getMaterialsLoanStatistics', {
      params,
    }),
  getMaterialsStatusStatistics: (params: StatisticsQuery) =>
    axiosInstance.get<MaterialsTypeCountRow[] | unknown>('/iscs/statistics-api/getMaterialsStatusStatistics', {
      params,
    }),
  getMaterialsChangeStatistics: (params: StatisticsQuery) =>
    axiosInstance.get<MaterialsTypeCountRow[] | unknown>('/iscs/statistics-api/getMaterialsChangeStatistics', {
      params,
    }),
  getDayLoanStatistics: (params: StatisticsQuery) =>
    axiosInstance.get<DayLoanStatRow[] | unknown>('/iscs/statistics-api/getDayLoanStatistics', { params }),
  getHomePage: (params: StatisticsQuery) =>
    axiosInstance.get('/iscs/statistics-api/getHomePage', { params }),
};

export function toStatArray<T>(res: T[] | { list?: T[]; records?: T[] } | null | undefined): T[] {
  if (res == null) return [];
  if (Array.isArray(res)) return res;
  if (Array.isArray((res as { list?: T[] }).list)) return (res as { list: T[] }).list;
  if (Array.isArray((res as { records?: T[] }).records)) return (res as { records: T[] }).records;
  return [];
}
