import type { ConfidenceLevel, DataQualityLevel } from '../domain/enums'
import { CONF_HIGH, CONF_MEDIUM, DQ_HIGH, DQ_MEDIUM } from './constants'

// ── Semantic color tokens ─────────────────────────────────────────────────────

export const DS_COLORS = {
  resultType: {
    VERIFIED_PORTFOLIO:  { bg: 'rgba(21,38,66,0.10)',   fg: '#152642' },
    MAISON_WORK:         { bg: 'rgba(45,90,138,0.10)',  fg: '#2d5a8a' },
    FUTURE_AVAILABILITY: { bg: 'rgba(91,63,138,0.10)',  fg: '#5b3f8a' },
  },
  confidence: {
    VERY_HIGH: { bg: 'rgba(29,99,66,0.12)',   fg: '#1d6342' },
    HIGH:      { bg: 'rgba(29,99,66,0.09)',   fg: '#1d6342' },
    MEDIUM:    { bg: 'rgba(21,38,66,0.10)',   fg: '#152642' },
    LOW:       { bg: 'rgba(161,98,7,0.12)',   fg: '#a16207' },
    VERY_LOW:  { bg: 'rgba(185,28,28,0.12)',  fg: '#b91c1c' },
  },
  risk: {
    LOW:      { bg: 'rgba(29,99,66,0.10)',   fg: '#1d6342' },
    MEDIUM:   { bg: 'rgba(161,98,7,0.10)',   fg: '#a16207' },
    HIGH:     { bg: 'rgba(185,28,28,0.10)',  fg: '#b91c1c' },
    CRITICAL: { bg: 'rgba(127,0,0,0.12)',    fg: '#7f1d1d' },
  },
  availability: {
    AVAILABLE_NOW:  { bg: 'rgba(29,99,66,0.10)',   fg: '#1d6342' },
    AVAILABLE_SOON: { bg: 'rgba(161,98,7,0.10)',   fg: '#a16207' },
    FUTURE_SIGNAL:  { bg: 'rgba(91,63,138,0.10)',  fg: '#5b3f8a' },
    OCCUPIED:       { bg: 'rgba(100,116,139,0.10)', fg: '#475569' },
    UNKNOWN:        { bg: '#f4f3f0',                fg: '#9a9a9a' },
  },
  freshness: {
    FRESH:    { bg: 'rgba(29,99,66,0.10)',  fg: '#1d6342' },
    STALE:    { bg: 'rgba(161,98,7,0.10)', fg: '#a16207' },
    OUTDATED: { bg: 'rgba(185,28,28,0.10)', fg: '#b91c1c' },
  },
  dataQuality: {
    HIGH:       { bg: 'rgba(29,99,66,0.10)',  fg: '#1d6342' },
    MEDIUM:     { bg: 'rgba(161,98,7,0.10)',  fg: '#a16207' },
    LOW:        { bg: 'rgba(185,28,28,0.10)', fg: '#b91c1c' },
    INCOMPLETE: { bg: 'rgba(127,0,0,0.12)',   fg: '#7f1d1d' },
  },
  heat: {
    VERY_HOT: { bg: '#fef3c7', border: '#fcd34d', icon: '#a16207', text: '#92400e' },
    HOT:      { bg: '#fff7ed', border: '#fed7aa', icon: '#a16207', text: '#92400e' },
  },
  futureCard: {
    controlled: {
      accent:      '#5b3f8a',
      accentHover: '#4a3070',
      headerBg:    '#faf7ff',
      border:      '#e9d5ff',
      badgeBg:     '#ede9fe',
      badgeText:   '#5b21b6',
    },
    signal: {
      accent:      '#2d5a8a',
      accentHover: '#1e4068',
      headerBg:    '#f0f6ff',
      border:      '#bfdbfe',
      badgeBg:     '#dbeafe',
      badgeText:   '#1e40af',
      indicator:   '#2d5a8a',
    },
  },
} as const

// ── Text color tokens ─────────────────────────────────────────────────────────

export const DS_TEXT = {
  primary:   '#141414',
  secondary: '#5c5c5c',
  muted:     '#9a9a9a',
  disabled:  '#c4c4c4',
  inverted:  '#ffffff',
  success:   '#1d6342',
  warning:   '#a16207',
  error:     '#b91c1c',
  danger:    '#b91c1c',
  info:      '#2d5a8a',
  brand:     '#152642',
  signal:    '#5b3f8a',
  successDark: '#166534',
  warningDark: '#92400e',
  signalDark:  '#4c1d95',
  infoDark:    '#0c4a6e',
  brandDark:   '#0e1c30',
} as const

// ── Background tokens ─────────────────────────────────────────────────────────

export const DS_BG = {
  page:    '#f9f8f6',
  surface: '#ffffff',
  subtle:  '#f4f3f0',
  muted:   '#e8e7e4',
} as const

// ── Border tokens ─────────────────────────────────────────────────────────────

export const DS_BORDER = {
  default: '#e8e7e4',
  muted:   '#f2f1ee',
  strong:  '#c8c7c4',
} as const

// ── Surface tokens ────────────────────────────────────────────────────────────

export const DS_SURFACE = {
  success:  { bg: '#f0fdf4', border: '#bbf7d0' },
  warning:  { bg: '#fffbeb', border: '#fde68a' },
  error:    { bg: '#fef2f2', border: '#fecaca' },
  info:     { bg: '#f0f6ff', border: '#bfdbfe' },
  indigo:   { bg: '#eef2ff', border: '#e0e7ff' },
  purple:   { bg: '#faf7ff', border: '#e9d5ff' },
  orange:   { bg: '#fff7ed', border: '#fed7aa' },
  blue:     { bg: '#f0f6ff', border: '#bfdbfe' },
  neutral:  { bg: '#f9f8f6', border: '#e8e7e4' },
  slate:    { bg: '#f4f3f0', border: '#e8e7e4' },
} as const

// ── Match tier surface ────────────────────────────────────────────────────────

export const DS_MATCH_TIER = {
  strong:   DS_SURFACE.success,
  moderate: DS_SURFACE.warning,
  weak:     DS_SURFACE.error,
} as const

// ── Pre-market & market signal aliases ────────────────────────────────────────

export const DS_PRE_MARKET    = DS_COLORS.futureCard.controlled
export const DS_MARKET_SIGNAL = DS_COLORS.futureCard.signal

// ── Badge color presets ───────────────────────────────────────────────────────

export const BADGE_COLORS = {
  confidenceHigh:   '#1d6342',
  confidenceMedium: '#a16207',
  confidenceLow:    '#b91c1c',
  riskHigh:         '#b91c1c',
  riskMedium:       '#a16207',
  riskLow:          '#1d6342',
  preMarket:        '#5b3f8a',
  marketSignal:     '#2d5a8a',
  verified:         '#152642',
  info:             '#2d5a8a',
  muted:            '#9a9a9a',
  gold:             '#b8975a',
  silver:           '#7a8a9a',
  bronze:           '#7a6050',
} as const

// ── Shadow tokens ─────────────────────────────────────────────────────────────

export const DS_SHADOW = {
  card:   '0 1px 3px rgba(0,0,0,0.06)',
  panel:  '0 3px 6px rgba(0,0,0,0.07)',
  dialog: '0 16px 24px rgba(0,0,0,0.10)',
} as const

// ── Inquiry status metadata ───────────────────────────────────────────────────

export const INQUIRY_STATUS_META: Record<string, { label: string; fg: string; bg: string }> = {
  new:         { label: 'Neu',          fg: '#b91c1c', bg: '#fef2f2' },
  in_progress: { label: 'Aktiv',        fg: '#a16207', bg: '#fffbeb' },
  answered:    { label: 'Beantwortet',  fg: '#1d6342', bg: '#f0fdf4' },
  archived:    { label: 'Archiviert',   fg: '#9a9a9a', bg: '#f9f8f6' },
}

// ── Score → qualitative level helpers ────────────────────────────────────────

export function scoreToConfidenceLevel(score: number): ConfidenceLevel {
  if (score >= CONF_HIGH) return 'VERY_HIGH'
  if (score >= 0.75)      return 'HIGH'
  if (score >= CONF_MEDIUM) return 'MEDIUM'
  if (score >= 0.35)      return 'LOW'
  return 'VERY_LOW'
}

export function scoreToDataQualityLevel(score: number): DataQualityLevel {
  if (score >= DQ_HIGH)   return 'HIGH'
  if (score >= DQ_MEDIUM) return 'MEDIUM'
  if (score > 0)          return 'LOW'
  return 'INCOMPLETE'
}

// ── Result type meta ──────────────────────────────────────────────────────────

export const RESULT_TYPE_META: Record<string, { label: string; color: string; bg: string }> = {
  VERIFIED_PORTFOLIO:  { label: 'Verifiziertes Objekt', color: '#152642', bg: 'rgba(21,38,66,0.10)' },
  MAISON_WORK:         { label: 'Maison Work',          color: '#2d5a8a', bg: 'rgba(45,90,138,0.10)' },
  FUTURE_AVAILABILITY: { label: 'Zukunftssignal',       color: '#5b3f8a', bg: 'rgba(91,63,138,0.10)' },
}
