import { type List, type PatientModel, type PatientQuery, type PatientReportModel, type PatientTagModel, transformPatient } from '@/model';
import request from '@/request/alova';

import type { PatientTagVO, PatientTagDTO } from '@/model/patient.model';
import { fromPatientTag } from '@/model/patient.model';

export function patientsHistoryMethod(page: number, size: number, query?: PatientQuery) {
  return request.Post<List<PatientReportModel>, List<any>>(`/fdhb-pc/patientInfoManage/pageMyPatient`, query ?? {}, {
    params: { pageNum: page, pageSize: size },
    transform(data, headers) {
      return {
        total: data.total,
        data: data.data.map((item: any) =>
          Object.assign(transformPatient(item), {
            report: {
              id: item.healthAnalysisReportId,
              time: item.reportTime,
            },
            createTime: item.createTime,
          })
        ),
      };
    },
  });
}

export function appendPatientMethod(data: Record<string, any>) {
  const { disease: diagnosis, date: medicalTime, department: medicalDepartment, doctor: medicalDoctor, tags, ...patient } = data;
  return request.Post(`/fdhb-pc/patientMedicalManage/addPatientMedical`, { ...patient, diagnosis, medicalTime, medicalDepartment, medicalDoctor }, {});
}

export function patientsRoomMethod(query?: PatientQuery) {
  return request.Post<PatientReportModel[], any[]>(`/fdhb-pc/patientInfoManage/pendPatient`, query ?? {}, {
    params: {},
    transform(data, headers) {
      return data.map((item: any) =>
        Object.assign(transformPatient(item), {
          uid: item.healthAnalysisReportId,
          report: {
            id: item.healthAnalysisReportId,
            time: item.reportTime,
          },
          createTime: item.createTime,
        })
      );
    },
    cacheFor: 1000,
  });
}

export function patientMethod(id: string) {
  console.log(id, 'patientMethod-id');
  return request.Get<PatientModel, any>(`/fdhb-pc/patientInfoManage/getPatientInfoDetailById`, {
    name: 'patient-report',
    cacheFor: null,
    params: { patientId: id },
    transform(data, headers) {
      data.patientId ??= id;
      return transformPatient(data);
    },
  });
}
// 患者基本信息修改
export function patientBasicInfoMethod(data: Record<string, any>) {
  return request.Post(`/fdhb-pc/patientInfoManage/updatePatientInfo`, data);
}
/**
 * @deprecated
 * @param id
 */
export function patientTags(id: string) {
  return request.Get<PatientTagModel, any[]>(`/fdhb-pc/patientInfoManage/getPatientTag?old`, {
    hitSource: 'update-tags',
    params: { patientId: id },
    transform(data, headers) {
      return {
        id,
        tags: data.map(({ id, name }) => ({ id, name })),
      };
    },
  });
}

/**
 * @deprecated
 * @param id
 */
export function patientTagsMethod(id: string) {
  return request.Get<PatientTagModel['tags'], any[]>(`/fdhb-pc/patientInfoManage/getPatientTag`, {
    hitSource: 'update-tags',
    params: { patientId: id },
  });
}

/**
 * @deprecated
 * @param id
 * @param data
 */
export function updatePatientTagsMethod(id: string, data: string[]) {
  return request.Post(`/fdhb-pc/patientInfoManage/updatePatientTag`, { patientId: id, tagIds: data }, { name: 'update-tags' });
}

export function getPatientTagsMethod(id: string) {
  return request.Get<PatientTagVO[], PatientTagDTO[]>(`/fdhb-pc/patientInfoManage/getPatientTag`, {
    hitSource: 'update-tags',
    params: { patientId: id },
    transform(data) {
      return data.map(fromPatientTag);
    },
  });
}

export function updatePatientTagMethod(id: string, tags: PatientTagVO[]) {
  const tagIds = tags.map(({ id }) => id);
  return request.Post(
    `/fdhb-pc/patientInfoManage/updatePatientTag`,
    { patientId: id, tagIds },
    {
      name: 'update-tags',
      transform() {
        return tags;
      },
    }
  );
}

export function searchTagsFromSelectableMethod() {
  return request.Post<PatientTagVO[], PatientTagDTO[]>(
    `/fdhb-pc/tagManage/selectTag`,
    {},
    {
      hitSource: /tag$/,
      transform(data) {
        return data.map(fromPatientTag);
      },
    }
  );
}

export function patientAnalysisCountMethod(id: string) {
  return request.Get(`/fdhb-pc/patientInfoManage/rechargeUseDetail`, {
    hitSource: 'update-analysis-count',
    params: { patientId: id },
    transform(data: any) {
      return {
        total: Math.max(0, data.usedCou),
        count: Math.max(0, data.residuedCou),
      };
    },
  });
}

export function updatePatientAnalysisCountMethod(id: string, count: number) {
  return request.Get(`/fdhb-pc/patientInfoManage/rechargeUpdate`, {
    name: 'update-analysis-count',
    params: { patientId: id, residuedCou: count },
    cacheFor: null,
  });
}

// 调养
export function carePatientMethod(id: string) {
  console.log(id, 'carePatientMethod-id');
  return request.Post(
    `/fdhb-pc/patientInfoManage/conditioning/${id}`,
    { patientId: id },
    {
      transform(data) {
        return data;
      },
    }
  );
}
