// Client-side UX validation only (docs/20-FRONTEND.md §3.1) — required fields,
// format/range checks the browser can already see. Server-authoritative rules
// (SKU uniqueness, master-in-use on delete, concurrency) are never
// re-implemented here; the server's ProblemDetails is the final word (§3.3).
// Hand-rolled (not zod) to match the plain-hooks posture most screens this
// session use — lib/validations/grn.ts is the one deliberate zod exception.

export function validateItemForm(input: {
  sku: string
  name: string
  categoryId: number | null
  baseUomId: number | null
}): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.sku.trim()) errors.sku = "SKU is required"
  if (!input.name.trim()) errors.name = "Item name is required"
  if (!input.categoryId) errors.categoryId = "Select a category"
  if (!input.baseUomId) errors.baseUomId = "Select a base UOM"
  return errors
}

export function validateReorderLine(input: { warehouseId: number | null; reorderPoint: string; reorderQty: string }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.warehouseId) errors.warehouseId = "Select a warehouse"
  const point = Number(input.reorderPoint)
  if (input.reorderPoint === "" || Number.isNaN(point) || point < 0) errors.reorderPoint = "Reorder point cannot be negative"
  const qty = Number(input.reorderQty)
  if (!input.reorderQty || Number.isNaN(qty) || qty <= 0) errors.reorderQty = "Reorder quantity must be greater than 0"
  return errors
}

export function validateConversionLine(input: { fromUom: number | null; toUom: number | null; factor: string }): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.fromUom) errors.fromUom = "Select a UOM"
  if (!input.toUom) errors.toUom = "Select a UOM"
  if (input.fromUom && input.toUom && input.fromUom === input.toUom) errors.toUom = "From and to UOM must differ"
  const factor = Number(input.factor)
  if (!input.factor || Number.isNaN(factor) || factor <= 0) errors.factor = "Factor must be greater than 0"
  return errors
}

export function validateUomName(name: string): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!name.trim()) errors.name = "UOM name is required"
  return errors
}

export function validateCategoryName(name: string): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!name.trim()) errors.name = "Category name is required"
  return errors
}

export function validateBrandName(name: string): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!name.trim()) errors.name = "Brand name is required"
  return errors
}

/** Renamed from validateVariantCategoryName — "variant categories" are Item Types now. */
export function validateItemTypeName(name: string): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!name.trim()) errors.name = "Item type name is required"
  return errors
}

export function validateVariantItemForm(input: {
  categoryId: number | null
  hasVariants: boolean
}): Record<string, string> {
  const errors: Record<string, string> = {}
  if (!input.categoryId) errors.categoryId = "Select a category"
  if (!input.hasVariants) errors.variants = "Check at least one variant category and add its values"
  return errors
}

/**
 * Fixed-price mode requires every generated variant to carry a sale price > 0
 * (docs/20 §3.1). Returns a map keyed by variant key → message; empty when valid.
 * In "stock" mode there is nothing to validate (prices are sent as null).
 */
export function validateVariantPrices(
  variantKeys: string[],
  priceFor: (key: string) => string,
): Record<string, string> {
  const errors: Record<string, string> = {}
  for (const key of variantKeys) {
    const raw = priceFor(key).trim()
    const value = Number(raw)
    if (raw === "" || Number.isNaN(value) || value <= 0) {
      errors[key] = "Enter a price greater than 0"
    }
  }
  return errors
}
