/**
 * Flock Freight 官网硬限（PRD §14.3，2026-07-13 实测）
 * 禁止与 MOTHERSHIP / CARGO_LIMITS 混用
 *
 * 字段上限之外另有拖车线性英尺约束：
 * 「Linear feet of shipment is larger than trailer」→ 53' (636 in)
 */
export const FLOCK_LIMITS = {
  palletCount: { min: 4, max: 20 },
  totalWeightLbMax: 45_000,
  dimIn: { lengthMax: 636, widthMax: 102, heightMax: 108, min: 1 },
  /** 53' 拖车可用线性英尺（官网实测） */
  trailerLinearFeetMax: 53,
  /** 调度高级选项（非 FCFS）须整票总重 > 此值才可选用，对齐官网灰色按钮 */
  schedulingWeightMinLb: 5000,
  zipPattern: /^\d{5}(-\d{4})?$/,
  defaultDimsIn: { length: 48, width: 40, height: 48 },
} as const;

export const FLOCK_VALIDATION_MESSAGES = {
  palletCount: "托盘件数只能输入 4 到 20",
  totalWeight: "整票总重不能超过 45000 lb",
  length: "单托长度不能超过 636 in",
  width: "单托宽度不能超过 102 in",
  height: "单托高度不能超过 108 in",
  linearFeet:
    "整票占用车长超过可用挂车长度，请下调件数或尺寸。",
  pickupZip: "需要提供提货邮编",
  deliveryZip: "需要提供派送邮编",
  pickupDate: "请选择提货日期",
  pickupDateTooEarly: "提货日期不能早于今天",
  pickupDateWeekend: "提货日期不能为周六或周日",
  zipFormat: "邮编须为 5 位数字（可选 +4）",
} as const;

/**
 * 估算占用线性英尺：拖车宽约 102in，托盘宽 ≤51 可双排，否则单排。
 * rows × (lengthIn/12) ≤ 53
 */
export function estimateFlockLinearFeet(
  palletCount: number,
  lengthIn: number,
  widthIn: number,
): number {
  const across = widthIn <= 51 ? 2 : 1;
  const rows = Math.ceil(Math.max(1, palletCount) / across);
  return (rows * Math.max(1, lengthIn)) / 12;
}

export function fitsFlockTrailerCargo(input: {
  palletCount: number;
  lengthIn: number;
  widthIn: number;
}): boolean {
  return (
    estimateFlockLinearFeet(
      input.palletCount,
      input.lengthIn,
      input.widthIn,
    ) <= FLOCK_LIMITS.trailerLinearFeetMax + 1e-6
  );
}
