import { describe, expect, it } from "vitest"

import {
  OEE_THRESHOLDS,
  oeeColorClass,
  oeeColorVar,
} from "../colors"

/**
 * Tests for the canonical OEE traffic-light coloring (KAR-704 O5).
 *
 * Before centralization the green/amber/red coloring was duplicated in 6 places
 * with a drifting amber (#FFCC00 vs #D97706) and no dark-mode awareness. The
 * helper now resolves everything to the design tokens, so these tests pin the
 * threshold boundaries and the token mapping for both the inline-style (var)
 * and className variants.
 */

describe("oeeColorVar (inline-style / chart contexts)", () => {
  it("returns the muted-foreground token for null/undefined", () => {
    expect(oeeColorVar(null)).toBe("var(--muted-foreground)")
    expect(oeeColorVar(undefined)).toBe("var(--muted-foreground)")
  })

  it("maps the default OEE thresholds (0.85 / 0.6) to the status tokens", () => {
    expect(oeeColorVar(0.85)).toBe("var(--success)")
    expect(oeeColorVar(0.9)).toBe("var(--success)")
    expect(oeeColorVar(0.6)).toBe("var(--warning)")
    expect(oeeColorVar(0.84)).toBe("var(--warning)")
    expect(oeeColorVar(0.59)).toBe("var(--destructive)")
    expect(oeeColorVar(0)).toBe("var(--destructive)")
  })

  it("honours per-factor thresholds when passed", () => {
    // Quality is stricter: 0.995 / 0.95
    expect(oeeColorVar(0.96, OEE_THRESHOLDS.quality)).toBe("var(--warning)")
    expect(oeeColorVar(0.995, OEE_THRESHOLDS.quality)).toBe("var(--success)")
    expect(oeeColorVar(0.94, OEE_THRESHOLDS.quality)).toBe("var(--destructive)")
    // Availability: 0.9 / 0.7
    expect(oeeColorVar(0.9, OEE_THRESHOLDS.availability)).toBe("var(--success)")
    expect(oeeColorVar(0.7, OEE_THRESHOLDS.availability)).toBe("var(--warning)")
    expect(oeeColorVar(0.69, OEE_THRESHOLDS.availability)).toBe("var(--destructive)")
  })
})

describe("oeeColorClass (className contexts)", () => {
  it("returns the muted-foreground class for null/undefined", () => {
    expect(oeeColorClass(null)).toBe("text-muted-foreground")
    expect(oeeColorClass(undefined)).toBe("text-muted-foreground")
  })

  it("maps the default OEE thresholds to text-* status classes", () => {
    expect(oeeColorClass(0.85)).toBe("text-success")
    expect(oeeColorClass(0.6)).toBe("text-warning")
    expect(oeeColorClass(0.59)).toBe("text-destructive")
  })
})

describe("OEE_THRESHOLDS", () => {
  it("keeps the intentional per-factor boundaries", () => {
    expect(OEE_THRESHOLDS.oee).toEqual({ high: 0.85, mid: 0.6 })
    expect(OEE_THRESHOLDS.availability).toEqual({ high: 0.9, mid: 0.7 })
    expect(OEE_THRESHOLDS.performance).toEqual({ high: 0.9, mid: 0.7 })
    expect(OEE_THRESHOLDS.quality).toEqual({ high: 0.995, mid: 0.95 })
  })
})
