import { requestClient } from '#/api/request';

/** 病历与词条 API（admin medical-record/） */
const prefix = 'medical-record/';

export async function getMedicalRecordEntryList(data: any) {
  return requestClient.get<any>(`${prefix}list`, { params: data });
}

export async function createMedicalRecordEntry(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}create`, data);
}

export async function updateMedicalRecordEntry(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}update`, data);
}

export async function deleteMedicalRecordEntry(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}delete`, data);
}

export async function getMedicalRecordFieldOptions() {
  return requestClient.get<any>(`${prefix}field-options`);
}

export async function searchMedicalRecordEntry(data: {
  field_code: string;
  keyword?: string;
  store_id?: number;
}) {
  return requestClient.get<any>(`${prefix}search-entry`, { params: data });
}

/** 我的常用词条（医生维度） */
export async function getMyMedicalRecordEntryList(data: {
  field_code?: string;
  limit?: number;
}) {
  return requestClient.get<any>(`${prefix}my-entry-list`, { params: data });
}

/** 加入常用词条 */
export async function addMyMedicalRecordEntry(entry_id: number) {
  return requestClient.post<any>(`${prefix}add-my-entry`, { entry_id });
}

/** 删除常用词条（常用记录 id） */
export async function deleteMyMedicalRecordEntry(id: number) {
  return requestClient.post<any>(`${prefix}delete-my-entry`, { id });
}

export async function getMedicalRecord(data: {
  register_id: number;
  store_id?: number;
}) {
  return requestClient.get<any>(`${prefix}get-record`, { params: data });
}

export async function saveMedicalRecord(data: Record<string, any>) {
  return requestClient.post<any>(`${prefix}save-record`, data);
}

export async function clearMedicalRecord(data: {
  register_id: number;
  store_id?: number;
}) {
  return requestClient.post<any>(`${prefix}clear-record`, data);
}

export async function getLatestMedicalRecord(data: {
  user_patient_id: number;
  register_id?: number;
  store_id?: number;
}) {
  return requestClient.get<any>(`${prefix}latest-by-patient`, { params: data });
}

/** 按就诊人拉取病历列表（查看历史） */
export async function getMedicalRecordListByPatient(data: {
  user_patient_id: number;
  store_id?: number;
  limit?: number;
}) {
  return requestClient.get<any>(`${prefix}list-by-patient`, { params: data });
}

/** AI 写病历（需先填主诉；可选传 medical_record 上下文字段） */
export async function aiGenerateMedicalRecord(data: {
  register_id: number;
  store_id?: number;
  chief_complaint?: string;
  medical_record?: Record<string, any>;
}) {
  return requestClient.post<any>(`${prefix}ai-generate-medical-record`, data);
}

/** 本挂号 AI 生成历史（与接诊共用 doctor-reception 接口） */
export async function aiListGenerations(data: {
  register_id: number;
  scene?: string;
  limit?: number;
}) {
  return requestClient.get<any>('doctor-reception/ai-list-generations', {
    params: data,
  });
}

/** AI 生成详情（点选历史） */
export async function aiGenerationDetail(data: { id: number }) {
  return requestClient.get<any>('doctor-reception/ai-generation-detail', {
    params: data,
  });
}

/** Excel 解析后批量导入词条 */
export async function importMedicalRecordEntry(data: {
  scope: 'public' | 'store';
  rows: Array<Record<string, any>>;
}) {
  return requestClient.post<any>(`${prefix}import-entry`, data);
}

/** 手动同步空拼音首拼 */
export async function syncMedicalRecordEntryPinyin(data?: {
  scope?: 'public' | 'store' | '';
}) {
  return requestClient.post<any>(`${prefix}sync-pinyin`, data || {});
}
