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

export const operationsQueueScreen = defineKbxScreen({
  id: 'COMMON-OPS-001',
  version: '1.3.0',
  module: 'COMMON',
  type: 'queue', templateCode:'T06',
  title: '업무 예외 센터',
  description: '정상 건이 아니라 사람이 판단하거나 복구해야 할 업무만 모아 처리합니다.',
  permissions: ['common.operations.read'],
  helpKey: 'COMMON-OPS-001',
  telemetry: { enabled: true },
  commands: [
    { id: 'search', label: '조회', group: 'query', shortcut: 'F3' },
    { id: 'claim', label: '내가 처리', group: 'workflow', requiresSelection: true, minSelection: 1, permission: 'common.operations.claim' },
    { id: 'excel', label: '엑셀', group: 'output' },
  ],
})

export const operationsSearchFields: KbxSearchField[] = [
  { key: 'module', label: '모듈', type: 'select', primary: true, options: [
    { value: 'OMS', label: 'OMS' }, { value: 'WMS', label: 'WMS' }, { value: 'ERP', label: 'ERP' },
  ] },
  { key: 'severity', label: '심각도', type: 'select', primary: true, options: [
    { value: 'critical', label: '긴급' }, { value: 'warning', label: '주의' }, { value: 'info', label: '정보' },
  ] },
  { key: 'status', label: '상태', type: 'select', primary: true, options: [
    { value: 'open', label: '미처리' }, { value: 'claimed', label: '처리중' }, { value: 'resolved', label: '해결' },
  ] },
  { key: 'owner', label: '담당', type: 'select', primary: false, options: [
    { value: 'mine', label: '내 업무' }, { value: 'unassigned', label: '미지정' },
  ] },
  { key: 'keyword', label: '검색', type: 'text', primary: true, width: 'lg', placeholder: '주문번호, 작업번호, 오류명' },
]

export const operationsColumns: KbxGridColumn<KbxWorkItem>[] = [
  { field: 'severity', header: '중요도', type: 'status', width: 92, pinned: 'left', statusMap:{definitions:kbxStatusCatalog.workSeverity} },
  { field: 'sourceModule', header: '모듈', width: 76 },
  { field: 'title', header: '확인할 업무', width: 250, pinned: 'left' },
  { field: 'referenceNo', header: '대상번호', type: 'code', width: 150 },
  { field: 'detail', header: '원인/안내', width: 300 },
  { field: 'ownerName', header: '담당자', width: 100 },
  { field: 'occurredAt', header: '발생시각', type: 'datetime', width: 160 },
  { field: 'ageMinutes', header: '경과(분)', type: 'integer', width: 90 },
  { field: 'status', header: '처리상태', type: 'status', width: 110, statusMap:{definitions:kbxStatusCatalog.workItem} },
]
