// Client-side UX validation only — required fields, format/range checks the
// browser can already see. Server-authoritative rules (referential existence,
// concurrency, reason-code context) are never re-implemented here. Same
// pattern as lib/validations/procurement.ts's validateReturnLine.

export function validateSalesReturnLine(input: { salesInvoiceLineId: number | null; qty: string; maxQty: number | null }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.salesInvoiceLineId) errors.salesInvoiceLineId = "Select an invoiced 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 remaining returnable qty — the server remains authoritative.
  if (input.maxQty !== null && qty > input.maxQty) {
    errors.qty = input.maxQty <= 0
      ? "This line has already been fully returned"
      : `Insufficient quantity — only ${input.maxQty} remain returnable`
  }
  return errors
}
