export const BUSINESS_PRIMARY_COLOR_IDS = [
  'red',
  'yellow',
  'black',
  'cyan',
  'purple',
  'light-blue',
  'dark-blue',
  'brown',
  'navy',
  'dark-green',
  'orange',
] as const

export type BusinessPrimaryColorId = (typeof BUSINESS_PRIMARY_COLOR_IDS)[number]

export const DEFAULT_BUSINESS_PRIMARY_COLOR_ID: BusinessPrimaryColorId = 'dark-blue'

export type BusinessPrimaryColorTokens = {
  label: string
  primary: string
  primaryDark: string
  primaryLight: string
  primaryGlow: string
  primaryRgb: string
  primaryDarkRgb: string
  /** Second chart series color (red→purple, blue→cyan, …). */
  chartAccent: string
  chartAccentDark: string
  chartAccentRgb: string
  chartAccentDarkRgb: string
}

const PURPLE_ACCENT = {
  chartAccent: '#a855f7',
  chartAccentDark: '#9333ea',
  chartAccentRgb: '168 85 247',
  chartAccentDarkRgb: '147 51 234',
} as const

const CYAN_ACCENT = {
  chartAccent: '#06b6d4',
  chartAccentDark: '#0891b2',
  chartAccentRgb: '6 182 212',
  chartAccentDarkRgb: '8 145 178',
} as const

export const BUSINESS_PRIMARY_COLOR_PALETTE: Record<
  BusinessPrimaryColorId,
  BusinessPrimaryColorTokens
> = {
  red: {
    label: 'Red',
    primary: '#ef4444',
    primaryDark: '#dc2626',
    primaryLight: '#fee2e2',
    primaryGlow: '#ef4444',
    primaryRgb: '239 68 68',
    primaryDarkRgb: '220 38 38',
    ...PURPLE_ACCENT,
  },
  yellow: {
    label: 'Yellow',
    primary: '#eab308',
    primaryDark: '#ca8a04',
    primaryLight: '#fef9c3',
    primaryGlow: '#eab308',
    primaryRgb: '234 179 8',
    primaryDarkRgb: '202 138 4',
    ...PURPLE_ACCENT,
  },
  black: {
    label: 'Black',
    primary: '#1e293b',
    primaryDark: '#0f172a',
    primaryLight: '#e2e8f0',
    primaryGlow: '#334155',
    primaryRgb: '30 41 59',
    primaryDarkRgb: '15 23 42',
    ...CYAN_ACCENT,
  },
  cyan: {
    label: 'Cyan',
    primary: '#06b6d4',
    primaryDark: '#0891b2',
    primaryLight: '#cffafe',
    primaryGlow: '#06b6d4',
    primaryRgb: '6 182 212',
    primaryDarkRgb: '8 145 178',
    ...PURPLE_ACCENT,
  },
  purple: {
    label: 'Purple',
    primary: '#a855f7',
    primaryDark: '#9333ea',
    primaryLight: '#f3e8ff',
    primaryGlow: '#a855f7',
    primaryRgb: '168 85 247',
    primaryDarkRgb: '147 51 234',
    ...CYAN_ACCENT,
  },
  'light-blue': {
    label: 'Light Blue',
    primary: '#38bdf8',
    primaryDark: '#0ea5e9',
    primaryLight: '#e0f2fe',
    primaryGlow: '#38bdf8',
    primaryRgb: '56 189 248',
    primaryDarkRgb: '14 165 233',
    ...CYAN_ACCENT,
  },
  'dark-blue': {
    label: 'Blue',
    primary: '#3b82f6',
    primaryDark: '#2563eb',
    primaryLight: '#dbeafe',
    primaryGlow: '#3b82f6',
    primaryRgb: '59 130 246',
    primaryDarkRgb: '37 99 235',
    ...CYAN_ACCENT,
  },
  brown: {
    label: 'Brown',
    primary: '#8b5e3c',
    primaryDark: '#6f4a2f',
    primaryLight: '#faf6f0',
    primaryGlow: '#a67c52',
    primaryRgb: '139 94 60',
    primaryDarkRgb: '111 74 47',
    ...CYAN_ACCENT,
  },
  navy: {
    label: 'Dark Blue',
    primary: '#1e3a8a',
    primaryDark: '#172554',
    primaryLight: '#e0e7ff',
    primaryGlow: '#1e40af',
    primaryRgb: '30 58 138',
    primaryDarkRgb: '23 37 84',
    ...CYAN_ACCENT,
  },
  'dark-green': {
    label: 'Dark Green',
    primary: '#166534',
    primaryDark: '#14532d',
    primaryLight: '#dcfce7',
    primaryGlow: '#15803d',
    primaryRgb: '22 101 52',
    primaryDarkRgb: '20 83 45',
    ...CYAN_ACCENT,
  },
  orange: {
    label: 'Orange',
    primary: '#ea580c',
    primaryDark: '#c2410c',
    primaryLight: '#ffedd5',
    primaryGlow: '#f97316',
    primaryRgb: '234 88 12',
    primaryDarkRgb: '194 65 12',
    ...PURPLE_ACCENT,
  },
}

export function normalizeBusinessPrimaryColorId(value: unknown): BusinessPrimaryColorId {
  if (
    typeof value === 'string' &&
    BUSINESS_PRIMARY_COLOR_IDS.includes(value as BusinessPrimaryColorId)
  ) {
    return value as BusinessPrimaryColorId
  }

  return DEFAULT_BUSINESS_PRIMARY_COLOR_ID
}

export function getBusinessPrimaryColorTokens(
  colorId: BusinessPrimaryColorId | undefined | null,
): BusinessPrimaryColorTokens {
  return BUSINESS_PRIMARY_COLOR_PALETTE[
    normalizeBusinessPrimaryColorId(colorId ?? DEFAULT_BUSINESS_PRIMARY_COLOR_ID)
  ]
}
