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

const prefix = 'doctor/';

/** 构建医生卡片查询参数：id 与 su_id 至少传一个 */
function buildDoctorCardParams(
  id?: number,
  suId?: number,
  extra: Record<string, any> = {},
) {
  const params: Record<string, any> = { ...extra };
  if (id) {
    params.id = id;
  }
  if (suId) {
    params.su_id = suId;
  }
  return params;
}

export async function getDoctorCardApi(id?: number, suId?: number) {
  return requestClient.get<any>(`${prefix}card`, {
    params: buildDoctorCardParams(id, suId),
  });
}

export async function getDoctorCardStatsApi(
  id?: number,
  suId?: number,
  storeId?: number,
) {
  return requestClient.get<any>(`${prefix}card-stats`, {
    params: buildDoctorCardParams(id, suId, { store_id: storeId }),
  });
}

export async function getDoctorCardPrescriptionsApi(
  id?: number,
  suId?: number,
  params: Record<string, any> = {},
) {
  return requestClient.get<any>(`${prefix}card-prescriptions`, {
    params: buildDoctorCardParams(id, suId, params),
  });
}

export async function getDoctorCardPatientsApi(
  id?: number,
  suId?: number,
  params: Record<string, any> = {},
) {
  return requestClient.get<any>(`${prefix}card-patients`, {
    params: buildDoctorCardParams(id, suId, params),
  });
}

export async function getDoctorCardOrdersApi(
  id?: number,
  suId?: number,
  params: Record<string, any> = {},
) {
  return requestClient.get<any>(`${prefix}card-orders`, {
    params: buildDoctorCardParams(id, suId, params),
  });
}

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

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

/** 超管/系统管理员更新医生 service_user 账号资料 */
export async function updateDoctorServiceUserApi(data: {
  su_id: number;
  mobile?: string;
  nickname?: string;
  avatar?: string;
}) {
  return requestClient.post<any>(`${prefix}update-service-user`, data);
}
