"use client"; import { Check, Sparkles } from "lucide-react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { useTranslations } from "@/lib/i18n/context"; import { CycleDisclaimer } from "./cycle-disclaimer"; import { FERTILE_HUE, FLOW_HUE, OVULATION_HUE } from "./phase-tokens"; import type { CyclePrediction, CycleHistoryResponse } from "./types"; /** * v1.15.0 — the predictions panel. * * Shows the next-period RANGE (a window, with a confidence pill), the fertile * window (already goal-gated server-side — the API nulls it unless TTC, so we * just render what is present), the "still learning" state for < 3 cycles or * raw-chart mode, the cycle-history stats, and the fixed non-medical * disclaimer. Never a single dated next-period claim — always the range. */ function formatDate(d: string): string { // Render at noon UTC so the YYYY-MM-DD never rolls a day across tz. return new Date(`${d}T12:00:00Z`).toLocaleDateString(undefined, { month: "short", day: "numeric", }); } function confidenceTone(c: number): { labelKey: string; variant: "default" | "secondary" | "outline"; } { if (c >= 0.66) return { labelKey: "cycle.predictions.confidenceHigh", variant: "default" }; if (c >= 0.33) return { labelKey: "cycle.predictions.confidenceMedium", variant: "secondary", }; return { labelKey: "cycle.predictions.confidenceLow", variant: "outline" }; } export interface PredictionsPanelProps { prediction: CyclePrediction | null; rawChartMode: boolean; history: CycleHistoryResponse | undefined; /** Fallback disclaimer when no prediction carries one. */ fallbackDisclaimer?: string; /** The active cycle goal — only AVOID_PREGNANCY surfaces the (deliberate) * contraceptive-safety caveat here; every other goal drops the generic * "predictions are estimates" line so it does not repeat under every graph. */ goal?: string; } export function PredictionsPanel({ prediction, rawChartMode, history, fallbackDisclaimer, goal, }: PredictionsPanelProps) { const { t } = useTranslations(); // Only the AVOID_PREGNANCY goal shows a disclaimer here: it surfaces the // fertile window, so the stronger "not a contraceptive method" caveat is a // deliberate medical safety note. For all other goals the generic // estimate-caveat is suppressed (it already lives once with the data). const disclaimer = goal === "AVOID_PREGNANCY" ? (prediction?.disclaimer ?? fallbackDisclaimer ?? "") : ""; return (
{rawChartMode ? ( ) : prediction == null ? (

{t("cycle.predictions.none")}

) : prediction.stillLearning ? ( <> ) : ( <> {prediction.fertileWindowStart && prediction.fertileWindowEnd ? ( ) : null} {prediction.predictedOvulation ? ( ) : null} )} {disclaimer ? : null}
); } function NextPeriod({ prediction }: { prediction: CyclePrediction }) { const { t } = useTranslations(); const tone = confidenceTone(prediction.confidence); return (
{t(tone.labelKey)}
); } function Row({ hue, label, value, }: { hue: string; label: string; value: string; }) { return (
); } function Learning({ title, body }: { title: string; body: string }) { return (

{title}

{body}

); } /** Inclusive day span between two `YYYY-MM-DD` dates (noon-UTC anchored). */ function daySpan(from: string, to: string): number { const a = Date.parse(`${from}T12:00:00Z`); const b = Date.parse(`${to}T12:00:00Z`); return Math.round((b - a) / 86_400_000) + 1; } function HistoryCard({ history, }: { history: CycleHistoryResponse | undefined; }) { const { t } = useTranslations(); const stats = history?.stats; // Most-recent observed cycles (the spread the regularity number summarises — // seeing it builds trust; the data was already on the wire). QA H2. const observed = (history?.cycles ?? []) .filter((c) => !c.isPredicted) .slice(0, 6); return ( {t("cycle.history.title")} {stats ? ( {t(`cycle.history.regularity${stats.regularity}`)} ) : null} {!stats || stats.avgLengthDays == null ? (

{t("cycle.history.none")}

) : (
{stats.lengthVariabilityDays != null ? ( ) : null} {stats.avgPeriodLengthDays != null ? ( ) : null}
)} {observed.length > 0 ? (

{t("cycle.history.recentCycles")}

    {observed.map((c) => { const periodLen = c.periodEndDate != null ? daySpan(c.startDate, c.periodEndDate) : null; return (
  • {formatDate(c.startDate)} {" – "} {c.endDate ? formatDate(c.endDate) : t("cycle.history.ongoing")} {c.lengthDays != null ? t("cycle.history.days", { count: c.lengthDays }) : "—"} {periodLen != null ? ( {t("cycle.history.periodDays", { count: periodLen })} ) : null} {c.ovulationConfirmed ? ( ) : null}
  • ); })}
) : null}
); } function Stat({ label, value }: { label: string; value: string }) { return (
{label}
{value}
); }