import { useMemo } from "react";

export type BabyStage = "newborn" | "infant" | "sitter" | "crawler" | "toddler" | "walker";

export interface StageInfo {
  stage: BabyStage;
  ageMonths: number;
  label: string;
  nextStageAt: number; // months until next stage
  emoji: string;
}

const STAGES: { stage: BabyStage; minMonths: number; maxMonths: number; label: string; emoji: string }[] = [
  { stage: "newborn",  minMonths: 0,  maxMonths: 3,  label: "Newborn (0–3 mo)",  emoji: "🍼" },
  { stage: "infant",   minMonths: 3,  maxMonths: 6,  label: "Infant (3–6 mo)",   emoji: "👶" },
  { stage: "sitter",   minMonths: 6,  maxMonths: 9,  label: "Sitter (6–9 mo)",   emoji: "🧸" },
  { stage: "crawler",  minMonths: 9,  maxMonths: 12, label: "Crawler (9–12 mo)", emoji: "🐛" },
  { stage: "toddler",  minMonths: 12, maxMonths: 24, label: "Toddler (1–2 yr)",  emoji: "🚶" },
  { stage: "walker",   minMonths: 24, maxMonths: Infinity, label: "Walker (2+ yr)", emoji: "🏃" },
];

export function useStageCheck(birthDate: string | null): StageInfo | null {
  return useMemo(() => {
    if (!birthDate) return null;
    const birth = new Date(birthDate);
    const now = new Date();
    const ageMonths = Math.floor((now.getTime() - birth.getTime()) / (30.44 * 24 * 3600 * 1000));

    const current = STAGES.find(s => ageMonths >= s.minMonths && ageMonths < s.maxMonths)
      ?? STAGES[STAGES.length - 1];
    const nextStageAt = current.maxMonths === Infinity ? 0 : current.maxMonths - ageMonths;

    return { stage: current.stage, ageMonths, label: current.label, nextStageAt, emoji: current.emoji };
  }, [birthDate]);
}
