export type Moment = "FASTING" | "LUNCH" | "DINNER";

export const MOMENTS: { value: Moment; label: string; short: string }[] = [
  { value: "FASTING", label: "Matin (à jeun)", short: "Matin" },
  { value: "LUNCH", label: "Midi (2h après repas)", short: "Midi" },
  { value: "DINNER", label: "Soir (2h après repas)", short: "Soir" },
];

export const MOMENT_LABELS: Record<Moment, string> = {
  FASTING: "Matin (à jeun)",
  LUNCH: "Midi (2h après repas)",
  DINNER: "Soir (2h après repas)",
};

export const TARGETS: Record<Moment, { min: number; max: number }> = {
  FASTING: { min: 0.7, max: 1.3 },
  LUNCH: { min: 0.7, max: 1.8 },
  DINNER: { min: 0.7, max: 1.8 },
};

export const HYPO_THRESHOLD = 0.7;
export const HYPER_THRESHOLD_FASTING = 1.3;
export const HYPER_THRESHOLD_POSTPRANDIAL = 1.8;

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

export function statusFor(value: number, moment: Moment): Status {
  const t = TARGETS[moment];
  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)
// HbA1c (%) = (avg_mg_dl + 46.7) / 28.7 ; avg_g_l * 100 = avg_mg_dl
export function estimateHbA1c(avgGperL: number): number {
  const avgMgDl = avgGperL * 100;
  return (avgMgDl + 46.7) / 28.7;
}
