/**
 * 어업 허가/판정 상태 카탈로그
 *
 * 사용처: GearDetection
 */

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

export type PermitStatus = 'VALID' | 'EXPIRED' | 'UNLICENSED' | 'PENDING';

interface PermitStatusMeta {
  code: PermitStatus;
  i18nKey: string;
  fallback: { ko: string; en: string };
  intent: BadgeIntent;
}

export const PERMIT_STATUSES: Record<PermitStatus, PermitStatusMeta> = {
  VALID: {
    code: 'VALID',
    i18nKey: 'permitStatus.VALID',
    fallback: { ko: '유효', en: 'Valid' },
    intent: 'success',
  },
  PENDING: {
    code: 'PENDING',
    i18nKey: 'permitStatus.PENDING',
    fallback: { ko: '확인 중', en: 'Pending' },
    intent: 'warning',
  },
  EXPIRED: {
    code: 'EXPIRED',
    i18nKey: 'permitStatus.EXPIRED',
    fallback: { ko: '만료', en: 'Expired' },
    intent: 'high',
  },
  UNLICENSED: {
    code: 'UNLICENSED',
    i18nKey: 'permitStatus.UNLICENSED',
    fallback: { ko: '무허가', en: 'Unlicensed' },
    intent: 'critical',
  },
};

const LEGACY_KO: Record<string, PermitStatus> = {
  '유효': 'VALID',
  '무허가': 'UNLICENSED',
  '만료': 'EXPIRED',
  '확인 중': 'PENDING',
  '확인중': 'PENDING',
};

export function getPermitStatusIntent(s: string): BadgeIntent {
  if (PERMIT_STATUSES[s as PermitStatus]) return PERMIT_STATUSES[s as PermitStatus].intent;
  const code = LEGACY_KO[s];
  return code ? PERMIT_STATUSES[code].intent : 'warning';
}
export function getPermitStatusLabel(
  s: string,
  t: (k: string, opts?: { defaultValue?: string }) => string,
  lang: 'ko' | 'en' = 'ko',
): string {
  let meta = PERMIT_STATUSES[s as PermitStatus];
  if (!meta) {
    const code = LEGACY_KO[s];
    if (code) meta = PERMIT_STATUSES[code];
  }
  if (!meta) return s;
  return t(meta.i18nKey, { defaultValue: meta.fallback[lang] });
}

// ─── 어구 판정 상태 ──────────────
export type GearJudgment = 'NORMAL' | 'SUSPECT_ILLEGAL' | 'CHECKING';

export const GEAR_JUDGMENTS: Record<GearJudgment, { i18nKey: string; fallback: { ko: string; en: string }; intent: BadgeIntent }> = {
  NORMAL: { i18nKey: 'gearJudgment.NORMAL', fallback: { ko: '정상', en: 'Normal' }, intent: 'success' },
  CHECKING: { i18nKey: 'gearJudgment.CHECKING', fallback: { ko: '확인 중', en: 'Checking' }, intent: 'warning' },
  SUSPECT_ILLEGAL: { i18nKey: 'gearJudgment.SUSPECT_ILLEGAL', fallback: { ko: '불법 의심', en: 'Suspect Illegal' }, intent: 'critical' },
};

const GEAR_LEGACY_KO: Record<string, GearJudgment> = {
  '정상': 'NORMAL',
  '확인 중': 'CHECKING',
  '확인중': 'CHECKING',
  '불법 의심': 'SUSPECT_ILLEGAL',
  '불법의심': 'SUSPECT_ILLEGAL',
};

export function getGearJudgmentIntent(s: string): BadgeIntent {
  if (GEAR_JUDGMENTS[s as GearJudgment]) return GEAR_JUDGMENTS[s as GearJudgment].intent;
  const code = GEAR_LEGACY_KO[s] ?? (s.includes('불법') ? 'SUSPECT_ILLEGAL' : undefined);
  return code ? GEAR_JUDGMENTS[code].intent : 'warning';
}
