import { describe, expect, it } from "vitest";
import { buildFlockQuotesApiRequest } from "@/lib/flock/quote-payload";
import { mapFlockFulfillmentOptionsToLines } from "@/lib/flock/map-fulfillment-options";
import type { FlockQuoteInput } from "@/workers/rpa/flock/types";

const sampleInput: FlockQuoteInput = {
  pickupDate: "07/15/2026",
  pickupZip: "90001",
  deliveryZip: "75201",
  palletCount: 6,
  totalWeightLb: 10_000,
  lengthIn: 48,
  widthIn: 40,
  heightIn: 48,
};

describe("buildFlockQuotesApiRequest", () => {
  it("对齐抓包契约字段", () => {
    const body = buildFlockQuotesApiRequest(sampleInput);
    expect(body.shipment.pickupDate).toBe("2026-07-15");
    expect(body.shipment.originPostalCode).toBe("90001");
    expect(body.shipment.destinationPostalCode).toBe("75201");
    expect(body.shipment.items[0]).toMatchObject({
      quantity: 6,
      weightLbs: 10000,
      lengthIn: 48,
      freightClass: "CLASS_60",
      packagingType: "PALLET_OTHER",
    });
    expect(body.isEdiRequote).toBe(false);
  });
});

describe("mapFlockFulfillmentOptionsToLines", () => {
  it("映射 GUARANTEED_HUBLESS 与 STANDARD 最低价", () => {
    const { quotes, reference } = mapFlockFulfillmentOptionsToLines({
      referenceNumber: "FFW-TEST",
      fulfillmentOptions: [
        {
          fulfillmentCategory: "STANDARD",
          rateUsd: "1512.67",
          transitTimeDaysMin: 3,
          transitTimeDaysMax: 7,
          carrierName: "A",
        },
        {
          fulfillmentCategory: "STANDARD",
          rateUsd: "1410.09",
          transitTimeDaysMin: 3,
          transitTimeDaysMax: 5,
          carrierName: "Roadrunner",
        },
        {
          fulfillmentCategory: "GUARANTEED_HUBLESS",
          rateUsd: "2091.00",
          transitTimeDaysMin: 4,
          transitTimeDaysMax: 4,
          transitSupportedOnWeekendsAndHolidays: true,
          carrierName: "Flock Freight Estimate",
        },
        {
          fulfillmentCategory: "GUARANTEED_HUBLESS",
          rateUsd: "1970.00",
          transitTimeDaysMin: 4,
          transitTimeDaysMax: 4,
          transitSupportedOnWeekendsAndHolidays: true,
          carrierName: "Flock Freight Estimate",
        },
      ],
    });
    expect(reference).toBe("FFW-TEST");
    expect(quotes).toHaveLength(2);
    const direct = quotes.find((q) => q.tier === "flock_direct");
    const standard = quotes.find((q) => q.tier === "standard");
    expect(direct?.totalUsd).toBe(1970);
    expect(direct?.label).toBe("FlockDirect®");
    expect(standard?.totalUsd).toBe(1410.09);
    expect(standard?.transitDays).toBe("3-5");
  });
});
