import type { MetricsSnapshotView, MetricsTimeBucket } from "@batch-cooking/shared";

/**
 * Pure helpers for `DashboardPage` — number/date formatting and the tile
 * list, kept out of the `.tsx` (repo convention: no derivation logic in a
 * component file) so they're trivially testable.
 */

/** French-grouped integer, e.g. `1234` → `"1 234"`. */
export function formatCount(value: number): string {
  return value.toLocaleString("fr-FR");
}

/** `"2026-08-28"` → `"28/08"` for a compact chart axis tick. */
export function shortDay(isoDate: string): string {
  const [, month, day] = isoDate.split("-");
  return `${day}/${month}`;
}

/** Sum of a time series — the "total over the window" figure shown next to each chart. */
export function seriesTotal(buckets: MetricsTimeBucket[]): number {
  return buckets.reduce((sum, bucket) => sum + bucket.count, 0);
}

/** One KPI tile: an i18n label key and the snapshot value it reads. */
export interface KpiTile {
  labelKey: string;
  value: number;
}

/**
 * The dashboard's KPI tiles, in display order. `labelKey` resolves under
 * `admin.dashboard.kpi.*`. Kept here (not inline in JSX) so the set is one
 * list to reorder/extend.
 */
export function kpiTiles(snapshot: MetricsSnapshotView): KpiTile[] {
  return [
    { labelKey: "users", value: snapshot.users },
    { labelKey: "households", value: snapshot.households },
    { labelKey: "activeHouseholds", value: snapshot.activeHouseholds },
    { labelKey: "recipes", value: snapshot.recipes },
    { labelKey: "recipesImported", value: snapshot.recipesImported },
    { labelKey: "plannings", value: snapshot.plannings },
    { labelKey: "planningItems", value: snapshot.planningItems },
    { labelKey: "favorites", value: snapshot.favorites },
    { labelKey: "corrections", value: snapshot.corrections },
    { labelKey: "correctionsUnconsumed", value: snapshot.correctionsUnconsumed },
    { labelKey: "trainingSuggestions", value: snapshot.trainingSuggestions },
    { labelKey: "admins", value: snapshot.admins },
  ];
}
