/**
 * 함정 상태 공통 카탈로그
 *
 * SSOT: backend `code_master` 그룹 PATROL_STATUS (V008 시드).
 * 향후 patrol_ship_master.status 컬럼 enum.
 *
 * 사용처: Dashboard PatrolStatusBadge, ShipAgent
 */

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

export type PatrolStatus =
  | 'AVAILABLE'
  | 'ON_PATROL'
  | 'IN_PURSUIT'
  | 'INSPECTING'
  | 'RETURNING'
  | 'STANDBY'
  | 'MAINTENANCE';

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

export const PATROL_STATUSES: Record<PatrolStatus, PatrolStatusMeta> = {
  IN_PURSUIT: {
    code: 'IN_PURSUIT',
    i18nKey: 'patrolStatus.IN_PURSUIT',
    fallback: { ko: '추적중', en: 'In Pursuit' },
    intent: 'critical',
    classes:
      'bg-red-100 text-red-800 border-red-300 dark:bg-red-500/20 dark:text-red-400 dark:border-red-500/30',
    order: 1,
  },
  INSPECTING: {
    code: 'INSPECTING',
    i18nKey: 'patrolStatus.INSPECTING',
    fallback: { ko: '검문중', en: 'Inspecting' },
    intent: 'high',
    classes:
      'bg-orange-100 text-orange-800 border-orange-300 dark:bg-orange-500/20 dark:text-orange-400 dark:border-orange-500/30',
    order: 2,
  },
  ON_PATROL: {
    code: 'ON_PATROL',
    i18nKey: 'patrolStatus.ON_PATROL',
    fallback: { ko: '초계중', en: 'On Patrol' },
    intent: 'info',
    classes:
      'bg-blue-100 text-blue-800 border-blue-300 dark:bg-blue-500/20 dark:text-blue-400 dark:border-blue-500/30',
    order: 3,
  },
  RETURNING: {
    code: 'RETURNING',
    i18nKey: 'patrolStatus.RETURNING',
    fallback: { ko: '귀항중', en: 'Returning' },
    intent: 'purple',
    classes:
      'bg-purple-100 text-purple-800 border-purple-300 dark:bg-purple-500/20 dark:text-purple-400 dark:border-purple-500/30',
    order: 4,
  },
  AVAILABLE: {
    code: 'AVAILABLE',
    i18nKey: 'patrolStatus.AVAILABLE',
    fallback: { ko: '가용', en: 'Available' },
    intent: 'success',
    classes:
      'bg-green-100 text-green-800 border-green-300 dark:bg-green-500/20 dark:text-green-400 dark:border-green-500/30',
    order: 5,
  },
  STANDBY: {
    code: 'STANDBY',
    i18nKey: 'patrolStatus.STANDBY',
    fallback: { ko: '대기', en: 'Standby' },
    intent: 'muted',
    classes:
      'bg-slate-100 text-slate-700 border-slate-300 dark:bg-slate-500/20 dark:text-slate-400 dark:border-slate-500/30',
    order: 6,
  },
  MAINTENANCE: {
    code: 'MAINTENANCE',
    i18nKey: 'patrolStatus.MAINTENANCE',
    fallback: { ko: '정비중', en: 'Maintenance' },
    intent: 'warning',
    classes:
      'bg-yellow-100 text-yellow-800 border-yellow-300 dark:bg-yellow-500/20 dark:text-yellow-400 dark:border-yellow-500/30',
    order: 7,
  },
};

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

/** 한글 라벨도 키로 받아주는 호환성 매핑 (mock 데이터에서 한글 사용 중) */
const LEGACY_KO_LABELS: Record<string, PatrolStatus> = {
  '추적 중': 'IN_PURSUIT',
  '추적중': 'IN_PURSUIT',
  '검문 중': 'INSPECTING',
  '검문중': 'INSPECTING',
  '초계 중': 'ON_PATROL',
  '초계중': 'ON_PATROL',
  '귀항 중': 'RETURNING',
  '귀항중': 'RETURNING',
  '가용': 'AVAILABLE',
  '대기': 'STANDBY',
  '정비 중': 'MAINTENANCE',
  '정비중': 'MAINTENANCE',
};

export function getPatrolStatusMeta(status: string): PatrolStatusMeta | undefined {
  // 영문 enum 우선
  if (PATROL_STATUSES[status as PatrolStatus]) {
    return PATROL_STATUSES[status as PatrolStatus];
  }
  // 한글 라벨 폴백 (mock 호환)
  const code = LEGACY_KO_LABELS[status];
  return code ? PATROL_STATUSES[code] : undefined;
}

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

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