import { defineKbxScreen, type KbxGridColumn, type KbxSearchField } from '@kbx/ui'

export interface InventoryItemRow {
  itemId: string; itemCode: string; itemName: string; specification: string
  totalQty: number; availableQty: number; allocatedQty: number; holdQty: number
}
export interface InventoryLocationRow {
  key: string; warehouseName: string; locationCode: string
  onHandQty: number; allocatedQty: number; availableQty: number; holdQty: number
}
export interface InventoryHistoryRow {
  entryId: string; occurredAt: string; businessType: string; referenceNo: string
  warehouseName: string; locationCode: string; inboundQty: number; outboundQty: number; balanceQty: number; actor: string
}

export const inventoryScreen = defineKbxScreen({
  id: 'ERP-INV-001', version: '1.1.0', module: 'ERP', type: 'master-detail', templateCode:'T05', title: '재고현황',
  description: '품목 현재고에서 창고·로케이션 근거와 재고이력까지 조회 Context를 유지해 탐색합니다.',
  helpKey: 'ERP-INV-001', permissions: ['erp.inventory.read'], telemetry: { enabled: true },
  commands: [{ id: 'search', label: '조회', group: 'query', shortcut: 'F3' }, { id: 'excel', label: '엑셀', group: 'output' }],
})

export const inventoryItemColumns: KbxGridColumn<InventoryItemRow>[] = [
  { field: 'itemCode', header: '품목코드', type: 'code', width: 120, pinned: 'left' },
  { field: 'itemName', header: '품목명', width: 190 },
  { field: 'specification', header: '규격', width: 140 },
  { field: 'totalQty', header: '현재고', type: 'quantity', width: 100, drilldown:true },
  { field: 'availableQty', header: '가용', type: 'quantity', width: 100, drilldown:true },
]
export const inventoryLocationColumns: KbxGridColumn<InventoryLocationRow>[] = [
  { field: 'warehouseName', header: '창고', width: 150 },
  { field: 'locationCode', header: '로케이션', type: 'code', width: 120 },
  { field: 'onHandQty', header: '현재고', type: 'quantity', width: 100 },
  { field: 'allocatedQty', header: '할당', type: 'quantity', width: 90 },
  { field: 'holdQty', header: '보류', type: 'quantity', width: 90 },
  { field: 'availableQty', header: '가용', type: 'quantity', width: 100 },
]
export const inventoryHistoryColumns: KbxGridColumn<InventoryHistoryRow>[] = [
  { field:'occurredAt', header:'일시', type:'datetime', width:165 },
  { field:'businessType', header:'업무', width:110 },
  { field:'referenceNo', header:'참조번호', type:'code', width:150 },
  { field:'warehouseName', header:'창고', width:130 },
  { field:'locationCode', header:'로케이션', type:'code', width:110 },
  { field:'inboundQty', header:'입고', type:'quantity', width:90 },
  { field:'outboundQty', header:'출고', type:'quantity', width:90 },
  { field:'balanceQty', header:'잔량', type:'quantity', width:100 },
  { field:'actor', header:'처리자', width:110 },
]
export const inventorySearchFields: KbxSearchField[] = [{ key: 'keyword', label: '품목', type: 'text', width: 'lg', placeholder: '품목코드/품목명' }]
