export interface DiagnosisReportDTO {
  patientMedicalRecordId?: string;
  healthAnalysisReportId: string;
  medicalTime: string;
  medicalDepartment: string;
  medicalDoctor: string;
  diagnosis: string;
  symptom: string;
  syndrome: string;
  medicalContentJson?: MedicalHistoryDTO;
}

export interface DiagnosisReportVO {
  id: string;
  date: string;
  department: {
    id?: string;
    name: string;
  };
  doctor: {
    id?: string;
    name: string;
  };
  disease: {
    id?: string;
    name: string;
  };
  symptom: {
    id?: string;
    name: string;
  };

  medicalHistory?: MedicalHistoryVO;
}

export function fromDiagnosisReport(data: DiagnosisReportDTO, index?: number): DiagnosisReportVO {
  const vo = {
    id: data.patientMedicalRecordId ?? crypto.randomUUID(),
    date: data.medicalTime,
    department: { name: data.medicalDepartment },
    doctor: { name: data.medicalDoctor },
    disease: { name: data.diagnosis },
    symptom: { name: data.symptom },

    medicalHistory: data.medicalContentJson ? fromMedicalHistory(data.medicalContentJson) : void 0,
  } as DiagnosisReportVO;
  if (vo.medicalHistory) {
    vo.medicalHistory.diagnosis.department.id ??= vo.department.id;
    vo.medicalHistory.diagnosis.department.name ??= vo.department.name;
  }
  return vo;
}

export interface PatientVO {
  id: string;
  name: string;
  avatar?: string;
  gender?: '0' | '1';
  age?: string | number;

  phone?: string;
  identityCard?: string;

  address?: string;
}

export interface PatientDTO {
  name: string;
  sex: string;
  age: string;
  phone: string;
  idCard: string;
  address: string;
}

export interface MedicalHistoryDTO extends PatientDTO {
  pid: string;
  // 就诊日期
  treatmentTime: string;

  tcmDiagnosis: string;
  westernMedicineDiagnosis: string;

  treatment: string;
}

export interface MedicalHistoryVO {
  id: string;
  date: string;
  patient: PatientVO;

  diagnosis: {
    TCM: string;
    ICD: string;
    therapies: string;
    descriptions: { label: string; value: string }[];

    department: { id?: string; name: string };
  };

  prescriptions: HerbalMedicinePrescriptionVO[];
}

interface HerbalMedicinePrescriptionVO {
  title: string;
  count: number;
  method: { label: string; value?: string };
  dosage: { label: string; value?: string };
  dosageType: { label: string; value?: string };
  frequency: { label: string; value?: string };
  frequencyTime: { label: string; value?: string };
  decoction: { label: string;  };
  remark: string;
  medicines: HerbalMedicineVO[];
}

interface HerbalMedicineVO {
  id: string;
  name: string;
  dosage: number;
  unit: string;
  usage: string;
}

export function fromMedicalHistory(data: MedicalHistoryDTO & Record<string, any>): MedicalHistoryVO {
  return {
    id: data.pid,
    date: data.treatmentTime,
    patient: {
      id: '',
      name: data.name,
      gender: data.sex as PatientVO['gender'],
      age: data.age,
      phone: data.phone,
      identityCard: data.idCard,
      address: data.address,
    },
    diagnosis: {
      TCM: data.tcmDiagnosis,
      ICD: data.westernMedicineDiagnosis,
      therapies: data.treatment,
      department: { name: data.clinic },

      descriptions: [
        { label: '主诉', value: data['chiefcomplaint'] },
        { label: '现病史', value: data['historypresent'] },
        { label: '既往史', value: data['pasthistory'] },
        { label: '中医四诊', value: data['fourmedicine'] },
        { label: '体格检查', value: data['physicalexamination'] },
        { label: '辅助检查', value: data['supplementaryexamination'] },
      ],
    },
    prescriptions: [
      ...(data.zhongyaochufangVos?.map?.(fromHerbalMedicinePrescription) ?? []),
      ...(data.zhongyaozhijiVos?.map?.(fromPreparationPrescription) ?? [])
    ],
  };
}

function fromHerbalMedicinePrescription(data: Record<string, any>, index?: number): HerbalMedicinePrescriptionVO {
  return {
    title: '中药处方' + (index != null ? index + 1 : ''),
    count: data.num,
    method: { label: data['usestr'] },
    dosage: { label: data['strongFried'] ? `每次${data['strongFried']}` : '', value: data['strongFried'] },
    dosageType: { label: data['dosageForm'] },
    frequency: { label: data['frequency'] },
    frequencyTime: { label: data['eatMedicineTime'] },
    remark: data.command,
    decoction: { label: data['agency'] === '1' ? '代煎' : '' },
    medicines: data.prescriptiondetailVos?.map?.(fromHerbalMedicine) ?? [],
  };
}

function fromPreparationPrescription(data: Record<string, any>): {} {
  return {};
}

function fromHerbalMedicine(data: Record<string, any>): HerbalMedicineVO {
  return {
    id: data.drugid,
    name: data.drugName,
    dosage: data.dose,
    unit: data.unit,
    usage: data.usagestr,
  };
}
