/**
 * Preset color themes — ported from craft-agents-oss theme system.
 *
 * Each theme is a 6-color semantic palette (background / foreground / accent /
 * info / success / destructive). The theme engine (theme-provider) injects
 * these into CSS variables; forge's surface ladder / ink / borders are derived
 * via color-mix from background+foreground, so the whole UI re-skins.
 *
 * Light/dark are orthogonal to the theme: `data-theme` selects which of the
 * two palettes applies. Themes marked dark-only below synthesize a light
 * variant from forge's light surface + the theme's accents.
 *
 * Sources: craft-agents-oss/apps/electron/resources/themes/*.json
 */

export interface ThemeColors {
  /** Page/canvas background → --bg-page */
  background: string
  /** Primary text/icons → --text-primary */
  foreground: string
  /** Brand/accent → --accent-indigo (--primary) */
  accent: string
  /** Success → --accent-green */
  success: string
  /** Info/warning → --accent-amber */
  info: string
  /** Destructive → --accent-coral */
  destructive: string
}

export interface PresetTheme {
  id: string
  name: string
  description: string
  /** Preview swatch color (CSS) for palette UI */
  swatch: string
  dark: ThemeColors
  light: ThemeColors
}

export const DEFAULT_THEME_ID = 'forge'

/** localStorage key for the active preset color theme id. */
export const ACCENT_STORAGE_KEY = 'forge-accent-theme'

export const PRESET_THEMES: PresetTheme[] = [
  {
    id: 'forge',
    name: 'Forge',
    description: 'Purple on deep charcoal — the default Forge identity',
    swatch: '#9770fc',
    // Values mirror globals.css :root (dark) & [data-theme=light] exactly,
    // so selecting this theme is a no-op visually.
    dark: {
      background: '#0B0B0E',
      foreground: '#FAFAF9',
      accent: 'oklch(0.65 0.20 293)',
      success: 'oklch(0.60 0.17 145)',
      info: 'oklch(0.70 0.16 70)',
      destructive: 'oklch(0.70 0.19 22)',
    },
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: 'oklch(0.58 0.22 293)',
      success: 'oklch(0.55 0.17 145)',
      info: 'oklch(0.75 0.16 70)',
      destructive: 'oklch(0.58 0.24 28)',
    },
  },
  {
    id: 'dracula',
    name: 'Dracula',
    description: 'Dark purple with bright accents',
    swatch: '#bd93f9',
    dark: {
      background: '#282a36',
      foreground: '#f8f8f2',
      accent: '#bd93f9',
      success: '#50fa7b',
      info: '#ffb86c',
      destructive: '#ff5555',
    },
    // dark-only source → light uses forge light surface + dracula accents
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: '#9d6cf5',
      success: '#3ddc6d',
      info: '#e8a23a',
      destructive: '#e04545',
    },
  },
  {
    id: 'tokyo-night',
    name: 'Tokyo Night',
    description: 'Downtown Tokyo at night',
    swatch: '#7aa2f7',
    dark: {
      background: '#1a1b26',
      foreground: '#c0caf5',
      accent: '#7aa2f7',
      success: '#9ece6a',
      info: '#e0af68',
      destructive: '#f7768e',
    },
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: '#5a82d8',
      success: '#6fa848',
      info: '#c89043',
      destructive: '#d6556e',
    },
  },
  {
    id: 'nord',
    name: 'Nord',
    description: 'Arctic, north-bluish, calm',
    swatch: '#88c0d0',
    dark: {
      background: '#2e3440',
      foreground: '#eceff4',
      accent: '#88c0d0',
      success: '#a3be8c',
      info: '#ebcb8b',
      destructive: '#bf616a',
    },
    light: {
      background: '#eceff4',
      foreground: '#2e3440',
      accent: '#5e81ac',
      success: '#a3be8c',
      info: '#ebcb8b',
      destructive: '#bf616a',
    },
  },
  {
    id: 'catppuccin',
    name: 'Catppuccin',
    description: 'Soothing pastel theme (Mocha / Latte)',
    swatch: '#cba6f7',
    dark: {
      background: '#1e1e2e',
      foreground: '#cdd6f4',
      accent: '#cba6f7',
      success: '#a6e3a1',
      info: '#fab387',
      destructive: '#f38ba8',
    },
    light: {
      background: '#eff1f5',
      foreground: '#4c4f69',
      accent: '#8839ef',
      success: '#40a02b',
      info: '#df8e1d',
      destructive: '#d20f39',
    },
  },
  {
    id: 'gruvbox',
    name: 'Gruvbox',
    description: 'Retro groove, earthy tones',
    swatch: '#fabd2f',
    dark: {
      background: '#282828',
      foreground: '#ebdbb2',
      accent: '#83a598',
      success: '#b8bb26',
      info: '#fabd2f',
      destructive: '#fb4934',
    },
    light: {
      background: '#fbf1c7',
      foreground: '#3c3836',
      accent: '#458588',
      success: '#98971a',
      info: '#d79921',
      destructive: '#cc241d',
    },
  },
  {
    id: 'solarized',
    name: 'Solarized',
    description: 'Precision colors, scientifically designed',
    swatch: '#268bd2',
    dark: {
      background: '#002b36',
      foreground: '#93a1a1',
      accent: '#268bd2',
      success: '#859900',
      info: '#b58900',
      destructive: '#dc322f',
    },
    light: {
      background: '#fdf6e3',
      foreground: '#586e75',
      accent: '#268bd2',
      success: '#859900',
      info: '#b58900',
      destructive: '#dc322f',
    },
  },
  {
    id: 'rose-pine',
    name: 'Rosé Pine',
    description: 'Soho vibes, warm and elegant',
    swatch: '#c4a7e7',
    dark: {
      background: '#191724',
      foreground: '#e0def4',
      accent: '#c4a7e7',
      success: '#31748f',
      info: '#f6c177',
      destructive: '#eb6f92',
    },
    light: {
      background: '#faf4ed',
      foreground: '#575279',
      accent: '#907aa9',
      success: '#286983',
      info: '#ea9d34',
      destructive: '#b4637a',
    },
  },
  {
    id: 'one-dark-pro',
    name: 'One Dark Pro',
    description: 'Atom-inspired, balanced contrast',
    swatch: '#61afef',
    dark: {
      background: '#282c34',
      foreground: '#abb2bf',
      accent: '#61afef',
      success: '#98c379',
      info: '#e5c07b',
      destructive: '#e06c75',
    },
    light: {
      background: '#fafafa',
      foreground: '#383a42',
      accent: '#4078f2',
      success: '#50a14f',
      info: '#c18401',
      destructive: '#e45649',
    },
  },
  {
    id: 'github',
    name: 'GitHub',
    description: 'Clean and professional',
    swatch: '#58a6ff',
    dark: {
      background: '#0d1117',
      foreground: '#e6edf3',
      accent: '#58a6ff',
      success: '#3fb950',
      info: '#d29922',
      destructive: '#f85149',
    },
    light: {
      background: '#ffffff',
      foreground: '#1f2328',
      accent: '#0969da',
      success: '#1a7f37',
      info: '#bf8700',
      destructive: '#cf222e',
    },
  },
  {
    id: 'night-owl',
    name: 'Night Owl',
    description: 'For night owls, reduced blue light',
    swatch: '#82aaff',
    dark: {
      background: '#011627',
      foreground: '#d6deeb',
      accent: '#82aaff',
      success: '#addb67',
      info: '#ecc48d',
      destructive: '#ef5350',
    },
    // dark-only source → light uses forge light surface + night-owl accents
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: '#5a7fcc',
      success: '#7aa847',
      info: '#c89858',
      destructive: '#c63835',
    },
  },
  {
    id: 'vitesse',
    name: 'Vitesse',
    description: 'Minimal and clean',
    swatch: '#4d9375',
    dark: {
      background: '#121212',
      foreground: '#dbd7ca', // source #dbd7caee — alpha dropped
      accent: '#4d9375',
      success: '#4d9375',
      info: '#caa75d',
      destructive: '#cb7676',
    },
    light: {
      background: '#ffffff',
      foreground: '#393a34',
      accent: '#1e754f',
      success: '#1e754f',
      info: '#b58900',
      destructive: '#ab5959',
    },
  },
  {
    id: 'craft-default',
    name: 'Craft Default',
    description: 'Craft Agents native default — clean purple-tinted neutral',
    swatch: '#a78bfa',
    dark: {
      background: '#1e1d21',
      foreground: '#f5f5f7',
      accent: '#a78bfa',
      success: '#22c55e',
      info: '#fbbf24',
      destructive: '#ef4444',
    },
    light: {
      background: '#faf9fb',
      foreground: '#1a1625',
      accent: '#8b5cf6',
      success: '#16a34a',
      info: '#d97706',
      destructive: '#dc2626',
    },
  },
  {
    id: 'pierre',
    name: 'Pierre',
    description: 'Clean, built for diffs',
    swatch: '#69b1ff',
    dark: {
      background: '#0d1117',
      foreground: '#c9d1d9',
      accent: '#69b1ff',
      success: '#5ecc71',
      info: '#ffa359',
      destructive: '#ff678d',
    },
    light: {
      background: '#ffffff',
      foreground: '#24292f',
      accent: '#009fff',
      success: '#0dbe4e',
      info: '#fe8c2c',
      destructive: '#fc2b73',
    },
  },
  {
    id: 'ghostty',
    name: 'Ghostty',
    description: 'Ghostty terminal default dark theme',
    swatch: '#83a5d6',
    dark: {
      background: '#292c33',
      foreground: '#ffffff',
      accent: '#83a5d6',
      success: '#b7bd73',
      info: '#e0af68',
      destructive: '#c55757',
    },
    // dark-only source → light uses forge light surface + ghostty accents
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: '#5a7da8',
      success: '#7a8047',
      info: '#c08a3a',
      destructive: '#a04040',
    },
  },
  {
    id: 'haze',
    name: 'Haze',
    description: 'Hazy purple — adapted from scenic original',
    swatch: '#a78bfa',
    // The source is a scenic (glass) theme with a translucent background over
    // an image; forge has no scenic mode, so a solid charcoal is used here.
    dark: {
      background: '#2a2a2e',
      foreground: '#f5f5f7',
      accent: '#a78bfa',
      success: '#22c55e',
      info: '#fbbf24',
      destructive: '#ef4444',
    },
    light: {
      background: '#FFFFFF',
      foreground: '#1A1A1E',
      accent: '#7c5fd6',
      success: '#1a8c3e',
      info: '#c89043',
      destructive: '#c63835',
    },
  },
]

/** Look up a preset theme by id; falls back to the Forge default. */
export function getPresetTheme(id: string | undefined | null): PresetTheme {
  return PRESET_THEMES.find((t) => t.id === id) ?? PRESET_THEMES[0]
}
