import chalk, { Chalk } from 'chalk'
import { env } from './env.js'

export type Theme = {
  autoAccept: string
  bashBorder: string
  amber: string
  amberShimmer: string // Lighter version of amber color for shimmer effect
  amberGold_FOR_SYSTEM_SPINNER: string
  amberGoldShimmer_FOR_SYSTEM_SPINNER: string
  permission: string
  permissionShimmer: string // Lighter version of permission color for shimmer effect
  planMode: string
  ide: string
  promptBorder: string
  promptBorderShimmer: string // Lighter version of promptBorder color for shimmer effect
  text: string
  inverseText: string
  inactive: string
  inactiveShimmer: string // Lighter version of inactive color for shimmer effect
  subtle: string
  suggestion: string
  remember: string
  background: string
  // Semantic colors
  success: string
  error: string
  warning: string
  merged: string
  warningShimmer: string // Lighter version of warning color for shimmer effect
  // Diff colors
  diffAdded: string
  diffRemoved: string
  diffAddedDimmed: string
  diffRemovedDimmed: string
  // Word-level diff highlighting
  diffAddedWord: string
  diffRemovedWord: string
  // Agent colors
  red_FOR_SUBAGENTS_ONLY: string
  blue_FOR_SUBAGENTS_ONLY: string
  green_FOR_SUBAGENTS_ONLY: string
  yellow_FOR_SUBAGENTS_ONLY: string
  purple_FOR_SUBAGENTS_ONLY: string
  orange_FOR_SUBAGENTS_ONLY: string
  pink_FOR_SUBAGENTS_ONLY: string
  cyan_FOR_SUBAGENTS_ONLY: string
  // Grove colors
  professionalBlue: string
  // Chrome colors
  chromeYellow: string
  // TUI V2 colors
  sandBody: string
  sandBackground: string
  userMessageBackground: string
  userMessageBackgroundHover: string
  /** Message-actions selection. Warm shift toward `suggestion` gold; distinct from default AND userMessageBackground. */
  messageActionsBackground: string
  /** Text-selection highlight background (alt-screen mouse selection). Solid
   *  bg that REPLACES the cell's bg while preserving its fg — matches native
   *  terminal selection. Previously SGR-7 inverse (swapped fg/bg per cell),
   *  which fragmented badly over syntax highlighting. */
  selectionBg: string
  bashMessageBackgroundColor: string

  memoryBackgroundColor: string
  rate_limit_fill: string
  rate_limit_empty: string
  fastMode: string
  fastModeShimmer: string
  // Brief/assistant mode label colors
  briefLabelYou: string
  briefLabelAssistant: string
  // Rainbow colors for ultrathink keyword highlighting
  rainbow_red: string
  rainbow_orange: string
  rainbow_yellow: string
  rainbow_green: string
  rainbow_blue: string
  rainbow_indigo: string
  rainbow_violet: string
  rainbow_red_shimmer: string
  rainbow_orange_shimmer: string
  rainbow_yellow_shimmer: string
  rainbow_green_shimmer: string
  rainbow_blue_shimmer: string
  rainbow_indigo_shimmer: string
  rainbow_violet_shimmer: string
  // ── Desert Forge palette ──────────────────────────
  forgeCopper: string // Burnt copper — secondary warm metal
  forgeBronze: string // Oxidized bronze — tertiary
  forgeEmber: string // Deep ember red — accent
  forgeFireOrange: string // Forge-fire orange — accent
  forgeSandBeige: string // Sand beige — subtle/body text
  forgeCharcoal: string // Dark charcoal — background tint
  forgeNightSky: string // Deep blue-black — deepest bg
  forgeVerdigris: string // Patina green — success semantic
  forgeRust: string // Rust red — error semantic
  forgeMolten: string // Molten gold — warning semantic
  forgeSlag: string // Gray-brown — disabled/inactive
  forgeAsh: string // Light ash gray — ultra-subtle
  forgeSteel: string // Cool blue-gray — informational
  forgeSpark: string // Bright white-gold — highlights
  forgeSparkShimmer: string // Lighter spark for shimmer
  forgeCopperShimmer: string // Lighter copper for shimmer
  forgeEmberShimmer: string // Lighter ember for shimmer
}

export const THEME_NAMES = [
  'dark',
  'light',
  'light-daltonized',
  'dark-daltonized',
  'light-ansi',
  'dark-ansi',
] as const

/** A renderable theme. Always resolvable to a concrete color palette. */
export type ThemeName = (typeof THEME_NAMES)[number]

export const THEME_SETTINGS = ['auto', ...THEME_NAMES] as const

/**
 * A theme preference as stored in user config. `'auto'` follows the system
 * dark/light mode and is resolved to a ThemeName at runtime.
 */
export type ThemeSetting = (typeof THEME_SETTINGS)[number]

/**
 * Light theme using explicit RGB values to avoid inconsistencies
 * from users' custom terminal ANSI color definitions
 */
const lightTheme: Theme = {
  autoAccept: 'rgb(150,90,35)', // forgeCopper light
  bashBorder: 'rgb(150,90,35)', // forgeCopper light
  amber: 'rgb(245,166,35)', // SANDCODE amber
  amberShimmer: 'rgb(255,196,65)', // Lighter amber for shimmer effect
  amberGold_FOR_SYSTEM_SPINNER: 'rgb(180,140,30)', // Gold for system spinner
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'rgb(218,175,60)', // Lighter gold for shimmer
  permission: 'rgb(150,90,35)', // forgeCopper light
  permissionShimmer: 'rgb(210,175,70)', // Lighter gold for shimmer
  planMode: 'rgb(130,110,60)', // Muted olive
  ide: 'rgb(140,120,60)', // Warm muted gold
  promptBorder: 'rgb(140,135,120)', // Warm gray
  promptBorderShimmer: 'rgb(170,165,150)', // Warm lighter gray
  text: 'rgb(30,25,15)', // Warm black
  inverseText: 'rgb(255,255,255)', // White
  inactive: 'rgb(170,165,150)', // forgeSlag light
  inactiveShimmer: 'rgb(150,145,130)', // Warm lighter gray
  subtle: 'rgb(120,110,90)', // forgeSandBeige light
  suggestion: 'rgb(120,110,90)', // forgeSandBeige light
  remember: 'rgb(160,130,40)', // Dusty gold
  background: 'rgb(240,235,220)', // Warm off-white
  success: 'rgb(45,120,90)', // forgeVerdigris light
  error: 'rgb(155,50,40)', // forgeRust light
  warning: 'rgb(190,145,30)', // forgeMolten light
  merged: 'rgb(218,165,32)', // SANDCODE gold (matches autoAccept)
  warningShimmer: 'rgb(200,158,80)', // Lighter amber for shimmer effect
  diffAdded: 'rgb(105,219,124)', // Light green
  diffRemoved: 'rgb(255,168,180)', // Light red
  diffAddedDimmed: 'rgb(199,225,203)', // Very light green
  diffRemovedDimmed: 'rgb(253,210,216)', // Very light red
  diffAddedWord: 'rgb(47,157,68)', // Medium green
  diffRemovedWord: 'rgb(209,69,75)', // Medium red
  // Agent colors
  red_FOR_SUBAGENTS_ONLY: 'rgb(220,38,38)', // Red 600
  blue_FOR_SUBAGENTS_ONLY: 'rgb(37,99,235)', // Blue 600
  green_FOR_SUBAGENTS_ONLY: 'rgb(22,163,74)', // Green 600
  yellow_FOR_SUBAGENTS_ONLY: 'rgb(202,138,4)', // Yellow 600
  purple_FOR_SUBAGENTS_ONLY: 'rgb(147,51,234)', // Purple 600
  orange_FOR_SUBAGENTS_ONLY: 'rgb(234,88,12)', // Orange 600
  pink_FOR_SUBAGENTS_ONLY: 'rgb(219,39,119)', // Pink 600
  cyan_FOR_SUBAGENTS_ONLY: 'rgb(8,145,178)', // Cyan 600
  // Grove colors
  professionalBlue: 'rgb(80,95,115)', // forgeSteel light
  // Chrome colors
  userMessageBackgroundHover: 'rgb(252, 252, 252)', // ≥250 to quantize distinct from base at 256-color level
  messageActionsBackground: 'rgb(240, 235, 225)', // Warm gray
  selectionBg: 'rgb(235, 220, 170)', // Warm gold selection
  bashMessageBackgroundColor: 'rgb(245, 240, 230)',

  memoryBackgroundColor: 'rgb(240, 235, 220)',
  rate_limit_fill: 'rgb(180,145,40)', // Dark gold
  rate_limit_empty: 'rgb(100,85,50)', // Dark warm brown
  fastMode: 'rgb(218,150,20)', // SANDCODE amber fast
  fastModeShimmer: 'rgb(245,180,50)', // Lighter amber
  // Brief/assistant mode
  briefLabelYou: 'rgb(140,120,60)', // Warm muted gold
  briefLabelAssistant: 'rgb(245,166,35)', // SANDCODE amber
  rainbow_red: 'rgb(235,95,87)',
  rainbow_orange: 'rgb(245,139,87)',
  rainbow_yellow: 'rgb(250,195,95)',
  rainbow_green: 'rgb(145,200,130)',
  rainbow_blue: 'rgb(130,170,220)',
  rainbow_indigo: 'rgb(155,130,200)',
  rainbow_violet: 'rgb(200,130,180)',
  rainbow_red_shimmer: 'rgb(250,155,147)',
  rainbow_orange_shimmer: 'rgb(255,185,137)',
  rainbow_yellow_shimmer: 'rgb(255,225,155)',
  rainbow_green_shimmer: 'rgb(185,230,180)',
  rainbow_blue_shimmer: 'rgb(180,205,240)',
  rainbow_indigo_shimmer: 'rgb(195,180,230)',
  rainbow_violet_shimmer: 'rgb(230,180,210)',
  // Desert Forge — light
  forgeCopper: 'rgb(150,90,35)',
  forgeBronze: 'rgb(110,95,65)',
  forgeEmber: 'rgb(145,45,30)',
  forgeFireOrange: 'rgb(195,95,20)',
  forgeSandBeige: 'rgb(120,110,90)',
  forgeCharcoal: 'rgb(230,225,215)',
  forgeNightSky: 'rgb(245,242,235)',
  forgeVerdigris: 'rgb(45,120,90)',
  forgeRust: 'rgb(155,50,40)',
  forgeMolten: 'rgb(190,145,30)',
  forgeSlag: 'rgb(170,165,150)',
  forgeAsh: 'rgb(180,175,165)',
  forgeSteel: 'rgb(80,95,115)',
  forgeSpark: 'rgb(180,140,30)',
  forgeSparkShimmer: 'rgb(210,170,60)',
  forgeCopperShimmer: 'rgb(180,120,65)',
  forgeEmberShimmer: 'rgb(175,75,60)',
}

/**
 * Light ANSI theme using only the 16 standard ANSI colors
 * for terminals without true color support
 */
const lightAnsiTheme: Theme = {
  autoAccept: 'ansi:yellowBright',
  bashBorder: 'ansi:yellow',
  amber: 'ansi:yellowBright', // SANDCODE amber
  amberShimmer: 'ansi:yellowBright',
  amberGold_FOR_SYSTEM_SPINNER: 'ansi:yellow',
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'ansi:yellowBright',
  permission: 'ansi:yellow',
  permissionShimmer: 'ansi:yellowBright',
  planMode: 'ansi:yellow',
  ide: 'ansi:yellow',
  promptBorder: 'ansi:white',
  promptBorderShimmer: 'ansi:whiteBright',
  text: 'ansi:black',
  inverseText: 'ansi:white',
  inactive: 'ansi:blackBright',
  inactiveShimmer: 'ansi:white',
  subtle: 'ansi:blackBright',
  suggestion: 'ansi:yellow',
  remember: 'ansi:yellow',
  background: 'ansi:yellow',
  success: 'ansi:green',
  error: 'ansi:red',
  warning: 'ansi:yellow',
  merged: 'ansi:yellowBright',
  warningShimmer: 'ansi:yellowBright',
  diffAdded: 'ansi:green',
  diffRemoved: 'ansi:red',
  diffAddedDimmed: 'ansi:green',
  diffRemovedDimmed: 'ansi:red',
  diffAddedWord: 'ansi:greenBright',
  diffRemovedWord: 'ansi:redBright',
  // Agent colors
  red_FOR_SUBAGENTS_ONLY: 'ansi:red',
  blue_FOR_SUBAGENTS_ONLY: 'ansi:blue',
  green_FOR_SUBAGENTS_ONLY: 'ansi:green',
  yellow_FOR_SUBAGENTS_ONLY: 'ansi:yellow',
  purple_FOR_SUBAGENTS_ONLY: 'ansi:magenta',
  orange_FOR_SUBAGENTS_ONLY: 'ansi:redBright',
  pink_FOR_SUBAGENTS_ONLY: 'ansi:magentaBright',
  cyan_FOR_SUBAGENTS_ONLY: 'ansi:cyan',
  // Grove colors
  professionalBlue: 'ansi:blueBright',
  // Chrome colors
  chromeYellow: 'ansi:yellow', // Chrome yellow
  // TUI V2 colors
  sandBody: 'ansi:yellowBright', // Sandy gold (fennec fox)
  sandBackground: 'ansi:black',
  userMessageBackground: 'ansi:white',
  userMessageBackgroundHover: 'ansi:whiteBright',
  messageActionsBackground: 'ansi:white',
  selectionBg: 'ansi:yellow', // lighter named bg for light-ansi; dark fgs stay readable
  bashMessageBackgroundColor: 'ansi:whiteBright',

  memoryBackgroundColor: 'ansi:white',
  rate_limit_fill: 'ansi:yellow',
  rate_limit_empty: 'ansi:black',
  fastMode: 'ansi:red',
  fastModeShimmer: 'ansi:redBright',
  briefLabelYou: 'ansi:yellow',
  briefLabelAssistant: 'ansi:yellowBright', // SANDCODE amber
  rainbow_red: 'ansi:red',
  rainbow_orange: 'ansi:redBright',
  rainbow_yellow: 'ansi:yellow',
  rainbow_green: 'ansi:green',
  rainbow_blue: 'ansi:cyan',
  rainbow_indigo: 'ansi:blue',
  rainbow_violet: 'ansi:magenta',
  rainbow_red_shimmer: 'ansi:redBright',
  rainbow_orange_shimmer: 'ansi:yellow',
  rainbow_yellow_shimmer: 'ansi:yellowBright',
  rainbow_green_shimmer: 'ansi:greenBright',
  rainbow_blue_shimmer: 'ansi:cyanBright',
  rainbow_indigo_shimmer: 'ansi:blueBright',
  rainbow_violet_shimmer: 'ansi:magentaBright',
  // Desert Forge — light ANSI
  forgeCopper: 'ansi:yellow',
  forgeBronze: 'ansi:yellow',
  forgeEmber: 'ansi:red',
  forgeFireOrange: 'ansi:redBright',
  forgeSandBeige: 'ansi:white',
  forgeCharcoal: 'ansi:whiteBright',
  forgeNightSky: 'ansi:whiteBright',
  forgeVerdigris: 'ansi:green',
  forgeRust: 'ansi:red',
  forgeMolten: 'ansi:yellowBright',
  forgeSlag: 'ansi:blackBright',
  forgeAsh: 'ansi:white',
  forgeSteel: 'ansi:blue',
  forgeSpark: 'ansi:yellowBright',
  forgeSparkShimmer: 'ansi:yellowBright',
  forgeCopperShimmer: 'ansi:yellowBright',
  forgeEmberShimmer: 'ansi:redBright',
}

/**
 * Dark ANSI theme using only the 16 standard ANSI colors
 * for terminals without true color support
 */
const darkAnsiTheme: Theme = {
  autoAccept: 'ansi:yellowBright',
  bashBorder: 'ansi:yellowBright',
  amber: 'ansi:yellowBright', // SANDCODE amber
  amberShimmer: 'ansi:yellowBright',
  amberGold_FOR_SYSTEM_SPINNER: 'ansi:yellowBright',
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'ansi:yellowBright',
  permission: 'ansi:yellowBright',
  permissionShimmer: 'ansi:yellowBright',
  planMode: 'ansi:yellow',
  ide: 'ansi:yellow',
  promptBorder: 'ansi:white',
  promptBorderShimmer: 'ansi:whiteBright',
  text: 'ansi:whiteBright',
  inverseText: 'ansi:black',
  inactive: 'ansi:white',
  inactiveShimmer: 'ansi:whiteBright',
  subtle: 'ansi:white',
  suggestion: 'ansi:yellowBright',
  remember: 'ansi:yellowBright',
  background: 'ansi:yellow',
  success: 'ansi:greenBright',
  error: 'ansi:redBright',
  warning: 'ansi:yellowBright',
  merged: 'ansi:yellowBright',
  warningShimmer: 'ansi:yellowBright',
  diffAdded: 'ansi:green',
  diffRemoved: 'ansi:red',
  diffAddedDimmed: 'ansi:green',
  diffRemovedDimmed: 'ansi:red',
  diffAddedWord: 'ansi:greenBright',
  diffRemovedWord: 'ansi:redBright',
  // Agent colors
  red_FOR_SUBAGENTS_ONLY: 'ansi:redBright',
  blue_FOR_SUBAGENTS_ONLY: 'ansi:blueBright',
  green_FOR_SUBAGENTS_ONLY: 'ansi:greenBright',
  yellow_FOR_SUBAGENTS_ONLY: 'ansi:yellowBright',
  purple_FOR_SUBAGENTS_ONLY: 'ansi:magentaBright',
  orange_FOR_SUBAGENTS_ONLY: 'ansi:redBright',
  pink_FOR_SUBAGENTS_ONLY: 'ansi:magentaBright',
  cyan_FOR_SUBAGENTS_ONLY: 'ansi:cyanBright',
  // Grove colors
  professionalBlue: 'rgb(106,155,204)',
  // Chrome colors
  chromeYellow: 'ansi:yellowBright', // Chrome yellow
  // TUI V2 colors
  sandBody: 'ansi:yellowBright', // Sandy gold (fennec fox)
  sandBackground: 'ansi:black',
  userMessageBackground: 'ansi:blackBright',
  userMessageBackgroundHover: 'ansi:white',
  messageActionsBackground: 'ansi:blackBright',
  selectionBg: 'ansi:yellow', // darker named bg for dark-ansi; bright fgs stay readable
  bashMessageBackgroundColor: 'ansi:black',

  memoryBackgroundColor: 'ansi:blackBright',
  rate_limit_fill: 'ansi:yellow',
  rate_limit_empty: 'ansi:white',
  fastMode: 'ansi:redBright',
  fastModeShimmer: 'ansi:redBright',
  briefLabelYou: 'ansi:yellowBright',
  briefLabelAssistant: 'ansi:yellowBright', // SANDCODE amber
  rainbow_red: 'ansi:red',
  rainbow_orange: 'ansi:redBright',
  rainbow_yellow: 'ansi:yellow',
  rainbow_green: 'ansi:green',
  rainbow_blue: 'ansi:cyan',
  rainbow_indigo: 'ansi:blue',
  rainbow_violet: 'ansi:magenta',
  rainbow_red_shimmer: 'ansi:redBright',
  rainbow_orange_shimmer: 'ansi:yellow',
  rainbow_yellow_shimmer: 'ansi:yellowBright',
  rainbow_green_shimmer: 'ansi:greenBright',
  rainbow_blue_shimmer: 'ansi:cyanBright',
  rainbow_indigo_shimmer: 'ansi:blueBright',
  rainbow_violet_shimmer: 'ansi:magentaBright',
  // Desert Forge — dark ANSI
  forgeCopper: 'ansi:yellow',
  forgeBronze: 'ansi:yellow',
  forgeEmber: 'ansi:red',
  forgeFireOrange: 'ansi:redBright',
  forgeSandBeige: 'ansi:white',
  forgeCharcoal: 'ansi:black',
  forgeNightSky: 'ansi:black',
  forgeVerdigris: 'ansi:greenBright',
  forgeRust: 'ansi:redBright',
  forgeMolten: 'ansi:yellowBright',
  forgeSlag: 'ansi:blackBright',
  forgeAsh: 'ansi:white',
  forgeSteel: 'ansi:blueBright',
  forgeSpark: 'ansi:whiteBright',
  forgeSparkShimmer: 'ansi:whiteBright',
  forgeCopperShimmer: 'ansi:yellowBright',
  forgeEmberShimmer: 'ansi:redBright',
}

/**
 * Light daltonized theme (color-blind friendly) using explicit RGB values
 * to avoid inconsistencies from users' custom terminal ANSI color definitions
 */
const lightDaltonizedTheme: Theme = {
  autoAccept: 'rgb(150,90,35)', // forgeCopper light
  bashBorder: 'rgb(150,90,35)', // forgeCopper light
  amber: 'rgb(245,166,35)', // SANDCODE amber (daltonized)
  amberShimmer: 'rgb(255,196,65)', // Lighter amber for shimmer effect
  amberGold_FOR_SYSTEM_SPINNER: 'rgb(180,140,30)', // Gold spinner
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'rgb(218,175,60)', // Lighter gold shimmer
  permission: 'rgb(150,90,35)', // forgeCopper light
  permissionShimmer: 'rgb(180,120,65)', // forgeCopperShimmer light
  planMode: 'rgb(130,110,60)', // Muted olive
  ide: 'rgb(140,120,60)', // Warm gold
  promptBorder: 'rgb(170,165,150)', // forgeSlag light
  promptBorderShimmer: 'rgb(183,183,183)', // Lighter gray for shimmer
  text: 'rgb(0,0,0)', // Black
  inverseText: 'rgb(255,255,255)', // White
  inactive: 'rgb(170,165,150)', // forgeSlag light
  inactiveShimmer: 'rgb(180,175,165)', // Lighter gray for shimmer effect
  subtle: 'rgb(120,110,90)', // forgeSandBeige light
  suggestion: 'rgb(120,110,90)', // forgeSandBeige light
  remember: 'rgb(160,130,40)', // Dusty gold
  background: 'rgb(240,235,220)', // Warm off-white
  success: 'rgb(0,102,153)', // Blue instead of green for deuteranopia
  error: 'rgb(204,0,0)', // Pure red for better distinction
  warning: 'rgb(255,153,0)', // Orange adjusted for deuteranopia
  merged: 'rgb(218,165,32)', // SANDCODE gold (matches autoAccept)
  warningShimmer: 'rgb(255,183,50)', // Lighter orange for shimmer
  diffAdded: 'rgb(153,204,255)', // Light blue instead of green
  diffRemoved: 'rgb(255,204,204)', // Light red
  diffAddedDimmed: 'rgb(209,231,253)', // Very light blue
  diffRemovedDimmed: 'rgb(255,233,233)', // Very light red
  diffAddedWord: 'rgb(51,102,204)', // Medium blue (less intense than deep blue)
  diffRemovedWord: 'rgb(153,51,51)', // Softer red (less intense than deep red)
  // Agent colors (daltonism-friendly)
  red_FOR_SUBAGENTS_ONLY: 'rgb(204,0,0)', // Pure red
  blue_FOR_SUBAGENTS_ONLY: 'rgb(0,102,204)', // Pure blue
  green_FOR_SUBAGENTS_ONLY: 'rgb(0,204,0)', // Pure green
  yellow_FOR_SUBAGENTS_ONLY: 'rgb(255,204,0)', // Golden yellow
  purple_FOR_SUBAGENTS_ONLY: 'rgb(128,0,128)', // True purple
  orange_FOR_SUBAGENTS_ONLY: 'rgb(255,128,0)', // True orange
  pink_FOR_SUBAGENTS_ONLY: 'rgb(255,102,178)', // Adjusted pink
  cyan_FOR_SUBAGENTS_ONLY: 'rgb(0,178,178)', // Adjusted cyan
  // Grove colors
  professionalBlue: 'rgb(106,155,204)',
  // Chrome colors
  chromeYellow: 'rgb(251,188,4)', // Chrome yellow
  // TUI V2 colors
  sandBody: 'rgb(218,165,32)', // Sandy gold (fennec fox)
  sandBackground: 'rgb(0,0,0)',
  userMessageBackground: 'rgb(220, 220, 220)', // Slightly darker grey for optimal contrast
  userMessageBackgroundHover: 'rgb(232, 232, 232)', // ≥230 to quantize distinct from base at 256-color level
  messageActionsBackground: 'rgb(235, 230, 215)', // warm gray
  selectionBg: 'rgb(235, 220, 170)', // warm gold selection
  bashMessageBackgroundColor: 'rgb(245, 240, 230)',

  memoryBackgroundColor: 'rgb(240, 235, 220)',
  rate_limit_fill: 'rgb(180,145,40)', // Dark gold
  rate_limit_empty: 'rgb(100,85,50)', // Dark warm brown
  fastMode: 'rgb(218,150,20)', // SANDCODE amber fast
  fastModeShimmer: 'rgb(245,180,50)', // Lighter amber
  briefLabelYou: 'rgb(140,120,60)', // Warm muted gold
  briefLabelAssistant: 'rgb(245,166,35)', // SANDCODE amber (daltonized)
  rainbow_red: 'rgb(235,95,87)',
  rainbow_orange: 'rgb(245,139,87)',
  rainbow_yellow: 'rgb(250,195,95)',
  rainbow_green: 'rgb(145,200,130)',
  rainbow_blue: 'rgb(130,170,220)',
  rainbow_indigo: 'rgb(155,130,200)',
  rainbow_violet: 'rgb(200,130,180)',
  rainbow_red_shimmer: 'rgb(250,155,147)',
  rainbow_orange_shimmer: 'rgb(255,185,137)',
  rainbow_yellow_shimmer: 'rgb(255,225,155)',
  rainbow_green_shimmer: 'rgb(185,230,180)',
  rainbow_blue_shimmer: 'rgb(180,205,240)',
  rainbow_indigo_shimmer: 'rgb(195,180,230)',
  rainbow_violet_shimmer: 'rgb(230,180,210)',
  // Desert Forge — light daltonized
  forgeCopper: 'rgb(150,90,35)',
  forgeBronze: 'rgb(110,95,65)',
  forgeEmber: 'rgb(145,45,30)',
  forgeFireOrange: 'rgb(195,95,20)',
  forgeSandBeige: 'rgb(120,110,90)',
  forgeCharcoal: 'rgb(230,225,215)',
  forgeNightSky: 'rgb(245,242,235)',
  forgeVerdigris: 'rgb(0,130,160)', // Blue-tinted for deuteranopia
  forgeRust: 'rgb(155,50,40)',
  forgeMolten: 'rgb(190,145,30)',
  forgeSlag: 'rgb(170,165,150)',
  forgeAsh: 'rgb(180,175,165)',
  forgeSteel: 'rgb(80,95,115)',
  forgeSpark: 'rgb(180,140,30)',
  forgeSparkShimmer: 'rgb(210,170,60)',
  forgeCopperShimmer: 'rgb(180,120,65)',
  forgeEmberShimmer: 'rgb(175,75,60)',
}

/**
 * Dark theme using explicit RGB values to avoid inconsistencies
 * from users' custom terminal ANSI color definitions
 */
const darkTheme: Theme = {
  autoAccept: 'rgb(184,115,51)', // forgeCopper
  bashBorder: 'rgb(184,115,51)', // forgeCopper
  amber: 'rgb(245,166,35)', // SANDCODE amber
  amberShimmer: 'rgb(255,196,65)', // Lighter amber for shimmer effect
  amberGold_FOR_SYSTEM_SPINNER: 'rgb(218,165,32)', // Gold for system spinner
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'rgb(255,196,65)', // Lighter gold for shimmer
  permission: 'rgb(184,115,51)', // forgeCopper
  permissionShimmer: 'rgb(214,145,81)', // forgeCopperShimmer
  planMode: 'rgb(160,140,90)', // Muted khaki
  ide: 'rgb(180,160,100)', // Warm muted gold
  promptBorder: 'rgb(95,88,72)', // forgeSlag
  promptBorderShimmer: 'rgb(140,130,110)', // Warm lighter gray
  text: 'rgb(240,235,220)', // Warm white
  inverseText: 'rgb(0,0,0)', // Black
  inactive: 'rgb(165,160,145)', // forgeAsh
  inactiveShimmer: 'rgb(180,175,160)', // Warm lighter gray
  subtle: 'rgb(95,88,72)', // forgeSlag
  suggestion: 'rgb(194,178,150)', // forgeSandBeige
  remember: 'rgb(200,170,80)', // Dusty gold
  background: 'rgb(60,55,40)', // Dark warm brown
  success: 'rgb(67,145,115)', // forgeVerdigris
  error: 'rgb(183,65,55)', // forgeRust
  warning: 'rgb(245,195,60)', // forgeMolten
  merged: 'rgb(245,189,45)', // SANDCODE gold (matches autoAccept)
  warningShimmer: 'rgb(255,210,70)', // Lighter amber for shimmer
  diffAdded: 'rgb(50,70,35)', // Dark olive green
  diffRemoved: 'rgb(100,40,35)', // Dark warm red
  diffAddedDimmed: 'rgb(60,65,50)', // Very dark olive
  diffRemovedDimmed: 'rgb(85,60,55)', // Very dark red-brown
  diffAddedWord: 'rgb(130,170,60)', // Olive green
  diffRemovedWord: 'rgb(200,90,80)', // Warm red
  // Agent colors
  red_FOR_SUBAGENTS_ONLY: 'rgb(220,38,38)', // Red 600
  blue_FOR_SUBAGENTS_ONLY: 'rgb(37,99,235)', // Blue 600
  green_FOR_SUBAGENTS_ONLY: 'rgb(22,163,74)', // Green 600
  yellow_FOR_SUBAGENTS_ONLY: 'rgb(202,138,4)', // Yellow 600
  purple_FOR_SUBAGENTS_ONLY: 'rgb(147,51,234)', // Purple 600
  orange_FOR_SUBAGENTS_ONLY: 'rgb(234,88,12)', // Orange 600
  pink_FOR_SUBAGENTS_ONLY: 'rgb(219,39,119)', // Pink 600
  cyan_FOR_SUBAGENTS_ONLY: 'rgb(8,145,178)', // Cyan 600
  // Grove colors
  professionalBlue: 'rgb(130,145,165)', // forgeSteel
  // Chrome colors
  chromeYellow: 'rgb(251,188,4)', // Chrome yellow
  // TUI V2 colors
  sandBody: 'rgb(184,115,51)', // forgeCopper
  sandBackground: 'rgb(0,0,0)',
  userMessageBackground: 'rgb(55, 55, 55)', // Lighter grey for better visual contrast
  userMessageBackgroundHover: 'rgb(70, 70, 70)',
  messageActionsBackground: 'rgb(55, 50, 40)', // warm dark gray
  selectionBg: 'rgb(80, 70, 40)', // warm gold selection
  bashMessageBackgroundColor: 'rgb(55, 52, 42)',

  memoryBackgroundColor: 'rgb(50, 48, 40)',
  rate_limit_fill: 'rgb(218,175,62)', // Warm gold
  rate_limit_empty: 'rgb(80,75,55)', // Dark warm brown
  fastMode: 'rgb(245,170,30)', // SANDCODE amber fast
  fastModeShimmer: 'rgb(255,200,70)', // Lighter amber
  briefLabelYou: 'rgb(200,185,140)', // Warm tan
  briefLabelAssistant: 'rgb(245,166,35)', // SANDCODE amber
  rainbow_red: 'rgb(235,95,87)',
  rainbow_orange: 'rgb(245,139,87)',
  rainbow_yellow: 'rgb(250,195,95)',
  rainbow_green: 'rgb(145,200,130)',
  rainbow_blue: 'rgb(130,170,220)',
  rainbow_indigo: 'rgb(155,130,200)',
  rainbow_violet: 'rgb(200,130,180)',
  rainbow_red_shimmer: 'rgb(250,155,147)',
  rainbow_orange_shimmer: 'rgb(255,185,137)',
  rainbow_yellow_shimmer: 'rgb(255,225,155)',
  rainbow_green_shimmer: 'rgb(185,230,180)',
  rainbow_blue_shimmer: 'rgb(180,205,240)',
  rainbow_indigo_shimmer: 'rgb(195,180,230)',
  rainbow_violet_shimmer: 'rgb(230,180,210)',
  // Desert Forge — dark
  forgeCopper: 'rgb(184,115,51)',
  forgeBronze: 'rgb(140,120,83)',
  forgeEmber: 'rgb(175,60,40)',
  forgeFireOrange: 'rgb(230,120,30)',
  forgeSandBeige: 'rgb(194,178,150)',
  forgeCharcoal: 'rgb(38,35,30)',
  forgeNightSky: 'rgb(20,18,15)',
  forgeVerdigris: 'rgb(67,145,115)',
  forgeRust: 'rgb(183,65,55)',
  forgeMolten: 'rgb(245,195,60)',
  forgeSlag: 'rgb(95,88,72)',
  forgeAsh: 'rgb(165,160,145)',
  forgeSteel: 'rgb(130,145,165)',
  forgeSpark: 'rgb(255,235,180)',
  forgeSparkShimmer: 'rgb(255,245,210)',
  forgeCopperShimmer: 'rgb(214,145,81)',
  forgeEmberShimmer: 'rgb(205,90,70)',
}

/**
 * Dark daltonized theme (color-blind friendly) using explicit RGB values
 * to avoid inconsistencies from users' custom terminal ANSI color definitions
 */
const darkDaltonizedTheme: Theme = {
  autoAccept: 'rgb(184,115,51)', // forgeCopper
  bashBorder: 'rgb(184,115,51)', // forgeCopper
  amber: 'rgb(245,166,35)', // SANDCODE amber (daltonized)
  amberShimmer: 'rgb(255,196,65)', // Lighter amber for shimmer effect
  amberGold_FOR_SYSTEM_SPINNER: 'rgb(218,165,32)', // Gold spinner
  amberGoldShimmer_FOR_SYSTEM_SPINNER: 'rgb(255,196,65)', // Lighter gold shimmer
  permission: 'rgb(184,115,51)', // forgeCopper
  permissionShimmer: 'rgb(214,145,81)', // forgeCopperShimmer
  planMode: 'rgb(160,140,90)', // Muted khaki
  ide: 'rgb(180,160,100)', // Warm gold
  promptBorder: 'rgb(95,88,72)', // forgeSlag
  promptBorderShimmer: 'rgb(140,130,110)', // Warm lighter gray
  text: 'rgb(240,235,220)', // Warm white
  inverseText: 'rgb(0,0,0)', // Black
  inactive: 'rgb(165,160,145)', // forgeAsh
  inactiveShimmer: 'rgb(180,175,160)', // Warm lighter gray
  subtle: 'rgb(95,88,72)', // forgeSlag
  suggestion: 'rgb(194,178,150)', // forgeSandBeige
  remember: 'rgb(200,170,80)', // Dusty gold
  background: 'rgb(60,55,40)', // Dark warm brown
  success: 'rgb(51,153,255)', // Blue instead of green
  error: 'rgb(255,102,102)', // Bright red
  warning: 'rgb(255,204,0)', // Yellow-orange for deuteranopia
  merged: 'rgb(245,189,45)', // SANDCODE gold (matches autoAccept)
  warningShimmer: 'rgb(255,234,50)', // Lighter yellow-orange for shimmer
  diffAdded: 'rgb(0,68,102)', // Dark blue
  diffRemoved: 'rgb(102,0,0)', // Dark red
  diffAddedDimmed: 'rgb(62,81,91)', // Dimmed blue
  diffRemovedDimmed: 'rgb(62,44,44)', // Dimmed red
  diffAddedWord: 'rgb(0,119,179)', // Medium blue
  diffRemovedWord: 'rgb(179,0,0)', // Medium red
  // Agent colors (daltonism-friendly, dark mode)
  red_FOR_SUBAGENTS_ONLY: 'rgb(255,102,102)', // Bright red
  blue_FOR_SUBAGENTS_ONLY: 'rgb(102,178,255)', // Bright blue
  green_FOR_SUBAGENTS_ONLY: 'rgb(102,255,102)', // Bright green
  yellow_FOR_SUBAGENTS_ONLY: 'rgb(255,255,102)', // Bright yellow
  purple_FOR_SUBAGENTS_ONLY: 'rgb(178,102,255)', // Bright purple
  orange_FOR_SUBAGENTS_ONLY: 'rgb(255,178,102)', // Bright orange
  pink_FOR_SUBAGENTS_ONLY: 'rgb(255,153,204)', // Bright pink
  cyan_FOR_SUBAGENTS_ONLY: 'rgb(102,204,204)', // Bright cyan
  // Grove colors
  professionalBlue: 'rgb(106,155,204)',
  // Chrome colors
  chromeYellow: 'rgb(251,188,4)', // Chrome yellow
  // TUI V2 colors
  sandBody: 'rgb(218,165,32)', // Sandy gold (fennec fox)
  sandBackground: 'rgb(0,0,0)',
  userMessageBackground: 'rgb(55, 55, 55)', // Lighter grey for better visual contrast
  userMessageBackgroundHover: 'rgb(70, 70, 70)',
  messageActionsBackground: 'rgb(55, 50, 40)', // warm dark gray
  selectionBg: 'rgb(80, 70, 40)', // warm gold selection
  bashMessageBackgroundColor: 'rgb(55, 52, 42)',

  memoryBackgroundColor: 'rgb(50, 48, 40)',
  rate_limit_fill: 'rgb(218,175,62)', // Warm gold
  rate_limit_empty: 'rgb(80,75,55)', // Dark warm brown
  fastMode: 'rgb(245,170,30)', // SANDCODE amber fast
  fastModeShimmer: 'rgb(255,200,70)', // Lighter amber
  briefLabelYou: 'rgb(200,185,140)', // Warm tan
  briefLabelAssistant: 'rgb(245,166,35)', // SANDCODE amber (daltonized)
  rainbow_red: 'rgb(235,95,87)',
  rainbow_orange: 'rgb(245,139,87)',
  rainbow_yellow: 'rgb(250,195,95)',
  rainbow_green: 'rgb(145,200,130)',
  rainbow_blue: 'rgb(130,170,220)',
  rainbow_indigo: 'rgb(155,130,200)',
  rainbow_violet: 'rgb(200,130,180)',
  rainbow_red_shimmer: 'rgb(250,155,147)',
  rainbow_orange_shimmer: 'rgb(255,185,137)',
  rainbow_yellow_shimmer: 'rgb(255,225,155)',
  rainbow_green_shimmer: 'rgb(185,230,180)',
  rainbow_blue_shimmer: 'rgb(180,205,240)',
  rainbow_indigo_shimmer: 'rgb(195,180,230)',
  rainbow_violet_shimmer: 'rgb(230,180,210)',
  // Desert Forge — dark daltonized
  forgeCopper: 'rgb(184,115,51)',
  forgeBronze: 'rgb(140,120,83)',
  forgeEmber: 'rgb(175,60,40)',
  forgeFireOrange: 'rgb(230,120,30)',
  forgeSandBeige: 'rgb(194,178,150)',
  forgeCharcoal: 'rgb(38,35,30)',
  forgeNightSky: 'rgb(20,18,15)',
  forgeVerdigris: 'rgb(0,130,160)', // Blue-tinted for deuteranopia
  forgeRust: 'rgb(183,65,55)',
  forgeMolten: 'rgb(245,195,60)',
  forgeSlag: 'rgb(95,88,72)',
  forgeAsh: 'rgb(165,160,145)',
  forgeSteel: 'rgb(130,145,165)',
  forgeSpark: 'rgb(255,235,180)',
  forgeSparkShimmer: 'rgb(255,245,210)',
  forgeCopperShimmer: 'rgb(214,145,81)',
  forgeEmberShimmer: 'rgb(205,90,70)',
}

export function getTheme(themeName: ThemeName): Theme {
  switch (themeName) {
    case 'light':
      return lightTheme
    case 'light-ansi':
      return lightAnsiTheme
    case 'dark-ansi':
      return darkAnsiTheme
    case 'light-daltonized':
      return lightDaltonizedTheme
    case 'dark-daltonized':
      return darkDaltonizedTheme
    default:
      return darkTheme
  }
}

// Create a chalk instance with 256-color level for Apple Terminal
// Apple Terminal doesn't handle 24-bit color escape sequences well
const chalkForChart =
  env.terminal === 'Apple_Terminal'
    ? new Chalk({ level: 2 }) // 256 colors
    : chalk

/**
 * Converts a theme color to an ANSI escape sequence for use with asciichart.
 * Uses chalk to generate the escape codes, with 256-color mode for Apple Terminal.
 */
export function themeColorToAnsi(themeColor: string): string {
  const rgbMatch = themeColor.match(/rgb\(\s?(\d+),\s?(\d+),\s?(\d+)\s?\)/)
  if (rgbMatch) {
    const r = parseInt(rgbMatch[1]!, 10)
    const g = parseInt(rgbMatch[2]!, 10)
    const b = parseInt(rgbMatch[3]!, 10)
    // Use chalk.rgb which auto-converts to 256 colors when level is 2
    // Extract just the opening escape sequence by using a marker
    const colored = chalkForChart.rgb(r, g, b)('X')
    return colored.slice(0, colored.indexOf('X'))
  }
  // Fallback to magenta if parsing fails
  return '\x1b[35m'
}
