// Procurement DTOs (docs/11-BACKEND-PHASE1.md §3; FR-PROC-01..09).
// Mirrors the live Dtos/Procurement/*.cs — the API contract is the source of truth.

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

export type RequisitionStatus = "Draft" | "Submitted"

export interface ReqLine {
  reqLineId: number
  itemId: number
  qty: number
  /** Date only, `YYYY-MM-DD`. */
  requiredBy: string | null
}

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 | null
}

export interface CreateRequisitionRequest {
  lines: CreateReqLineInput[]
}

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

export type RfqStatus = "Open" | "Closed"

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

/**
 * Note: the server returns no `vendorIds` or `createdAt` on an RFQ. The invited-vendor
 * list is validated on create but not persisted (no RFQ↔vendor link in the model);
 * quotations reference vendors directly.
 */
export interface Rfq {
  rfqId: number
  docNo: string
  requisitionId: number
  status: RfqStatus
  lines: RfqLine[]
}

export interface RfqSummary {
  rfqId: number
  docNo: string
  requisitionId: number
  status: RfqStatus
  lineCount: number
  quotationCount: number
}

export interface CreateRfqLineInput {
  itemId: number
  qty: number
}

export interface CreateRfqRequest {
  /** Required — an RFQ is always raised against a requisition. */
  requisitionId: number
  vendorIds: number[]
  lines: CreateRfqLineInput[]
}

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

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

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

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

export interface RfqComparisonRow {
  itemId: number
  qty: number
  quotes: RfqComparisonCell[]
}

export interface RfqComparison {
  rfqId: number
  vendorIds: number[]
  rows: RfqComparisonRow[]
}

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

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

export interface PoLine {
  poLineId: number
  itemId: number
  warehouseId: number
  qty: number
  unitPrice: number
  /** Rate, 0..1 (e.g. 0.18), not an amount. */
  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
  warehouseId: number
  qty: number
  unitPrice: number
  tax: number
}

export interface CreatePurchaseOrderRequest {
  vendorId: number
  requisitionId?: number | null
  lines: CreatePoLineInput[]
  /** When true the PO is created as an editable/deletable Draft; false (default) auto-approves. */
  saveAsDraft?: boolean
}

/** PUT /purchase-orders/{poId} — edit a Draft only (FR-PROC-05, revised); same line shape as create. */
export type UpdatePurchaseOrderRequest = Omit<CreatePurchaseOrderRequest, "saveAsDraft">

export interface CancelPurchaseOrderRequest {
  reason?: string | null
}

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

export type PurchaseReturnStatus = "Draft" | "Posted"

export interface PurchaseReturnLine {
  returnLineId: number
  /** Optional: a return may reference the originating GRN line for traceability. */
  grnLineId: number | null
  itemId: number
  qty: number
}

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

export interface PurchaseReturnSummary {
  returnId: number
  docNo: string
  vendorId: number
  warehouseId: number
  reasonCodeId: number
  status: PurchaseReturnStatus
  createdBy: number
  createdAt: string
  lineCount: number
  glJournalNo: string | null
}

export interface CreatePurchaseReturnLineInput {
  grnLineId?: number | null
  itemId: number
  qty: number
}

export interface CreatePurchaseReturnRequest {
  vendorId: number
  warehouseId: number
  /** Mandatory (FR-PROC-08); omitting it returns 400 REASON_CODE_REQUIRED. */
  reasonCodeId: number
  lines: CreatePurchaseReturnLineInput[]
}
