import { flockPickupIsoFromDisplay } from "@/lib/flock/pickup-date";
import type { FlockQuoteInput } from "@/workers/rpa/flock/types";

/** POST /user/quotes 请求体（抓包契约） */
export type FlockQuotesApiRequest = {
  shipment: {
    pickupDate: string;
    originPostalCode: string;
    originLocationType: "BUSINESS_DOCK";
    originLiftgateRequired: boolean;
    insidePickupRequired: boolean;
    destinationPostalCode: string;
    destinationLocationType: "BUSINESS_DOCK";
    destinationLiftgateRequired: boolean;
    insideDeliveryRequired: boolean;
    items: Array<{
      packagingType: "PALLET_OTHER";
      hazardous: boolean;
      stackable: boolean;
      turnable: boolean;
      contentsDescription: string;
      freightClass: "CLASS_60";
      quantity: number;
      weightLbs: number;
      lengthIn: number;
      widthIn: number;
      heightIn: number;
    }>;
    deliveryTimeConstraint: "NONE";
    unloadingServicesRequired: boolean;
    blind: boolean;
    loadToRideRequested: boolean;
    dryVanAllowed: boolean;
    reeferAllowed: boolean;
    straightTruckAllowed: boolean;
    sprinterVanAllowed: boolean;
    partOfMultiShipmentMultiStopRoute: boolean;
    automatedTrackingRequired: boolean;
  };
  isEdiRequote: boolean;
  sameDayPricingOverrideRequested: boolean;
  additionalServiceRequest: null;
};

function toApiPickupDate(displayOrIso: string): string {
  const trimmed = displayOrIso.trim();
  if (/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) return trimmed;
  const iso = flockPickupIsoFromDisplay(trimmed);
  if (!iso) {
    throw new Error(`取货日期格式无效: ${displayOrIso}`);
  }
  return iso;
}

/** 由标准化 Flock 入参构造直连接口 payload */
export function buildFlockQuotesApiRequest(
  input: FlockQuoteInput,
): FlockQuotesApiRequest {
  return {
    shipment: {
      pickupDate: toApiPickupDate(input.pickupDate),
      originPostalCode: input.pickupZip.slice(0, 5),
      originLocationType: "BUSINESS_DOCK",
      originLiftgateRequired: false,
      insidePickupRequired: false,
      destinationPostalCode: input.deliveryZip.slice(0, 5),
      destinationLocationType: "BUSINESS_DOCK",
      destinationLiftgateRequired: false,
      insideDeliveryRequired: false,
      items: [
        {
          packagingType: "PALLET_OTHER",
          hazardous: false,
          stackable: false,
          turnable: false,
          contentsDescription: "Unknown",
          freightClass: "CLASS_60",
          quantity: input.palletCount,
          weightLbs: input.totalWeightLb,
          lengthIn: input.lengthIn,
          widthIn: input.widthIn,
          heightIn: input.heightIn,
        },
      ],
      deliveryTimeConstraint: "NONE",
      unloadingServicesRequired: false,
      blind: false,
      loadToRideRequested: false,
      dryVanAllowed: true,
      reeferAllowed: true,
      straightTruckAllowed: true,
      sprinterVanAllowed: false,
      partOfMultiShipmentMultiStopRoute: false,
      automatedTrackingRequired: false,
    },
    isEdiRequote: false,
    sameDayPricingOverrideRequested: false,
    additionalServiceRequest: null,
  };
}
