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

export interface OrderSearchRow {
  id: string
  orderNo: string
  channelName: string
  orderedAt: string
  customerName: string
  itemSummary: string
  totalQty: number
  amount: number
  allocationStatus: string
  shipmentStatus: string
  exceptionCount: number
}

export const orderListScreen = defineKbxScreen({
  id: 'OMS-ORD-001',
  version: '1.5.0',
  module: 'OMS',
  type: 'list', templateCode:'T01',
  title: '주문관리',
  description: '주문을 조회하고 예외 및 출고대상을 일괄 처리합니다.',
  permissions: ['oms.order.read'],
  helpKey: 'OMS-ORD-001',
  telemetry: { enabled: true },
  commands: [
    { id: 'search', label: '조회', group: 'query', shortcut: 'F3' },
    { id: 'new', label: '신규', group: 'edit', permission: 'oms.order.create' },
    {
      id: 'ship', label: '출고지시', group: 'workflow', variant: 'primary',
      requiresSelection: true, minSelection: 1, permission: 'oms.order.ship'
    },
    { id: 'excel', label: '엑셀', group: 'output', menu: 'excel' },
  ],
})

export const orderColumns: KbxGridColumn<OrderSearchRow>[] = [
  { field: 'orderNo', header: '주문번호', type: 'link', width: 150, pinned: 'left' },
  { field: 'channelName', header: '판매채널', width: 110 },
  { field: 'orderedAt', header: '주문일시', type: 'datetime', width: 160 },
  { field: 'customerName', header: '주문자', width: 120 },
  { field: 'itemSummary', header: '대표상품', width: 240 },
  { field: 'totalQty', header: '수량', type: 'quantity', width: 90 },
  { field: 'amount', header: '금액', type: 'money', width: 130 },
  { field: 'allocationStatus', header: '재고', type: 'status', width: 110, statusMap:{definitions:kbxStatusCatalog.orderAllocation} },
  { field: 'shipmentStatus', header: '출고상태', type: 'status', width: 120, statusMap:{definitions:kbxStatusCatalog.orderShipment} },
  { field: 'exceptionCount', header: '오류', type: 'integer', width: 80 },
]

export const orderSearchFields: KbxSearchField[] = [
  { key: 'period', label: '주문기간', type: 'date-range', range: { from: 'from', to: 'to' } },
  { key: 'status', label: '상태', type: 'select', options: kbxStatusOptions(kbxStatusCatalog.orderShipment) },
  { key: 'keyword', label: '통합검색', type: 'text', width: 'lg', placeholder: '주문번호/주문자/상품' },
]
