// Procurement DTOs (docs/11-BACKEND-PHASE1.md §3; FR-PROC-01..09). Mirrors the
// planned Dtos/Procurement/*.cs — no Procurement backend exists yet, see
// Frontend/PROGRESS.md §3 for the same frontend-only posture as GRN/Stock.

// --- 3.1 Requisitions --------------------------------------------------------------

export type RequisitionStatus = "Draft" | "Submitted"

export interface ReqLine {
  reqLineId: number
  itemId: number
  qty: number
  requiredBy: string
}

export interface Requisition {
  requisitionId: number
  docNo: string
  status: RequisitionStatus
  requestedBy: number
  createdAt: string
  lines: ReqLine[]
}

export interface RequisitionSummary {
  requisitionId: number
  docNo: string
  status: RequisitionStatus
  requestedBy: number
  createdAt: string
  lineCount: number
}

export interface CreateReqLineInput {
  itemId: number
  qty: number
  requiredBy: string
}

export interface CreateRequisitionRequest {
  lines: CreateReqLineInput[]
}

// --- 3.2 RFQs & Quotations ----------------------------------------------------------

/** Phase 1 only documents "Open" on creation; "Closed" is a frontend-only convenience
 * applied once a PO is created from the RFQ (see Frontend/PROGRESS.md §3 deviation note). */
export type RfqStatus = "Open" | "Closed"

export interface RfqLine {
  rfqLineId: number
  itemId: number
  qty: number
}

export interface Rfq {
  rfqId: number
  docNo: string
  requisitionId: number | null
  status: RfqStatus
  vendorIds: number[]
  createdAt: string
  lines: RfqLine[]
}

export interface RfqSummary {
  rfqId: number
  docNo: string
  requisitionId: number | null
  status: RfqStatus
  vendorIds: number[]
  createdAt: string
}

export interface CreateRfqLineInput {
  itemId: number
  qty: number
}

export interface CreateRfqRequest {
  requisitionId?: number | null
  vendorIds: number[]
  lines: CreateRfqLineInput[]
}

export interface QuotationLine {
  itemId: number
  unitPrice: number
  leadDays: number
}

export interface Quotation {
  quotationId: number
  rfqId: number
  vendorId: number
  createdAt: string
  lines: QuotationLine[]
}

export interface CreateQuotationRequest {
  vendorId: number
  lines: QuotationLine[]
}

export interface RfqComparisonCell {
  vendorId: number
  unitPrice: number
  leadDays: number
}

export interface RfqComparisonLine {
  itemId: number
  qty: number
  cells: RfqComparisonCell[]
}

export interface RfqComparison {
  rfqId: number
  vendorIds: number[]
  lines: RfqComparisonLine[]
}

// --- 3.3 Purchase Orders -------------------------------------------------------------

export type PurchaseOrderStatus =
  | "Draft"
  | "PendingApproval"
  | "Approved"
  | "PartiallyReceived"
  | "FullyReceived"
  | "Closed"
  | "Cancelled"

export interface PoLine {
  poLineId: number
  itemId: number
  uomId: number
  warehouseId: number
  qty: number
  unitPrice: number
  tax: number
  qtyReceived: number
}

export interface PoTotals {
  subTotal: number
  tax: number
  grandTotal: number
  currency: string
}

export interface PurchaseOrder {
  poId: number
  docNo: string
  vendorId: number
  requisitionId: number | null
  status: PurchaseOrderStatus
  approvalRequired: boolean
  createdBy: number
  createdAt: string
  updatedAt: string | null
  totals: PoTotals
  lines: PoLine[]
}

export interface PurchaseOrderSummary {
  poId: number
  docNo: string
  vendorId: number
  status: PurchaseOrderStatus
  approvalRequired: boolean
  createdAt: string
  totals: PoTotals
}

export interface CreatePoLineInput {
  itemId: number
  uomId: number
  warehouseId: number
  qty: number
  unitPrice: number
  tax: number
}

export interface CreatePurchaseOrderRequest {
  vendorId: number
  requisitionId?: number | null
  lines: CreatePoLineInput[]
}

/** PUT /purchase-orders/{poId} — edit-while-open, same line shape as create (FR-PROC-05, Option B). */
export type UpdatePurchaseOrderRequest = CreatePurchaseOrderRequest

export interface CancelPurchaseOrderRequest {
  reason: string
}

// --- 3.4 Purchase Returns -------------------------------------------------------------

export type PurchaseReturnStatus = "Posted"

export interface PurchaseReturnLine {
  returnLineId: number
  grnLineId: number
  itemId: number
  qty: number
}

export interface PurchaseReturn {
  returnId: number
  docNo: string
  vendorId: number
  warehouseId: number
  reasonCodeId: number
  status: PurchaseReturnStatus
  createdBy: number
  createdAt: string
  lines: PurchaseReturnLine[]
  ledgerRefs: number[]
}

export interface PurchaseReturnSummary {
  returnId: number
  docNo: string
  vendorId: number
  warehouseId: number
  reasonCodeId: number
  status: PurchaseReturnStatus
  createdAt: string
}

export interface CreatePurchaseReturnLineInput {
  grnLineId: number
  itemId: number
  qty: number
}

export interface CreatePurchaseReturnRequest {
  vendorId: number
  warehouseId: number
  reasonCodeId: number
  lines: CreatePurchaseReturnLineInput[]
}
