import type { FdiToothId } from '@/types/treatment';

/**
 * Mean mesiodistal crown widths (mm) — buccal chart relative scale.
 *
 * Anchored on classic Wheeler's Dental Anatomy values (widely used teaching
 * standard). Population studies vary slightly, but ratios are stable:
 * maxillary central ≈ 1.3× lateral (8.5 / 6.5).
 *
 * Sources: Wheeler's Dental Anatomy; consistent with published odontometric
 * means (e.g. maxillary CI ~8.5–9.2, LI ~6.5–7.4, C ~7.5–8.0, P ~7.0–7.4,
 * M1 ~10.0–10.5 mm).
 */
export const FDI_MESIODISTAL_MM: Record<FdiToothId, number> = {
  // Maxillary
  '11': 8.5,
  '21': 8.5,
  '12': 6.5,
  '22': 6.5,
  '13': 7.5,
  '23': 7.5,
  '14': 7.0,
  '24': 7.0,
  '15': 7.0,
  '25': 7.0,
  '16': 10.0,
  '26': 10.0,
  '17': 9.0,
  '27': 9.0,
  '18': 8.5,
  '28': 8.5,
  // Mandibular
  '41': 5.0,
  '31': 5.0,
  '42': 5.5,
  '32': 5.5,
  '43': 7.0,
  '33': 7.0,
  '44': 7.0,
  '34': 7.0,
  '45': 7.0,
  '35': 7.0,
  '46': 11.0,
  '36': 11.0,
  '47': 10.5,
  '37': 10.5,
  '48': 10.0,
  '38': 10.0,
};

/** Reference tooth for relative scale (maxillary lateral). */
export const FDI_SCALE_REFERENCE: FdiToothId = '12';

/** CSS rem per mm of mesiodistal crown width (overall chart size). */
export const TOOTH_REM_PER_MM = 0.34;

/** Extra column padding beyond glyph width (keep small so teeth sit close). */
export const TOOTH_COLUMN_PAD_REM = 0.04;

export function getToothMesiodistalMm(fdi: FdiToothId): number {
  return FDI_MESIODISTAL_MM[fdi];
}

/** Width relative to maxillary lateral (12). e.g. 11 → ~1.31 */
export function getToothRelativeWidth(fdi: FdiToothId): number {
  return FDI_MESIODISTAL_MM[fdi] / FDI_MESIODISTAL_MM[FDI_SCALE_REFERENCE];
}

export function getToothGlyphWidthRem(fdi: FdiToothId): number {
  return Number((FDI_MESIODISTAL_MM[fdi] * TOOTH_REM_PER_MM).toFixed(3));
}

export function getToothColumnWidthRem(fdi: FdiToothId): number {
  return Number((getToothGlyphWidthRem(fdi) + TOOTH_COLUMN_PAD_REM).toFixed(3));
}
