/**
 * 단속 결과 코드 공통 카탈로그
 *
 * SSOT: backend `code_master` 그룹 ENFORCEMENT_RESULT (V008 시드).
 * 백엔드 EnforcementRecord.result enum.
 *
 * 사용처: EnforcementHistory(결과 컬럼), 단속 통계
 */

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

export type EnforcementResult =
  | 'PUNISHED'
  | 'WARNED'
  | 'RELEASED'
  | 'REFERRED'
  | 'FALSE_POSITIVE';

export interface EnforcementResultMeta {
  code: EnforcementResult;
  i18nKey: string;
  fallback: { ko: string; en: string };
  intent: BadgeIntent;
  classes: string;
  order: number;
}

export const ENFORCEMENT_RESULTS: Record<EnforcementResult, EnforcementResultMeta> = {
  PUNISHED: {
    code: 'PUNISHED',
    i18nKey: 'enforcementResult.PUNISHED',
    fallback: { ko: '처벌', en: 'Punished' },
    intent: 'critical',
    classes: 'bg-red-100 text-red-800 dark:bg-red-500/20 dark:text-red-400',
    order: 1,
  },
  REFERRED: {
    code: 'REFERRED',
    i18nKey: 'enforcementResult.REFERRED',
    fallback: { ko: '수사의뢰', en: 'Referred' },
    intent: 'purple',
    classes: 'bg-purple-100 text-purple-800 dark:bg-purple-500/20 dark:text-purple-400',
    order: 2,
  },
  WARNED: {
    code: 'WARNED',
    i18nKey: 'enforcementResult.WARNED',
    fallback: { ko: '경고', en: 'Warned' },
    intent: 'warning',
    classes: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-500/20 dark:text-yellow-400',
    order: 3,
  },
  RELEASED: {
    code: 'RELEASED',
    i18nKey: 'enforcementResult.RELEASED',
    fallback: { ko: '훈방', en: 'Released' },
    intent: 'success',
    classes: 'bg-green-100 text-green-800 dark:bg-green-500/20 dark:text-green-400',
    order: 4,
  },
  FALSE_POSITIVE: {
    code: 'FALSE_POSITIVE',
    i18nKey: 'enforcementResult.FALSE_POSITIVE',
    fallback: { ko: '오탐(정상)', en: 'False Positive' },
    intent: 'muted',
    classes: 'bg-muted text-muted-foreground',
    order: 5,
  },
};

export function getEnforcementResultIntent(result: string): BadgeIntent {
  return getEnforcementResultMeta(result)?.intent ?? 'muted';
}

export function getEnforcementResultMeta(result: string): EnforcementResultMeta | undefined {
  return ENFORCEMENT_RESULTS[result as EnforcementResult];
}

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

export function getEnforcementResultClasses(result: string): string {
  return getEnforcementResultMeta(result)?.classes ?? 'bg-muted text-muted-foreground';
}
