export type ScoreLevel = 'good' | 'warning' | 'concerning' | 'critical'

/**
 * Global score-to-level mapping for consistent health indicators.
 *
 * Thresholds (aligned with tenant-ui):
 * - 80+: good (healthy)
 * - 60-79: warning (needs attention)
 * - 40-59: concerning (degraded)
 * - <40: critical
 */
export function getScoreLevel(score: number): ScoreLevel {
  if (score >= 80) return 'good'
  if (score >= 60) return 'warning'
  if (score >= 40) return 'concerning'
  return 'critical'
}

/** Pre-built Tailwind class and CSS var map keyed by score level. */
export const scoreColors = {
  good: {
    bg: 'bg-score-good',
    text: 'text-score-good',
    hex: 'var(--color-score-good)',
  },
  warning: {
    bg: 'bg-score-warning',
    text: 'text-score-warning',
    hex: 'var(--color-score-warning)',
  },
  concerning: {
    bg: 'bg-score-concerning',
    text: 'text-score-concerning',
    hex: 'var(--color-score-concerning)',
  },
  critical: {
    bg: 'bg-score-critical',
    text: 'text-score-critical',
    hex: 'var(--color-score-critical)',
  },
} as const

// ---------------------------------------------------------------------------
// CSS custom-property helpers (for inline styles)
// ---------------------------------------------------------------------------

export function scoreToColor(score: number): string {
  const level = getScoreLevel(score)
  return `var(--color-score-${level})`
}

export function scoreToTextColor(score: number): string {
  return scoreToColor(score)
}

// ---------------------------------------------------------------------------
// Tailwind class helpers (single class strings)
// ---------------------------------------------------------------------------

/** Returns `bg-score-<level>` */
export function getScoreBgClass(score: number): string {
  return `bg-score-${getScoreLevel(score)}`
}

/** Returns `text-score-<level>` */
export function getScoreTextClass(score: number): string {
  return `text-score-${getScoreLevel(score)}`
}

/** Returns `border-score-<level>` */
export function getScoreBorderClass(score: number): string {
  return `border-score-${getScoreLevel(score)}`
}

// ---------------------------------------------------------------------------
// Lookup helpers (match tenant-ui API)
// ---------------------------------------------------------------------------

/** Returns the Tailwind `bg-score-*` class for a numeric score. */
export function getScoreBgColor(score: number): string {
  return scoreColors[getScoreLevel(score)].bg
}

/** Returns the Tailwind `text-score-*` class for a numeric score. */
export function getScoreTextColor(score: number): string {
  return scoreColors[getScoreLevel(score)].text
}

/** Returns the CSS variable reference (`var(--color-score-*)`) for a numeric score. */
export function getScoreHexColor(score: number): string {
  return scoreColors[getScoreLevel(score)].hex
}

/**
 * Returns composite Tailwind classes (text, bg with 10% opacity, border with 20% opacity)
 * suitable for badge / pill styling.
 */
export function getScoreColorClasses(score: number): {
  text: string
  bg: string
  border: string
} {
  const level = getScoreLevel(score)
  const colorMap = {
    good: { text: 'text-score-good', bg: 'bg-score-good/10', border: 'border-score-good/20' },
    warning: {
      text: 'text-score-warning',
      bg: 'bg-score-warning/10',
      border: 'border-score-warning/20',
    },
    concerning: {
      text: 'text-score-concerning',
      bg: 'bg-score-concerning/10',
      border: 'border-score-concerning/20',
    },
    critical: {
      text: 'text-score-critical',
      bg: 'bg-score-critical/10',
      border: 'border-score-critical/20',
    },
  }
  return colorMap[level]
}
