/**
 * Canonical OEE traffic-light coloring (KAR-704 O5).
 *
 * Single source of truth for the green / amber / red status coloring used across
 * every OEE view. Built on the app's design tokens (`--success` / `--warning` /
 * `--destructive` / `--muted-foreground`) so it stays theme-aware (light + dark).
 *
 * Replaces the previous per-component copies that hardcoded a separate hex palette
 * (#008A00 / #FFCC00|#D97706 / #CC0000), drifted on the amber value, and ignored
 * dark mode entirely.
 *
 * Two output forms:
 *  - `oeeColorVar`  -> `var(--token)` for inline `style` and SVG/recharts colors
 *  - `oeeColorClass` -> Tailwind `text-*` class for `className` contexts
 *
 * For translucent badge fills, mix the returned var with `color-mix(...)` at the
 * call site (e.g. `color-mix(in srgb, ${color} 9%, transparent)`), which works
 * with CSS-var colors — the old `${hex}18` alpha concatenation did not.
 */

export interface OeeThreshold {
  /** value >= high -> success (green) */
  high: number
  /** value >= mid -> warning (amber); below -> destructive (red) */
  mid: number
}

/**
 * Per-factor thresholds. The overall OEE uses the loosest band; the sub-factors
 * are intentionally stricter (availability/performance tighter, quality tightest).
 * These boundaries are preserved verbatim from the pre-centralization call sites.
 */
export const OEE_THRESHOLDS = {
  oee: { high: 0.85, mid: 0.6 },
  availability: { high: 0.9, mid: 0.7 },
  performance: { high: 0.9, mid: 0.7 },
  quality: { high: 0.995, mid: 0.95 },
} as const satisfies Record<string, OeeThreshold>

/**
 * CSS color reference for inline styles and SVG/chart props (recharts `stroke`,
 * `borderLeftColor`, …). Returns a `var(--token)` so it follows the active theme.
 */
export function oeeColorVar(
  value: number | null | undefined,
  threshold: OeeThreshold = OEE_THRESHOLDS.oee,
): string {
  if (value == null) return "var(--muted-foreground)"
  if (value >= threshold.high) return "var(--success)"
  if (value >= threshold.mid) return "var(--warning)"
  return "var(--destructive)"
}

/** Tailwind text-color class for `className` contexts. */
export function oeeColorClass(
  value: number | null | undefined,
  threshold: OeeThreshold = OEE_THRESHOLDS.oee,
): string {
  if (value == null) return "text-muted-foreground"
  if (value >= threshold.high) return "text-success"
  if (value >= threshold.mid) return "text-warning"
  return "text-destructive"
}
