// Client-side UX validation only (docs/20-FRONTEND.md §3.1) — required fields,
// format/range checks the browser can already see. Server-authoritative rules
// (referential existence, PO editability, concurrency, stock sufficiency on
// returns) are never re-implemented here; the server's ProblemDetails is the
// final word (§3.3). Same pattern as lib/validations/grn.ts.

export function validateRequisitionLine(input: { itemId: number | null; qty: string; requiredBy: string }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.itemId) errors.itemId = "Select an item"
  const qty = Number(input.qty)
  if (!input.qty || Number.isNaN(qty) || qty <= 0) errors.qty = "Quantity must be greater than 0"
  if (!input.requiredBy) errors.requiredBy = "Required-by date is needed"
  return errors
}

export function validateRfqLine(input: { itemId: number | null; qty: string }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.itemId) errors.itemId = "Select an item"
  const qty = Number(input.qty)
  if (!input.qty || Number.isNaN(qty) || qty <= 0) errors.qty = "Quantity must be greater than 0"
  return errors
}

export function validateQuotationLine(input: { unitPrice: string; leadDays: string }): Record<string, string> {
  const errors: Record<string, string> = {}
  const unitPrice = Number(input.unitPrice)
  if (input.unitPrice === "" || Number.isNaN(unitPrice) || unitPrice < 0) errors.unitPrice = "Unit price cannot be negative"
  const leadDays = Number(input.leadDays)
  if (input.leadDays === "" || Number.isNaN(leadDays) || leadDays < 0) errors.leadDays = "Lead days cannot be negative"
  return errors
}

export function validatePoLine(input: {
  itemId: number | null
  warehouseId: number | null
  qty: string
  unitPrice: string
  tax: string
}): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.itemId) errors.itemId = "Select an item"
  if (!input.warehouseId) errors.warehouseId = "Select a warehouse"
  const qty = Number(input.qty)
  if (!input.qty || Number.isNaN(qty) || qty <= 0) errors.qty = "Quantity must be greater than 0"
  const unitPrice = Number(input.unitPrice)
  if (input.unitPrice === "" || Number.isNaN(unitPrice) || unitPrice < 0) errors.unitPrice = "Unit price cannot be negative"
  const tax = Number(input.tax)
  if (input.tax === "" || Number.isNaN(tax) || tax < 0) errors.tax = "Tax rate cannot be negative"
  return errors
}

export function validateReturnLine(input: { grnLineId: number | null; qty: string; maxQty: number | null }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.grnLineId) errors.grnLineId = "Select a received line"
  const qty = Number(input.qty)
  if (!input.qty || Number.isNaN(qty) || qty <= 0) errors.qty = "Quantity must be greater than 0"
  // Client-side sanity bound on the originally received qty — the server is still
  // authoritative on live available stock (STOCK_NEGATIVE_BLOCKED, docs/20 §3.1).
  if (input.maxQty !== null && qty > input.maxQty) errors.qty = `Cannot exceed the received quantity (${input.maxQty})`
  return errors
}
