import { defineKbxScreen, type KbxGridColumn, type KbxReconcileItem, type KbxSearchField } from '@kbx/ui'
import { kbxStatusCatalog } from '../../../registry/statusCatalog'

export const reconcileScreen = defineKbxScreen({
  id: 'COMMON-REC-001',
  version: '1.3.0',
  module: 'COMMON',
  type: 'reconcile', templateCode:'T07',
  title: '업무 데이터 대사',
  description: '원천·대상 시스템의 기대값과 실제값을 비교하고 불일치 원인을 추적합니다.',
  permissions: ['common.reconcile.read'],
  helpKey: 'COMMON-REC-001',
  telemetry: { enabled: true },
  commands: [
    { id: 'search', label: '조회', group: 'query', shortcut: 'F3' },
    { id: 'createException', label: '예외 등록', group: 'workflow', requiresSelection: true, minSelection: 1, permission: 'common.operations.create' },
    { id: 'excel', label: '엑셀', group: 'output' },
  ],
})

export const reconcileSearchFields: KbxSearchField[] = [
  { key: 'reconcileType', label: '대사유형', type: 'select', primary: true, options: [
    { value: 'OMS_WMS_OUTBOUND_QTY', label: 'OMS↔WMS 출고수량' },
    { value: 'ORDER_PICK_QTY', label: '주문↔피킹수량' },
    { value: 'INVENTORY_SNAPSHOT', label: '재고 스냅샷' },
  ] },
  { key: 'status', label: '결과', type: 'select', primary: true, options: [
    { value: 'mismatch', label: '불일치' }, { value: 'pending', label: '확인중' }, { value: 'matched', label: '정상' }, { value: 'resolved', label: '해결' },
  ] },
  { key: 'keyword', label: '검색', type: 'text', primary: true, width: 'lg', placeholder: '주문번호, 품목, 참조번호' },
]

export const reconcileColumns: KbxGridColumn<KbxReconcileItem>[] = [
  { field: 'referenceNo', header: '참조번호', type: 'code', width: 160, pinned: 'left' },
  { field: 'sourceLabel', header: '기준', width: 130 },
  { field: 'expectedValue', header: '기대값', width: 130 },
  { field: 'targetLabel', header: '비교대상', width: 130 },
  { field: 'actualValue', header: '실제값', width: 130 },
  { field: 'differenceValue', header: '차이', width: 110 },
  { field: 'reasonText', header: '원인', width: 280 },
  { field: 'status', header: '상태', type: 'status', width: 110, statusMap:{definitions:kbxStatusCatalog.reconcile} },
  { field: 'occurredAt', header: '확인시각', type: 'datetime', width: 160 },
]
