import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";

/**
 * HorizonTabs — the single shared "trailing window" selector for analytics and
 * overview cards. The value is a number of days; the server interprets it as a
 * business-day window ending today (see inventory-server/docs/TIME.md).
 *
 * Replaces the per-card duplication this consolidated:
 *   - SalesMetrics       (shadcn <Tabs>,    7 / 30 / 90)
 *   - InventoryFlow      (raw button-group, 30 / 90)
 *   - InventoryTrends    (raw button-group, 30 / 90 / 365)
 *   - InventoryValueTrend(raw button-group, 30 / 90 / 365)
 *
 * Note: ForecastMetrics keeps its own selector — its third option ("EOY") is a
 * forward, calendar-anchored forecast horizon, not a trailing window.
 */

const HORIZON_LABELS: Record<number, string> = {
  7: "7D",
  14: "14D",
  30: "30D",
  90: "90D",
  365: "1Y",
};

export interface HorizonTabsProps {
  /** Currently selected trailing window, in days. */
  value: number;
  /** Called with the newly selected number of days. */
  onChange: (days: number) => void;
  /** Day options to render, left → right. Defaults to the full set. */
  options?: number[];
  /** Optional className forwarded to the Tabs root. */
  className?: string;
}

export function HorizonTabs({
  value,
  onChange,
  options = [7, 30, 90, 365],
  className,
}: HorizonTabsProps) {
  return (
    <Tabs
      value={String(value)}
      onValueChange={(v) => onChange(Number(v))}
      className={className}
    >
      <TabsList>
        {options.map((days) => (
          <TabsTrigger key={days} value={String(days)}>
            {HORIZON_LABELS[days] ?? `${days}D`}
          </TabsTrigger>
        ))}
      </TabsList>
    </Tabs>
  );
}

export default HorizonTabs;
