export type Moment =
  | "FASTING"
  | "BEFORE_LUNCH"
  | "AFTER_LUNCH"
  | "BEFORE_DINNER"
  | "AFTER_DINNER"
  | "BEDTIME"
  | "OTHER";

export const MOMENTS: { value: Moment; label: string; short: string }[] = [
  { value: "FASTING", label: "Matin (à jeun)", short: "A jeun" },
  { value: "BEFORE_LUNCH", label: "Avant déjeuner", short: "Av. Déj" },
  { value: "AFTER_LUNCH", label: "Après déjeuner (2h)", short: "Ap. Déj" },
  { value: "BEFORE_DINNER", label: "Avant dîner", short: "Av. Dîner" },
  { value: "AFTER_DINNER", label: "Après dîner (2h)", short: "Ap. Dîner" },
  { value: "BEDTIME", label: "Au coucher", short: "Coucher" },
  { value: "OTHER", label: "Autre moment", short: "Autre" },
];

export const MOMENT_LABELS: Record<Moment, string> = {
  FASTING: "Matin (à jeun)",
  BEFORE_LUNCH: "Avant déjeuner",
  AFTER_LUNCH: "Après déjeuner (2h)",
  BEFORE_DINNER: "Avant dîner",
  AFTER_DINNER: "Après dîner (2h)",
  BEDTIME: "Au coucher",
  OTHER: "Autre moment",
};

// Cibles glycémiques types (peuvent varier selon les recommandations médicales)
export const TARGETS: Record<Moment, { min: number; max: number }> = {
  FASTING: { min: 0.7, max: 1.3 },
  BEFORE_LUNCH: { min: 0.7, max: 1.3 },
  AFTER_LUNCH: { min: 0.7, max: 1.8 },
  BEFORE_DINNER: { min: 0.7, max: 1.3 },
  AFTER_DINNER: { min: 0.7, max: 1.8 },
  BEDTIME: { min: 1.0, max: 1.5 }, // Cible souvent plus haute au coucher pour éviter les hypos nocturnes
  OTHER: { min: 0.7, max: 1.6 },
};

export const HYPO_THRESHOLD = 0.7;

export type Status = "hypo" | "in-range" | "hyper";

export function statusFor(value: number, moment: Moment): Status {
  const t = TARGETS[moment] || TARGETS.OTHER;
  if (value < t.min) return "hypo";
  if (value > t.max) return "hyper";
  return "in-range";
}

export const STATUS_STYLE: Record<
  Status,
  { label: string; color: string; bg: string; border: string }
> = {
  hypo: {
    label: "Hypoglycémie",
    color: "text-amber-700",
    bg: "bg-amber-100",
    border: "border-amber-300",
  },
  "in-range": {
    label: "Dans la cible",
    color: "text-emerald-700",
    bg: "bg-emerald-100",
    border: "border-emerald-300",
  },
  hyper: {
    label: "Hyperglycémie",
    color: "text-rose-700",
    bg: "bg-rose-100",
    border: "border-rose-300",
  },
};

export function formatValue(value: number): string {
  return value.toFixed(2).replace(".", ",") + " g/L";
}

// Estimation HbA1c à partir de la moyenne glycémique (ADAG formula)
export function estimateHbA1c(avgGperL: number): number {
  const avgMgDl = avgGperL * 100;
  return (avgMgDl + 46.7) / 28.7;
}
