/**
 * One place that decides what each state looks like.
 *
 * Colour is a fast secondary cue, never the message itself: every consumer of these
 * classes also renders the state's text label, so the interface stays unambiguous for
 * colour-blind operators and on poorly calibrated projectors.
 */

import type { AlertLevel, GridState, Severity } from '../domain/enums'

export interface StateStyle {
  /** Text colour. */
  text: string
  /** Tinted background for chips and rows. */
  bg: string
  /** Border for outlined treatments. */
  border: string
  /** Solid fill for dots, bars and map markers. */
  dot: string
  /** Raw hex, for SVG and chart contexts that cannot take a class. */
  hex: string
}

export const STATE_STYLES: Record<GridState, StateStyle> = {
  normal: {
    text: 'text-[#10b981]',
    bg: 'bg-[#10b981]/12',
    border: 'border-[#10b981]/35',
    dot: 'bg-[#10b981]',
    hex: '#10b981',
  },
  watch: {
    text: 'text-[#fbbf24]',
    bg: 'bg-[#fbbf24]/12',
    border: 'border-[#fbbf24]/35',
    dot: 'bg-[#fbbf24]',
    hex: '#fbbf24',
  },
  warning: {
    text: 'text-[#f97316]',
    bg: 'bg-[#f97316]/12',
    border: 'border-[#f97316]/35',
    dot: 'bg-[#f97316]',
    hex: '#f97316',
  },
  critical: {
    text: 'text-[#ef4444]',
    bg: 'bg-[#ef4444]/14',
    border: 'border-[#ef4444]/40',
    dot: 'bg-[#ef4444]',
    hex: '#ef4444',
  },
  emergency: {
    text: 'text-[#ff3d71]',
    bg: 'bg-[#ff3d71]/16',
    border: 'border-[#ff3d71]/45',
    dot: 'bg-[#ff3d71]',
    hex: '#ff3d71',
  },
}

const ALERT_TO_STATE: Record<AlertLevel, GridState> = {
  information: 'normal',
  warning: 'watch',
  high: 'warning',
  critical: 'critical',
  emergency: 'emergency',
}

const SEVERITY_TO_STATE: Record<Severity, GridState> = {
  low: 'normal',
  medium: 'watch',
  high: 'warning',
  critical: 'critical',
}

export function styleForState(state: GridState): StateStyle {
  return STATE_STYLES[state] ?? STATE_STYLES.normal
}

export function styleForAlertLevel(level: AlertLevel): StateStyle {
  return STATE_STYLES[ALERT_TO_STATE[level] ?? 'normal']
}

export function styleForSeverity(severity: Severity): StateStyle {
  return STATE_STYLES[SEVERITY_TO_STATE[severity] ?? 'normal']
}

/** Health is the inverse of risk: high is good. */
export function styleForHealth(health: number): StateStyle {
  if (health >= 85) return STATE_STYLES.normal
  if (health >= 70) return STATE_STYLES.watch
  if (health >= 55) return STATE_STYLES.warning
  if (health >= 40) return STATE_STYLES.critical
  return STATE_STYLES.emergency
}

export function styleForRisk(risk: number): StateStyle {
  if (risk >= 90) return STATE_STYLES.emergency
  if (risk >= 75) return STATE_STYLES.critical
  if (risk >= 55) return STATE_STYLES.warning
  if (risk >= 35) return STATE_STYLES.watch
  return STATE_STYLES.normal
}

/** Chart series palette — distinguishable in both light projection and dark rooms. */
export const CHART_COLORS = {
  primary: '#24d07f',
  secondary: '#38bdf8',
  tertiary: '#a78bfa',
  quaternary: '#fbbf24',
  danger: '#ef4444',
  muted: '#64789a',
  grid: '#1e2d47',
  axis: '#64789a',
  band: '#38bdf833',
} as const
