// Goods Receipt Note types (docs/11-BACKEND-PHASE1.md §4; FR-GRN-01..08).
//
// Deviation from the documented contract (recorded in Frontend/PROGRESS.md):
// the doc only specifies POST /grns, POST /grns/{id}/confirm and
// POST /grns/{id}/lines/{id}/release. A usable list/detail screen needs
// GET /grns and GET /grns/{id} — these are modelled here as a planned,
// not-yet-built backend extension (see Backend/PROGRESS.md when it lands).

export type GrnStatus = "Draft" | "Confirmed" | "Closed"
export type HoldStatus = "OnHold" | "Available" | "Rejected"

export interface BatchInput {
  batchNo: string
  expiryDate?: string | null
}

/** One received line on create (docs/11 §4.1). */
export interface CreateGrnLineInput {
  poLineId?: number | null
  itemId: number
  uomId: number
  binId?: number | null
  qty: number
  unitCost: number
  holdStatus: HoldStatus
  batch?: BatchInput | null
  /** FR-GRN-04 — required when the item's trackingMode is "Serial"; length must equal qty. */
  serialNumbers?: string[] | null
}

export interface CreateGrnRequest {
  poId?: number | null
  /** Required when poId is null (direct/emergency receipt, FR-GRN-02). */
  vendorId?: number | null
  warehouseId: number
  lines: CreateGrnLineInput[]
}

export interface GrnLine {
  grnLineId: number
  poLineId: number | null
  itemId: number
  uomId: number
  binId: number | null
  qty: number
  unitCost: number
  receivedValue: number
  holdStatus: HoldStatus
  batchId: number | null
}

export interface Grn {
  grnId: number
  docNo: string
  poId: number | null
  vendorId: number
  warehouseId: number
  status: GrnStatus
  createdBy: number
  createdAt: string
  lines: GrnLine[]
}

export interface GrnSummary {
  grnId: number
  docNo: string
  poId: number | null
  vendorId: number
  warehouseId: number
  status: GrnStatus
  createdAt: string
}

/** docs/11 §4.2 confirm response. */
export interface CreatedLayer {
  layerId: number
  itemId: number
  warehouseId: number
  batchId: number | null
  qtyReceived: number
  qtyRemaining: number
  unitCost: number
  receiptDate: string
}

export interface ConfirmGrnResponse {
  grnId: number
  status: GrnStatus
  postedAt: string
  createdLayers: CreatedLayer[]
  ledgerRefs: number[]
  poStatus: string | null
}

export type ReleaseAction = "Release" | "Reject"

export interface ReleaseGrnLineRequest {
  action: ReleaseAction
}

export interface ReleaseGrnLineResponse {
  grnLineId: number
  holdStatus: HoldStatus
}
