export type AssessmentLang = 'de' | 'en' | 'zh'

export interface AssessmentMainCategory {
  id: string
  code: string
  label: string
  label_de: string | null
  label_en: string | null
  sort_order: number
  is_active: boolean
  /** Hex color set in DB or derived from MAIN_CATEGORY_PALETTE by sort_order */
  color?: string | null
}

/** Category color palette, indexed by (sort_order - 1). Customer-specific palettes live under lib/customers/<id>/branding. */
export const CATEGORY_COLORS = [
  { bg: '#E6F4F9', border: '#0077A8', text: '#004A6B' }, // 1 — Methyl Blue
  { bg: '#E8F5EE', border: '#4CAF6A', text: '#1E7A3A' }, // 2 — Simply Green
  { bg: '#FBF0DC', border: '#D48830', text: '#8A5610' }, // 3 — Orange Ochre
  { bg: '#F2E8F5', border: '#7A3B8E', text: '#521D64' }, // 4 — Vivid Viola
  { bg: '#F9E4E6', border: '#BA2636', text: '#7D1420' }, // 5 — Pompeian Red
  { bg: '#E0F5FA', border: '#009EC6', text: '#006580' }, // 6 — Cyan Blue
] as const
export type CategoryColor = (typeof CATEGORY_COLORS)[number]

export interface AssessmentSubCategory {
  id: string
  main_category_id: string
  code: string
  label: string
  label_de: string | null
  label_en: string | null
  sort_order: number
  is_active: boolean
}

export interface AssessmentQuestion {
  id: string
  index_number: number
  main_category_id: string
  sub_category_id: string
  question_text: string
  question_text_de: string | null
  question_text_en: string | null
  answer_text_1: string
  answer_text_1_de: string | null
  answer_text_1_en: string | null
  answer_text_2: string
  answer_text_2_de: string | null
  answer_text_2_en: string | null
  answer_text_3: string
  answer_text_3_de: string | null
  answer_text_3_en: string | null
  answer_text_4: string
  answer_text_4_de: string | null
  answer_text_4_en: string | null
  is_active: boolean
  sort_order: number
  created_at: string
  updated_at: string
}

export interface QuestionWithCategories extends AssessmentQuestion {
  main_category: AssessmentMainCategory
  sub_category: AssessmentSubCategory
}

export interface Assessment {
  id: string
  title: string
  location: string | null
  assessment_number: string | null
  supplier_name: string | null
  conducted_at: string | null
  project_id: string
  status: 'draft' | 'in_progress' | 'completed'
  created_by: string | null
  created_at: string
  updated_at: string
  is_demo?: boolean | null
}

export interface AssessmentResponse {
  id: string
  assessment_id: string
  question_id: string
  is_relevant: boolean
  is_flagged: boolean
  selected_rating: number | null
  comment: string | null
  updated_by: string | null
  updated_at: string
}

// Local state for workspace (includes unsaved changes)
export interface ResponseDraft {
  dbId: string | null  // null = not yet saved
  is_relevant: boolean
  is_flagged: boolean
  selected_rating: number | null
  comment: string
}

export const STATUS_LABELS: Record<Assessment['status'], string> = {
  draft:       'Entwurf',
  in_progress: 'In Bearbeitung',
  completed:   'Abgeschlossen',
}

export const STATUS_COLORS: Record<Assessment['status'], string> = {
  draft:       '#6B7A8D',
  in_progress: 'var(--primary)',
  completed:   '#008A00',
}

export const RATING_COLORS: Record<number, { bg: string; border: string; text: string; selectedBg: string }> = {
  1: { bg: '#FEF2F2', border: '#EF4444', text: '#991B1B', selectedBg: '#FEE2E2' },
  2: { bg: '#FFFBEB', border: '#F59E0B', text: '#92400E', selectedBg: '#FEF3C7' },
  3: { bg: '#F0FDF4', border: '#22C55E', text: '#14532D', selectedBg: '#DCFCE7' },
  4: { bg: '#EFF6FF', border: '#3B82F6', text: '#1E3A8A', selectedBg: '#DBEAFE' },
}

export const RATING_LABELS: Record<number, string> = {
  1: 'Nicht erfüllt',
  2: 'Teilweise',
  3: 'Erfüllt',
  4: 'Best Practice',
}

// New color system — used by redesigned workspace/question-card
export const RATING_CONFIG = {
  1: { accent: '#DC2626', bg: '#FEE2E2', text: '#991B1B', label: 'Nicht erfüllt' },
  2: { accent: '#D97706', bg: '#FEF3C7', text: '#92400E', label: 'Teilweise'     },
  3: { accent: '#0D9488', bg: '#CCFBF1', text: '#134E4A', label: 'Erfüllt'       },
  4: { accent: '#16A34A', bg: '#DCFCE7', text: '#14532D', label: 'Best Practice' },
} as const
