/**
 * 이벤트 처리 상태 공통 카탈로그
 *
 * SSOT: backend `code_master` 그룹 EVENT_STATUS (V008 시드).
 * 향후 `GET /api/code-master?groupCode=EVENT_STATUS`로 fetch 예정.
 *
 * 사용처: EventList(처리상태 컬럼), 알림 처리, 단속 등록 액션
 */

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

export type EventStatus =
  | 'NEW'
  | 'ACK'
  | 'IN_PROGRESS'
  | 'RESOLVED'
  | 'FALSE_POSITIVE';

export interface EventStatusMeta {
  code: EventStatus;
  i18nKey: string;
  fallback: { ko: string; en: string };
  intent: BadgeIntent;
  classes: string; // bg + text 묶음 (legacy - use intent 권장)
  order: number;
}

export const EVENT_STATUSES: Record<EventStatus, EventStatusMeta> = {
  NEW: {
    code: 'NEW',
    i18nKey: 'eventStatus.NEW',
    fallback: { ko: '신규', en: 'New' },
    intent: 'critical',
    classes: 'bg-red-100 text-red-800 dark:bg-red-500/20 dark:text-red-400',
    order: 1,
  },
  ACK: {
    code: 'ACK',
    i18nKey: 'eventStatus.ACK',
    fallback: { ko: '확인', en: 'Acknowledged' },
    intent: 'high',
    classes: 'bg-orange-100 text-orange-800 dark:bg-orange-500/20 dark:text-orange-400',
    order: 2,
  },
  IN_PROGRESS: {
    code: 'IN_PROGRESS',
    i18nKey: 'eventStatus.IN_PROGRESS',
    fallback: { ko: '처리중', en: 'In Progress' },
    intent: 'info',
    classes: 'bg-blue-100 text-blue-800 dark:bg-blue-500/20 dark:text-blue-400',
    order: 3,
  },
  RESOLVED: {
    code: 'RESOLVED',
    i18nKey: 'eventStatus.RESOLVED',
    fallback: { ko: '완료', en: 'Resolved' },
    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: 'eventStatus.FALSE_POSITIVE',
    fallback: { ko: '오탐', en: 'False Positive' },
    intent: 'muted',
    classes: 'bg-muted text-muted-foreground',
    order: 5,
  },
};

export function getEventStatusIntent(status: string): BadgeIntent {
  return getEventStatusMeta(status)?.intent ?? 'muted';
}

export function getEventStatusMeta(status: string): EventStatusMeta | undefined {
  return EVENT_STATUSES[status as EventStatus];
}

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

export function getEventStatusClasses(status: string): string {
  return getEventStatusMeta(status)?.classes ?? 'bg-muted text-muted-foreground';
}
