// Data-driven badge colors. A record stores a hex color; the UI renders it with
// an auto-picked readable text color, so any color stays legible. Pickers should
// offer PALETTE (curated, distinct, accessible) with custom hex as a fallback.

// Curated categorical palette - distinct + accessible. ~12 is the practical
// ceiling for at-a-glance distinguishability, which is plenty per category.
export const PALETTE = [
  '#f5365c', // red
  '#fb6340', // orange
  '#ff8800', // amber
  '#ffc107', // gold
  '#2dce89', // green
  '#04b962', // emerald
  '#11cdef', // cyan
  '#14abef', // blue
  '#0d6efd', // royal blue
  '#7934f3', // purple
  '#e83e8c', // pink
  '#6c757d', // gray
]

// Black or white text for a given background, by perceived brightness.
export function readableText(bg) {
  if (!bg || typeof bg !== 'string') return '#ffffff'
  let hex = bg.trim().replace('#', '')
  if (hex.length === 3) hex = hex.split('').map(c => c + c).join('')
  if (hex.length !== 6) return '#ffffff'
  const r = parseInt(hex.slice(0, 2), 16)
  const g = parseInt(hex.slice(2, 4), 16)
  const b = parseInt(hex.slice(4, 6), 16)
  const brightness = (0.299 * r + 0.587 * g + 0.114 * b) / 255
  return brightness > 0.6 ? '#1a1a1a' : '#ffffff'
}

// Style object for a badge/pill from a stored color (with a neutral fallback).
export function colorStyle(color, fallback = '#6c757d') {
  const backgroundColor = color || fallback
  return { backgroundColor, color: readableText(backgroundColor) }
}
