import { EntityStatus } from "@/types/common"

export type BundleSaleStatus = "Draft" | "Posted" | "Cancelled"

export interface BundleSaleTemplateLine {
  bundleSaleTemplateLineId: number
  itemId: number
  warehouseId: number
  qty: number
  unitPrice: number
  includeInBundle: boolean
  sortOrder: number
}

export interface BundleSaleTemplateSummary {
  bundleSaleTemplateId: number
  templateCode: string
  templateName: string
  description: string | null
  status: EntityStatus
  lineCount: number
  createdAt: string
  updatedAt: string | null
}

export interface BundleSaleTemplate {
  bundleSaleTemplateId: number
  templateCode: string
  templateName: string
  description: string | null
  status: EntityStatus
  createdAt: string
  updatedAt: string | null
  lines: BundleSaleTemplateLine[]
}

export interface BundleSaleLine {
  bundleSaleLineId: number
  itemId: number
  description: string
  qty: number
  warehouseId: number
  unitPrice: number
  lineTotal: number
  includeInBundle: boolean
  isComponent: boolean
  parentLineId: number | null
}

export interface BundleSaleTotals {
  componentSubtotal: number
  bundlePrice: number
  marginAmount: number
  discountTotal: number
  taxTotal: number
  grandTotal: number
}

export interface BundleSaleSummary {
  bundleSaleId: number
  bundleNo: string
  bundleDate: string
  customerId: number
  customerSnapshotName: string
  warehouseId: number
  bundleName: string
  bundleCode: string
  status: BundleSaleStatus
  componentSubtotal: number
  bundlePrice: number
  grandTotal: number
  createdAt: string
}

export interface BundleSale extends BundleSaleSummary, BundleSaleTotals {
  cashierUserId: number
  bundleSaleTemplateId: number
  updatedAt: string | null
  lines: BundleSaleLine[]
}

export interface BundleSalePostingIssue {
  bundleSaleLineId: number
  itemId: number
  itemSku: string
  itemName: string
  warehouseId: number
  requestedQty: number
  availableQty: number
  shortQty: number
}

export interface BundleSalePostingCheck {
  bundleSaleId: number
  bundleNo: string
  status: BundleSaleStatus
  canPost: boolean
  issues: BundleSalePostingIssue[]
}

export interface CreateBundleSaleRequest {
  customerId: number
  warehouseId: number
  cashierUserId: number
  bundleSaleTemplateId: number
  bundleName: string
  bundlePrice: number
  allowPriceOverride: boolean
  lines: BundleSaleTemplateLine[]
}

export type UpdateBundleSaleRequest = CreateBundleSaleRequest
