import dayjs, { type Dayjs } from 'dayjs';


export interface ReportModel {
  id: string;
  time?: string;
  analysable?: boolean;
  scheme?: { show: boolean; editable?: boolean };
  [ key: string ]: any;
}

export interface ReportSchemeModel {
  id: string;
  time?: string;
  process?: string;
  children: ReportSchemeItemModel[];
}

export interface ReportSchemeItemModel {
  category: string;
  id: string;
  name?: string;
  type: 'acupoint' | 'medicine' | 'other',
  content?: Record<string, string>[];
  descriptions?: { id?: string; name: string; description: string }[]
}

export function transformReportSchemeItem(category: string, data: Record<string, any>): ReportSchemeItemModel {
  let type: ReportSchemeItemModel['type'] = 'other';
  if ( data.items?.length ) {
    if ( data.items.every((t: any) => t?.type === 'acupoint' && t.name) ) type = data.items[ 0 ].type;
    if ( data.items.every((t: any) => t?.type === 'medicine' && t.name) ) type = data.items[ 0 ].type;
  }
  return {
    category,
    id: data.id,
    name: data.name || void 0,
    type, content: data.items,
    descriptions: data.attrs,
  };
}

export function transformReportScheme(data: Record<string, any>): ReportSchemeModel {
  const { healthAnalysisReportId, types, ..._data } = data;
  return {
    ..._data,
    id: healthAnalysisReportId,
    time: dayjs(data.time).format('YYYY/MM/DD'),
    children: types?.flatMap((item: any) => {
      const category = item?.type;
      return category ? item?.groups?.map((group: any) => transformReportSchemeItem(category, group)) : void 0;
    })?.filter(Boolean) ?? [],
  };
}

export interface ReportIndicatorModel {
  id: string;
  name: string;
  unit: string;
  range: [ number, number ];
  editor: {
    min: number;
    max: number;
    precision?: number;
    step?: number;
    disabled?: boolean;
    prefix?: string;
    suffix?: string;
  };

  records: ReportIndicatorRecordModel[];
  value?: number;
}

export interface ReportIndicatorRecordModel {
  id: string;
  name: string;
  unit: string;
  value: number;
  abnormal: -1 | 0 | 1 | null;
  time: Dayjs;
}

function transformIndicatorRecords(model: ReportIndicatorModel, data?: any[]) {
  const getAbnormal = (value: string) => {
    if ( value?.includes('高') ) return 1 as const;
    if ( value?.includes('低') ) return -1 as const;
    return null;
  };

  const records = data?.map<ReportIndicatorRecordModel>((item: any) => {
    return {
      ...model,
      value: item?.quotaVal,
      abnormal: item?.abnormal ? getAbnormal(item.abnormalDesc) : 0,
      time: dayjs(item?.time2),
    };
  }) ?? [];
  return records.sort((a, b) => b.time?.toDate().getTime() - a.time?.toDate().getTime());
}

export function transformIndicators(data: any[]) {
  return data?.map((item: any) => {
    const model: Partial<ReportIndicatorModel> = {
      id: item.quotaId?.toString(),
      name: item.name,
      unit: item.unit,
    };
    const records = transformIndicatorRecords(<ReportIndicatorModel> model, item.patientQuotaRecordDTOS);
    return {
      ...model,
      range: [ item.minVal, item.maxVal ],
      editor: {
        disabled: item.isAuto === 'Y',
        min: +item.inputMin,
        max: +item.inputMax,
        precision: { 0: 2, 1: 1 }[ <number> item.inputPrecision ] || 0,
        step: { 2: 2 }[ <number> item.inputPrecision ] || 1,
        suffix: item.unit,
      },
      records,
      value: records[ 0 ]?.value,
    } as ReportIndicatorModel;
  }) ?? [];
}

export interface AnalysisRecordModel {
  pickedSymptom: string;
  algorithmInferSymptom: string;
  upImg: string;
  downImg: string;
  faceImg: string;
  recordDate: string;
}

export function transformAnalysisRecord(data: any): AnalysisRecordModel {
  return {
    ...data,
    recordDate: data?.analysisEndTime ?? dayjs(data.createTime).format('YYYY/MM/DD'),
  };
}
