import type { KbxStatusDefinition } from '@kbx/contracts'

type StatusSet = readonly KbxStatusDefinition[]

/**
 * Product-wide UI status dictionary.
 * Domain/API raw value는 그대로 유지하고 Grid/Status renderer에서 Label/Semantic만 해석한다.
 */
export const kbxStatusCatalog = {
  orderAllocation: [
    { value:'정상', label:'정상', semantic:'ready' },
    { value:'할당완료', label:'할당완료', semantic:'completed' },
    { value:'부족', label:'재고 부족', semantic:'warning' },
    { value:'미확인', label:'미확인', semantic:'pending' },
  ],
  orderLifecycle: [
    { value:'NEW', label:'신규', semantic:'draft' },
    { value:'DRAFT', label:'작성', semantic:'draft' },
    { value:'CONFIRMED', label:'확정', semantic:'pending' },
    { value:'ALLOCATED', label:'할당', semantic:'processing' },
    { value:'PICKING', label:'피킹', semantic:'processing' },
    { value:'CHECKED', label:'검수', semantic:'processing' },
    { value:'SHIPPED', label:'출고완료', semantic:'completed' },
    { value:'HOLD', label:'보류', semantic:'hold' },
    { value:'ERROR', label:'오류', semantic:'error' },
  ],
  orderShipment: [
    { value:'NEW', label:'신규', semantic:'pending' },
    { value:'READY', label:'출고대기', semantic:'ready' },
    { value:'CONFIRMED', label:'출고지시', semantic:'processing' },
    { value:'HOLD', label:'보류', semantic:'hold' },
    { value:'SHIPPED', label:'출고완료', semantic:'completed' },
    { value:'ERROR', label:'오류', semantic:'error' },
  ],
  claim: [
    { value:'REQUESTED', label:'접수', semantic:'pending' },
    { value:'APPROVED', label:'승인', semantic:'processing' },
    { value:'IN_PROGRESS', label:'처리중', semantic:'processing' },
    { value:'HOLD', label:'보류', semantic:'hold' },
    { value:'COMPLETED', label:'완료', semantic:'completed' },
  ],
  workSeverity: [
    { value:'info', label:'정보', semantic:'info' },
    { value:'warning', label:'주의', semantic:'warning' },
    { value:'critical', label:'긴급', semantic:'error' },
  ],
  workItem: [
    { value:'open', label:'미처리', semantic:'pending' },
    { value:'claimed', label:'처리중', semantic:'processing' },
    { value:'resolved', label:'해결', semantic:'completed' },
    { value:'ignored', label:'무시', semantic:'disabled' },
  ],
  reconcile: [
    { value:'matched', label:'정상', semantic:'completed' },
    { value:'mismatch', label:'불일치', semantic:'warning' },
    { value:'pending', label:'확인중', semantic:'pending' },
    { value:'resolved', label:'해결', semantic:'completed' },
  ],
  wmsWork: [
    { value:'READY', label:'대기', semantic:'ready' },
    { value:'IN_PROGRESS', label:'진행', semantic:'processing' },
    { value:'BLOCKED', label:'예외', semantic:'error' },
    { value:'COMPLETED', label:'완료', semantic:'completed' },
    { value:'CANCELLED', label:'취소', semantic:'cancelled' },
  ],
  experiment: [
    { value:'draft', label:'초안', semantic:'draft' },
    { value:'running', label:'진행', semantic:'processing' },
    { value:'paused', label:'일시중지', semantic:'hold' },
    { value:'rolled-back', label:'롤백', semantic:'cancelled' },
    { value:'graduated', label:'승격', semantic:'completed' },
    { value:'completed', label:'완료', semantic:'completed' },
  ],
  externalData: [
    { value:'fresh', label:'최신', semantic:'ready' },
    { value:'stale', label:'Stale', semantic:'warning' },
    { value:'expired', label:'만료', semantic:'error' },
    { value:'unavailable', label:'사용불가', semantic:'error' },
  ],
} as const satisfies Record<string, StatusSet>


export type KbxOrderLifecycleValue = typeof kbxStatusCatalog.orderLifecycle[number]['value']
export type KbxOrderShipmentValue = typeof kbxStatusCatalog.orderShipment[number]['value']

export function kbxStatusOptions(definitions: StatusSet, values?: readonly string[]) {
  const allowed = values ? new Set(values) : null
  return definitions
    .filter(definition => !allowed || allowed.has(definition.value))
    .map(definition => ({ value: definition.value, label: definition.label }))
}


export function normalizeKbxStatusValue(definitions: StatusSet, value: unknown): string | null {
  const raw = typeof value === 'string' ? value.trim() : ''
  return raw && definitions.some(definition => definition.value === raw) ? raw : null
}
