/**
 * 디바이스/Agent 상태 카탈로그
 *
 * 함정 Agent, 외부 시스템 연결 등 디바이스의 운영 상태 표기.
 *
 * 사용처: ShipAgent, DataHub agent 상태, ExternalService 등
 */

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

export type DeviceStatus = 'ONLINE' | 'OFFLINE' | 'NOT_DEPLOYED' | 'SYNCING';

export interface DeviceStatusMeta {
  code: DeviceStatus;
  i18nKey: string;
  fallback: { ko: string; en: string };
  intent: BadgeIntent;
}

export const DEVICE_STATUSES: Record<DeviceStatus, DeviceStatusMeta> = {
  ONLINE: {
    code: 'ONLINE',
    i18nKey: 'deviceStatus.ONLINE',
    fallback: { ko: '온라인', en: 'Online' },
    intent: 'success',
  },
  SYNCING: {
    code: 'SYNCING',
    i18nKey: 'deviceStatus.SYNCING',
    fallback: { ko: '동기화중', en: 'Syncing' },
    intent: 'info',
  },
  OFFLINE: {
    code: 'OFFLINE',
    i18nKey: 'deviceStatus.OFFLINE',
    fallback: { ko: '오프라인', en: 'Offline' },
    intent: 'critical',
  },
  NOT_DEPLOYED: {
    code: 'NOT_DEPLOYED',
    i18nKey: 'deviceStatus.NOT_DEPLOYED',
    fallback: { ko: '미배포', en: 'Not Deployed' },
    intent: 'muted',
  },
};

/** 한글 라벨도 키로 받음 (mock 호환) */
const LEGACY_KO: Record<string, DeviceStatus> = {
  '온라인': 'ONLINE',
  '오프라인': 'OFFLINE',
  '미배포': 'NOT_DEPLOYED',
  '동기화중': 'SYNCING',
  '동기화 중': 'SYNCING',
};

export function getDeviceStatusMeta(status: string): DeviceStatusMeta | undefined {
  if (DEVICE_STATUSES[status as DeviceStatus]) return DEVICE_STATUSES[status as DeviceStatus];
  const code = LEGACY_KO[status];
  return code ? DEVICE_STATUSES[code] : undefined;
}

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

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