/**
 * disease-knowledge —— 病种 **canonical 身份**(tier-agnostic,三档共用)。
 *
 * 收口决策(2026-06):病种的**口语文案**(risks/advantages/复查时长)是**稳健模板**,
 *   已移 `tiers/stable/phrasing.ts`。本文件只保留 tier-agnostic 的**病种名映射**
 *   (subKey → 患者口径病种名),标准/深度档拿它得到病种 canonical 名,措辞自行组织。
 */

import { diagnosisCodeNameZh } from '@pac/types';

/** subKey(去 @tooth 后缀)→ 患者口径病种名 */
const SUBKEY_LABEL: Record<string, string> = {
  missing_tooth: '缺失牙',
  caries_no_filling: '龋齿',
  endo_no_rct: '牙髓/根尖周炎',
  perio_no_srp: '牙周炎',
  ortho_no_consult: '错颌畸形（牙齿不齐）',
  hard_tissue_damage: '牙体缺损',
  gum_alveolar_lesion: '牙龈/牙槽问题',
  impacted_tooth: '阻生牙',
  jaw_cyst: '颌骨囊肿',
  development_eruption: '牙齿萌出异常',
};

/** subKey(可带 @tooth 后缀)→ 病种名;未映射 → null */
export function diseaseLabelForSubKey(subKey: string | null | undefined): string | null {
  const base = (subKey ?? '').split('@')[0]!.trim();
  return base ? (SUBKEY_LABEL[base] ?? null) : null;
}

/** reason → 病种名(触发感知):
 *   lead=医生建议(无诊断)→ 用建议名(如"建议种植"),不套子场景诊断词("缺失牙");
 *   lead=诊断/影像(影像也是 K 诊断)→ subKey 病种名,以诊断为准。subKey 缺失用 fallbackLabel。 */
export function resolveDiseaseLabel(
  reason: { subKey?: string | null; leadTrigger?: { type: string; code: string } | null } | null | undefined,
  fallbackLabel: string,
): string {
  const lead = reason?.leadTrigger;
  if (lead?.type === 'recommendation' && lead.code) {
    return diagnosisCodeNameZh(lead.code) || diseaseLabelForSubKey(reason?.subKey) || fallbackLabel;
  }
  return diseaseLabelForSubKey(reason?.subKey) ?? fallbackLabel;
}
