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

export interface WmsWorkRow {
  taskId: string
  taskNo: string
  taskType: 'RECEIVING' | 'PUTAWAY' | 'PICKING' | 'CHECKING' | 'COUNTING'
  waveNo?: string
  ownerName?: string
  progress: number
  totalLines: number
  completedLines: number
  slaAt?: string
  status: string
  exceptionCount: number
}

export const wmsWorkScreen = defineKbxScreen({
  id: 'WMS-WORK-001', version: '1.4.0', module: 'WMS', type: 'queue', templateCode:'T06', title: '물류 작업',
  description: '입고·적치·피킹·검수·실사 작업과 예외를 작업자 관점에서 조회합니다.',
  helpKey: 'WMS-WORK-001', permissions: ['wms.work.read'], telemetry: { enabled: true },
  commands: [
    { id:'search', label:'조회', group:'query', shortcut:'F3' },
    { id:'start', label:'작업 시작', group:'workflow', variant:'primary', requiresSelection:true, minSelection:1, maxSelection:1, permission:'wms.work.execute' },
  ],
})

export const wmsWorkSearchFields: KbxSearchField[] = [
  { key:'taskType', label:'작업유형', type:'select', primary:true, options:[
    {value:'RECEIVING',label:'입고검수'},{value:'PUTAWAY',label:'적치'},{value:'PICKING',label:'피킹'},{value:'CHECKING',label:'검수'},{value:'COUNTING',label:'실사'},
  ] },
  { key:'status', label:'상태', type:'select', primary:true, options:[{value:'READY',label:'대기'},{value:'IN_PROGRESS',label:'진행'},{value:'BLOCKED',label:'예외'}] },
  { key:'owner', label:'작업자', type:'select', primary:true, options:[{value:'mine',label:'내 작업'},{value:'unassigned',label:'미배정'}] },
  { key:'keyword', label:'검색', type:'text', primary:true, width:'lg', placeholder:'작업번호 / Wave / 주문번호' },
]

export const wmsWorkColumns: KbxGridColumn<WmsWorkRow>[] = [
  { field:'taskNo', header:'작업번호', type:'code', width:150, pinned:'left' },
  { field:'taskType', header:'작업유형', width:100 },
  { field:'waveNo', header:'Wave', type:'code', width:130 },
  { field:'ownerName', header:'작업자', width:100 },
  { field:'progress', header:'진행률(%)', type:'percent', width:100 },
  { field:'completedLines', header:'완료', type:'integer', width:80 },
  { field:'totalLines', header:'전체', type:'integer', width:80 },
  { field:'slaAt', header:'SLA', type:'datetime', width:155 },
  { field:'status', header:'상태', type:'status', width:110, statusMap:{definitions:kbxStatusCatalog.wmsWork} },
  { field:'exceptionCount', header:'예외', type:'integer', width:80 },
]
