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

export interface ClaimRow {
  id: string; claimNo: string; orderNo: string; channelName: string; type: string; reason: string; requestedQty: number; status: string; ownerName: string; requestedAt: string
}
export const claimScreen = defineKbxScreen({
  id:'OMS-CLM-001', version:'1.2.0', module:'OMS', type:'list', templateCode:'T01', title:'반품·클레임 관리', helpKey:'OMS-CLM-001', permissions:['oms.claim.read'], telemetry:{ enabled:true },
  description:'반품·교환·취소 요청을 조회하고 정상 건은 표준 상태전이로 처리합니다.',
  commands:[
    { id:'search', label:'조회', group:'query', shortcut:'F3' },
    { id:'approve', label:'승인', group:'workflow', variant:'primary', requiresSelection:true, minSelection:1, permission:'oms.claim.approve' },
    { id:'hold', label:'보류', group:'workflow', requiresSelection:true, minSelection:1, permission:'oms.claim.hold' },
    { id:'excel', label:'엑셀', group:'output' },
  ]
})
export const claimSearchFields: KbxSearchField[] = [
  { key:'period', label:'요청기간', type:'date-range', range:{ from:'from', to:'to' } },
  { key:'type', label:'유형', type:'select', options:[{value:'RETURN',label:'반품'},{value:'EXCHANGE',label:'교환'},{value:'CANCEL',label:'취소'}] },
  { key:'status', label:'상태', type:'select', options:[{value:'REQUESTED',label:'접수'},{value:'APPROVED',label:'승인'},{value:'IN_PROGRESS',label:'처리중'},{value:'COMPLETED',label:'완료'},{value:'HOLD',label:'보류'}] },
  { key:'keyword', label:'검색', type:'text', width:'lg', placeholder:'클레임번호/주문번호/고객' },
]
export const claimColumns: KbxGridColumn<ClaimRow>[] = [
  { field:'claimNo', header:'클레임번호', type:'code', width:150, pinned:'left' },
  { field:'orderNo', header:'주문번호', type:'code', width:150 },
  { field:'channelName', header:'채널', width:100 }, { field:'type', header:'유형', width:90 }, { field:'reason', header:'사유', width:220 },
  { field:'requestedQty', header:'수량', type:'quantity', width:90 }, { field:'status', header:'상태', type:'status', width:110, statusMap:{definitions:kbxStatusCatalog.claim} }, { field:'ownerName', header:'담당', width:100 }, { field:'requestedAt', header:'요청일시', type:'datetime', width:160 },
]
export const claimWorkflow: KbxWorkflowDefinition = {
  id:'oms.claim', version:'1.1.0',
  states:[
    {value:'REQUESTED',label:'접수',semantic:'pending'}, {value:'APPROVED',label:'승인',semantic:'processing'}, {value:'IN_PROGRESS',label:'처리중',semantic:'processing'}, {value:'HOLD',label:'보류',semantic:'hold',terminal:true}, {value:'COMPLETED',label:'완료',semantic:'completed',terminal:true}
  ],
  transitions:[
    {id:'approve',from:['REQUESTED'],to:'APPROVED',label:'승인',permission:'oms.claim.approve'},
    {id:'hold',from:['REQUESTED'],to:'HOLD',label:'보류',permission:'oms.claim.hold'},
    {id:'start',from:['APPROVED'],to:'IN_PROGRESS',label:'처리 시작',permission:'oms.claim.process'},
    {id:'complete',from:['IN_PROGRESS'],to:'COMPLETED',label:'처리 완료',permission:'oms.claim.process',confirm:true},
  ]
}
