// Stock Management types (docs/11-BACKEND-PHASE1.md §5; FR-STK-01..14, FR-WH-*).
//
// Deviation from the documented contract (recorded in Frontend/PROGRESS.md,
// same pattern as the GRN deviation): §5.4/5.5/5.6 document only the
// transactional POSTs/PUT for transfers/adjustments/counts, not a list/detail
// GET. A usable list+detail UI needs to reload these documents, so this file
// (and the matching lib/api/stock-*.ts modules) assume GET list/detail
// endpoints will exist once the real Stock Core backend is built.

export type Currency = "LKR"

// --- 5.1 On-hand ----------------------------------------------------------------

export interface OnHand {
  itemId: number
  warehouseId: number
  onHand: number
  available: number
  onHold: number
  inTransit: number
  reserved: number
  asOf: string
}

// --- 5.2 Ledger -------------------------------------------------------------------

export type LedgerDirection = "In" | "Out"

export interface LedgerEntry {
  ledgerId: number
  itemId: number
  warehouseId: number
  binId: number | null
  batchId: number | null
  serialId: number | null
  direction: LedgerDirection
  qtyBase: number
  unitCost: number
  value: number
  runningBalance: number
  sourceDocType: string
  sourceDocId: number
  userId: number
  createdAt: string
}

// --- 5.3 Valuation ------------------------------------------------------------------

export interface ValuationLayer {
  layerId: number
  qtyRemaining: number
  unitCost: number
  value: number
  receiptDate: string
}

export interface Valuation {
  itemId: number
  warehouseId: number
  layers: ValuationLayer[]
  totalQty: number
  totalValue: number
  currency: Currency
  costingMethod: "FIFO"
}

// --- 5.4 Transfers ------------------------------------------------------------------

export type TransferStatus = "Draft" | "InTransit" | "Received" | "Closed"

export interface TransferLine {
  transferLineId: number
  itemId: number
  srcBinId: number | null
  destBinId: number | null
  batchId: number | null
  qty: number
}

export interface StockTransfer {
  transferId: number
  docNo: string
  srcWarehouseId: number
  destWarehouseId: number
  status: TransferStatus
  createdBy: number
  createdAt: string
  lines: TransferLine[]
}

export interface StockTransferSummary {
  transferId: number
  docNo: string
  srcWarehouseId: number
  destWarehouseId: number
  status: TransferStatus
  createdAt: string
}

export interface CreateTransferLineInput {
  itemId: number
  srcBinId?: number | null
  destBinId?: number | null
  batchId?: number | null
  qty: number
}

export interface CreateTransferRequest {
  srcWarehouseId: number
  destWarehouseId: number
  lines: CreateTransferLineInput[]
}

export interface ConsumedLayer {
  layerId: number
  qtyConsumed: number
  unitCost: number
}

export interface DispatchTransferResponse {
  transferId: number
  status: TransferStatus
  consumedLayers: ConsumedLayer[]
  ledgerRefs: number[]
}

export interface ReceiveTransferLineInput {
  transferLineId: number
  qty: number
}

export interface CreatedTransferLayer {
  layerId: number
  warehouseId: number
  qtyReceived: number
  unitCost: number
}

export interface ReceiveTransferResponse {
  transferId: number
  status: TransferStatus
  createdLayers: CreatedTransferLayer[]
  ledgerRefs: number[]
}

// --- 5.5 Adjustments ------------------------------------------------------------------

export type AdjustmentStatus = "Posted"

export interface AdjustmentLine {
  adjLineId: number
  itemId: number
  binId: number | null
  batchId: number | null
  qtyDelta: number
}

export interface StockAdjustment {
  adjustmentId: number
  docNo: string
  warehouseId: number
  reasonCodeId: number
  status: AdjustmentStatus
  createdBy: number
  createdAt: string
  lines: AdjustmentLine[]
  ledgerRefs: number[]
}

export interface StockAdjustmentSummary {
  adjustmentId: number
  docNo: string
  warehouseId: number
  reasonCodeId: number
  status: AdjustmentStatus
  createdAt: string
}

export interface CreateAdjustmentLineInput {
  itemId: number
  binId?: number | null
  batchId?: number | null
  qtyDelta: number
}

export interface CreateAdjustmentRequest {
  warehouseId: number
  reasonCodeId: number
  lines: CreateAdjustmentLineInput[]
}

// --- 5.6 Counts ------------------------------------------------------------------

export type CountType = "Cycle" | "Full"
export type CountStatus = "Draft" | "Posted"

export interface CountLine {
  countLineId: number
  itemId: number
  binId: number | null
  systemQty: number
  countedQty: number | null
  variance: number | null
}

export interface StockCount {
  countId: number
  docNo: string
  warehouseId: number
  countType: CountType
  status: CountStatus
  createdBy: number
  createdAt: string
  lines: CountLine[]
}

export interface StockCountSummary {
  countId: number
  docNo: string
  warehouseId: number
  countType: CountType
  status: CountStatus
  createdAt: string
}

export interface CreateCountRequest {
  warehouseId: number
  countType: CountType
  itemIds: number[]
}

export interface EnterCountLineInput {
  countLineId: number
  countedQty: number
}

export interface EnterCountsRequest {
  lines: EnterCountLineInput[]
}

export interface EnterCountsResponse {
  lines: CountLine[]
}

export interface PostCountResponse {
  countId: number
  status: CountStatus
  adjustmentId: number
  ledgerRefs: number[]
}

// --- 5.7 Reorder alerts ------------------------------------------------------------------

export interface ReorderAlert {
  itemId: number
  warehouseId: number
  available: number
  reorderPoint: number
  reorderQty: number
  suggestedRequisitionQty: number
}

export interface ReorderRequisitionResponse {
  requisitionId: number
  docNo: string
  itemId: number
  warehouseId: number
  qty: number
  status: "Draft"
}

// --- §6 Reference data ------------------------------------------------------------------

export type ReasonCodeContext = "Adjustment" | "Return" | "CountVariance"

export interface ReasonCode {
  reasonCodeId: number
  code: string
  description: string
  context: ReasonCodeContext
}
