// Brand colors from CLAUDE.md
export const BRAND_DARK_BLUE = '#053d57'
export const BRAND_RED = '#de0f1e'
export const BRAND_TEAL = '#557f8c'
export const BRAND_WHITE = '#fefefe'

// Extended palette derived from brand
export const BRAND_COLORS = [
  '#053d57', // Dark Blue
  '#de0f1e', // Red
  '#557f8c', // Teal
  '#1e7a8a', // Deep Teal
  '#c4453a', // Coral
  '#3a6f7f', // Mid Teal
  '#8b1a24', // Dark Red
  '#2d8659', // Sea Green
  '#7c9aa6', // Light Teal
  '#a83240', // Rose
] as const

// Tremor named colors for chart components
// These are the official Tremor palette names that render correctly
export const TREMOR_BRAND = 'blue' as const
export const TREMOR_ACCENT = 'indigo' as const
export const TREMOR_CHART_COLORS = [
  'blue',
  'emerald',
  'amber',
  'violet',
  'rose',
  'indigo',
  'sky',
  'fuchsia',
  'lime',
  'orange',
] as const

// Donut / status chart colors (mapped to Tremor names)
// Covers both global ProjectStatus and round-level ProjectRoundState values
export const TREMOR_STATUS_COLORS: Record<string, string> = {
  // Global project statuses
  SUBMITTED: 'sky',
  ELIGIBLE: 'blue',
  ASSIGNED: 'violet',
  SEMIFINALIST: 'amber',
  FINALIST: 'emerald',
  REJECTED: 'rose',
  DRAFT: 'gray',
  WITHDRAWN: 'slate',
  // Round-level states (ProjectRoundState)
  PENDING: 'sky',
  IN_PROGRESS: 'blue',
  PASSED: 'emerald',
  COMPLETED: 'indigo',
  // Evaluation review states
  FULLY_REVIEWED: 'emerald',
  PARTIALLY_REVIEWED: 'amber',
  NOT_REVIEWED: 'rose',
}

// Project status colors — mapped to actual ProjectStatus enum values
export const STATUS_COLORS: Record<string, string> = {
  SUBMITTED: '#557f8c',    // Teal
  ELIGIBLE: '#053d57',     // Dark Blue
  ASSIGNED: '#1e7a8a',     // Deep Teal
  SEMIFINALIST: '#c4453a', // Coral
  FINALIST: '#2d8659',     // Sea Green
  REJECTED: '#de0f1e',     // Red
  DRAFT: '#9ca3af',        // Gray
  WITHDRAWN: '#6b7280',    // Dark Gray
  // Evaluation review states
  FULLY_REVIEWED: '#2d8659',   // Sea Green
  PARTIALLY_REVIEWED: '#d97706', // Amber
  NOT_REVIEWED: '#de0f1e',       // Red
}

// Human-readable status labels
export const STATUS_LABELS: Record<string, string> = {
  SUBMITTED: 'Submitted',
  ELIGIBLE: 'In-Competition',
  ASSIGNED: 'Special Award',
  SEMIFINALIST: 'Semi-finalist',
  FINALIST: 'Finalist',
  REJECTED: 'Rejected',
  DRAFT: 'Draft',
  WITHDRAWN: 'Withdrawn',
  // Round-level states
  PENDING: 'Pending',
  IN_PROGRESS: 'In Progress',
  PASSED: 'Passed',
  COMPLETED: 'Completed',
  // Evaluation review states
  FULLY_REVIEWED: 'Fully Reviewed',
  PARTIALLY_REVIEWED: 'Partially Reviewed',
  NOT_REVIEWED: 'Not Reviewed',
}

/**
 * Score gradient: Red (low) → Teal (mid) → Dark Blue (high)
 * for scores on a 1-10 scale
 */
export function scoreGradient(score: number): string {
  const t = Math.max(0, Math.min(1, (score - 1) / 9))
  if (t < 0.5) {
    // Red → Teal (0 → 0.5)
    const p = t * 2
    return lerpColor(BRAND_RED, BRAND_TEAL, p)
  }
  // Teal → Dark Blue (0.5 → 1)
  const p = (t - 0.5) * 2
  return lerpColor(BRAND_TEAL, BRAND_DARK_BLUE, p)
}

function lerpColor(a: string, b: string, t: number): string {
  const ar = parseInt(a.slice(1, 3), 16)
  const ag = parseInt(a.slice(3, 5), 16)
  const ab = parseInt(a.slice(5, 7), 16)
  const br = parseInt(b.slice(1, 3), 16)
  const bg = parseInt(b.slice(3, 5), 16)
  const bb = parseInt(b.slice(5, 7), 16)
  const r = Math.round(ar + (br - ar) * t)
  const g = Math.round(ag + (bg - ag) * t)
  const bl = Math.round(ab + (bb - ab) * t)
  return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${bl.toString(16).padStart(2, '0')}`
}

/**
 * Helper: get color for a status value from STATUS_COLORS
 * Falls back to a neutral gray
 */
export function getStatusColor(status: string): string {
  return TREMOR_STATUS_COLORS[status] || 'gray'
}

/**
 * Helper: format a status value for display
 */
export function formatStatus(status: string): string {
  return STATUS_LABELS[status] || status.charAt(0) + status.slice(1).toLowerCase().replace(/_/g, ' ')
}
