/**
 * 단속 조치 코드 공통 카탈로그
 *
 * SSOT: backend `code_master` 그룹 ENFORCEMENT_ACTION (V008 시드).
 * 백엔드 EnforcementRecord.action enum.
 *
 * 사용처: EnforcementHistory(조치 컬럼), 단속 등록 폼
 */

import type { BadgeIntent } from '@lib/theme/variants';

export type EnforcementAction =
  | 'CAPTURE'
  | 'INSPECT'
  | 'WARN'
  | 'DISPERSE'
  | 'TRACK'
  | 'EVIDENCE';

export interface EnforcementActionMeta {
  code: EnforcementAction;
  i18nKey: string;
  fallback: { ko: string; en: string };
  intent: BadgeIntent;
  classes: string;
  hex: string;
  order: number;
}

export const ENFORCEMENT_ACTIONS: Record<EnforcementAction, EnforcementActionMeta> = {
  CAPTURE: {
    code: 'CAPTURE',
    i18nKey: 'enforcementAction.CAPTURE',
    fallback: { ko: '나포', en: 'Capture' },
    intent: 'critical',
    classes: 'bg-red-100 text-red-800 dark:bg-red-500/20 dark:text-red-400',
    hex: '#ef4444',
    order: 1,
  },
  INSPECT: {
    code: 'INSPECT',
    i18nKey: 'enforcementAction.INSPECT',
    fallback: { ko: '검문', en: 'Inspect' },
    intent: 'warning',
    classes: 'bg-amber-100 text-amber-800 dark:bg-amber-500/20 dark:text-amber-400',
    hex: '#f59e0b',
    order: 2,
  },
  WARN: {
    code: 'WARN',
    i18nKey: 'enforcementAction.WARN',
    fallback: { ko: '경고', en: 'Warn' },
    intent: 'info',
    classes: 'bg-blue-100 text-blue-800 dark:bg-blue-500/20 dark:text-blue-400',
    hex: '#3b82f6',
    order: 3,
  },
  DISPERSE: {
    code: 'DISPERSE',
    i18nKey: 'enforcementAction.DISPERSE',
    fallback: { ko: '퇴거', en: 'Disperse' },
    intent: 'purple',
    classes: 'bg-violet-100 text-violet-800 dark:bg-violet-500/20 dark:text-violet-400',
    hex: '#8b5cf6',
    order: 4,
  },
  TRACK: {
    code: 'TRACK',
    i18nKey: 'enforcementAction.TRACK',
    fallback: { ko: '추적', en: 'Track' },
    intent: 'cyan',
    classes: 'bg-cyan-100 text-cyan-800 dark:bg-cyan-500/20 dark:text-cyan-400',
    hex: '#06b6d4',
    order: 5,
  },
  EVIDENCE: {
    code: 'EVIDENCE',
    i18nKey: 'enforcementAction.EVIDENCE',
    fallback: { ko: '증거수집', en: 'Evidence' },
    intent: 'muted',
    classes: 'bg-slate-100 text-slate-700 dark:bg-slate-500/20 dark:text-slate-300',
    hex: '#64748b',
    order: 6,
  },
};

export function getEnforcementActionClasses(action: string): string {
  return getEnforcementActionMeta(action)?.classes ?? 'bg-muted text-muted-foreground';
}

export function getEnforcementActionIntent(action: string): BadgeIntent {
  return getEnforcementActionMeta(action)?.intent ?? 'muted';
}

export function getEnforcementActionMeta(action: string): EnforcementActionMeta | undefined {
  return ENFORCEMENT_ACTIONS[action as EnforcementAction];
}

export function getEnforcementActionLabel(
  action: string,
  t: (key: string, opts?: { defaultValue?: string }) => string,
  lang: 'ko' | 'en' = 'ko',
): string {
  const meta = getEnforcementActionMeta(action);
  if (!meta) return action;
  return t(meta.i18nKey, { defaultValue: meta.fallback[lang] });
}
