export interface EntityBase {
  id: string
  createdAt: string
  updatedAt: string
  isDeleted: boolean
  deletedAt?: string
  deletedBy: string
  version: number
  isLock: boolean
  createdBy: string
  updatedBy: string
  remark: string
  extend: string
}

export interface Warehouse extends EntityBase {
  code: string
  name: string
  type: string
  enabled: boolean
  sortOrder: number
}

export interface WarehouseArea extends EntityBase {
  warehouseId: string
  code: string
  name: string
  type: string
  layoutMode: string
  state: string
  enabled: boolean
  sortOrder: number
}

export interface Storage extends EntityBase {
  areaId: string
  code: string
  name: string
  storageType: string
  locationKind: string
  columnNo: number
  levelNo: number
  depthNo: number
  siteId: string
  siteCode: string
  barcode: string
  capacity: number
  status: string
  usage: string
  priority: number
  zoneCode: string
  allowInbound: boolean
  allowOutbound: boolean
  enabled: boolean
}

export interface Container extends EntityBase {
  areaId?: string
  code: string
  name: string
  containerType: string
  status: string
  barcode: string
  length: number
  width: number
  height: number
  enabled: boolean
}

export interface MaterialType extends EntityBase {
  code: string
  name: string
  spec: string
  unit: string
  category: string
  barcodePrefix: string
  enabled: boolean
}

export interface Material extends EntityBase {
  code: string
  name: string
  typeCode: string
  barcode: string
  spec: string
  unit: string
  category: string
  lifecycleStatus: string
  unboundAt?: string
  enabled: boolean
}

export type ContainerLocationType = 'Storage' | 'Car'
export type ContainerLocationStatus = 'Active' | 'Locked' | 'Exception'

export interface ContainerLocation extends EntityBase {
  containerId: string
  locationType: ContainerLocationType
  locationId: string
  locationCode: string
  locationName: string
  status: ContainerLocationStatus
  enteredAt: string
}

export interface ContainerMaterial extends EntityBase {
  containerId: string
  materialId: string
  /** @deprecated 绑定语义固定为 1，UI 不展示数量 */
  quantity: number
  batchNo: string
  serialNo: string
  status: string
  boundAt: string
  loadedAt: string
  unloadedAt?: string
}

export interface InventoryMaterialRow {
  materialId: string
  materialCode: string
  materialName: string
  materialBarcode: string
  typeCode: string
  containerId: string
  containerCode: string
  containerName: string
  storageId?: string
  storageCode: string
  storageName: string
  areaId?: string
  areaCode: string
  areaName: string
  locationType: string
  boundAt: string
}

export interface StockEvent {
  id: string
  eventType: string
  materialId?: string
  materialCode: string
  materialName: string
  materialBarcode: string
  materialTypeCode: string
  containerId?: string
  containerCode: string
  containerName: string
  storageId?: string
  storageCode: string
  storageName: string
  areaCode: string
  fromStorageId?: string
  fromStorageCode: string
  fromStorageName: string
  toStorageId?: string
  toStorageCode: string
  toStorageName: string
  refType: string
  refId?: string
  refCode: string
  operator: string
  operatedAt: string
  reason: string
}

export interface ContainerLocationHistory {
  id: string
  relationId?: string
  eventType: string
  beforeJson: string
  afterJson: string
  containerId?: string
  operator: string
  operatedAt: string
  source: string
  reason: string
  remark: string
  extend: string
  fromLocationType: string
  fromLocationId: string
  toLocationType: string
  toLocationId: string
}

export interface ContainerMaterialHistory {
  id: string
  relationId?: string
  eventType: string
  beforeJson: string
  afterJson: string
  containerId?: string
  operator: string
  operatedAt: string
  source: string
  reason: string
  remark: string
  extend: string
  materialId?: string
  quantityDelta: number
}

export interface MasterDataPayload {
  id?: string
  version?: number
  code: string
  name: string
  type: string
  status: string
  enabled: boolean
  sortOrder: number
  isLock: boolean
  remark: string
  extend: string
}

export interface AreaPayload {
  id?: string
  version?: number
  warehouseId?: string
  code: string
  name: string
  type: string
  layoutMode: string
  state: string
  enabled: boolean
  sortOrder: number
  isLock: boolean
  remark: string
  extend: string
}

export interface StoragePayload {
  id?: string
  version?: number
  areaId: string
  code: string
  name: string
  storageType: string
  locationKind: string
  columnNo: number
  levelNo: number
  depthNo: number
  siteId: string
  siteCode: string
  barcode: string
  capacity: number
  status: string
  usage: string
  priority: number
  zoneCode: string
  allowInbound: boolean
  allowOutbound: boolean
  enabled: boolean
  isLock: boolean
  remark: string
  extend: string
}

export interface ContainerPayload {
  id?: string
  version?: number
  areaId?: string
  code: string
  name: string
  type: string
  status: string
  barcode: string
  length: number
  width: number
  height: number
  enabled: boolean
  isLock: boolean
  remark: string
  extend: string
}

export interface MaterialTypePayload {
  id?: string
  version?: number
  code: string
  name: string
  spec: string
  unit: string
  category: string
  barcodePrefix: string
  enabled: boolean
  isLock: boolean
  remark: string
  extend: string
}

export interface MaterialPayload {
  id?: string
  version?: number
  code: string
  name: string
  typeCode: string
  barcode: string
  spec: string
  unit: string
  category: string
  lifecycleStatus: string
  enabled: boolean
  isLock: boolean
  remark: string
  extend: string
}

export interface ContainerLocationPayload {
  id?: string
  version?: number
  containerId: string
  locationType: ContainerLocationType
  locationId: string
  status: ContainerLocationStatus
  enteredAt?: string
  source: string
  reason: string
  isLock: boolean
  remark: string
  extend: string
}

export interface BindMaterialPayload {
  id?: string
  version?: number
  containerId: string
  materialId: string
  source: string
  reason: string
  isLock: boolean
  remark: string
  extend: string
}

export interface GenerateBinsPayload {
  columnFrom: number
  columnTo: number
  levelFrom: number
  levelTo: number
  depthFrom: number
  depthTo: number
  codePattern?: string
}

export type WmsTransportTriggerType = 'MaterialCall' | 'FinishedGoodsOffline' | 'AutoTransfer'
export type WmsTransportTaskStatus = 'Pending' | 'Reserved' | 'Dispatched' | 'InTransit' | 'Completed' | 'Failed' | 'Cancelled'

export interface WmsTransportRule extends EntityBase {
  code: string
  name: string
  triggerType: WmsTransportTriggerType
  enabled: boolean
  priority: number
  sourceSelectorJson: string
  targetSelectorJson: string
  taskOptionsJson: string
}

export interface WmsTransportTask extends EntityBase {
  businessType: WmsTransportTriggerType
  ruleId?: string
  sourceStorageId: string
  targetStorageId: string
  containerId: string
  materialId?: string
  quantity?: number
  status: WmsTransportTaskStatus
  dispatchMissionId: string
  deliveryId: string
  dispatchStatus: string
  reason: string
  snapshotJson: string
  errorMessage: string
  taskPriority: number
}

export interface TransportRulePayload {
  id?: string
  version?: number
  code: string
  name: string
  triggerType: WmsTransportTriggerType
  enabled: boolean
  priority: number
  sourceSelectorJson: string
  targetSelectorJson: string
  taskOptionsJson: string
  isLock: boolean
  remark: string
  extend: string
}

export interface TransportRequestQuery {
  triggerType: WmsTransportTriggerType
  ruleId?: string
  requestSiteId?: string
  materialId?: string
  quantity?: number
  containerId?: string
  sourceStorageId?: string
  targetStorageId?: string
  batchNo?: string
  priority?: number
  reason?: string
}

export interface TransportCandidatePair {
  ruleId: string
  ruleCode: string
  sourceStorageId: string
  sourceStorageCode: string
  targetStorageId: string
  targetStorageCode: string
  containerId: string
  containerCode: string
  materialId?: string
  materialCode?: string
  quantity?: number
  score: number
  rejectReasons: string[]
}

export interface RulePreviewResult {
  ruleId: string
  ruleCode: string
  ruleName: string
  candidateCount: number
  rejectReasons: string[]
}

export interface TransportCandidatePreview {
  request: TransportRequestQuery
  candidates: TransportCandidatePair[]
  ruleResults: RulePreviewResult[]
}
