import { describe, expect, it } from "vitest";
import {
  buildQuoteRequestBodyFromLoggedIn,
  resolveLoggedInPalletCount,
} from "@/lib/frontend/mothership-logged-in-quote-body";
import type { MothershipLoggedInShipmentPayload } from "@/components/mothership/mothership-logged-in-shipment-form";
import type { MothershipAddressCandidate } from "@/lib/frontend/types";

const pickup: MothershipAddressCandidate = {
  option_id: "p1",
  display_label: "A, Fairhope, AL",
  formatted_address: "A, Fairhope, AL",
  street: "A",
  city: "Fairhope",
  state: "AL",
  zip: "36532",
};

const delivery: MothershipAddressCandidate = {
  option_id: "d1",
  display_label: "B, Brea, CA",
  formatted_address: "B, Brea, CA",
  street: "B",
  city: "Brea",
  state: "CA",
  zip: "92821",
};

function payload(
  cargo: MothershipLoggedInShipmentPayload["cargo"],
): MothershipLoggedInShipmentPayload {
  return {
    pickupQuery: pickup.display_label,
    deliveryQuery: delivery.display_label,
    pickupConfirmed: pickup,
    deliveryConfirmed: delivery,
    pickupAccessorials: [],
    deliveryAccessorials: [],
    readyDate: "2026-07-16",
    readyTime: "12:00 PM",
    timezone: "GMT+8",
    cargo,
  };
}

describe("mothership-logged-in-quote-body", () => {
  it("多行托盘 quantity 合计", () => {
    expect(
      resolveLoggedInPalletCount(
        payload([
          {
            cargoType: "pallet",
            quantity: 2,
            weightLb: 250,
            lengthIn: 48,
            widthIn: 48,
            heightIn: 48,
          },
          {
            cargoType: "pallet",
            quantity: 3,
            weightLb: 100,
            lengthIn: 40,
            widthIn: 40,
            heightIn: 40,
          },
        ]),
      ),
    ).toBe(5);
  });

  it("询价体重取首行单件重量", () => {
    const body = buildQuoteRequestBodyFromLoggedIn(
      payload([
        {
          cargoType: "pallet",
          quantity: 2,
          weightLb: 250,
          lengthIn: 48,
          widthIn: 48,
          heightIn: 48,
        },
      ]),
      "CUST_001",
    );
    expect(body.weight.value).toBe(250);
    expect(body.pallet_count).toBe(2);
  });

  it("透传附加服务与可提货时间", () => {
    const base = payload([
      {
        cargoType: "pallet",
        quantity: 1,
        weightLb: 100,
        lengthIn: 48,
        widthIn: 40,
        heightIn: 48,
      },
    ]);
    const body = buildQuoteRequestBodyFromLoggedIn(
      {
        ...base,
        pickupAccessorials: ["liftgate", "residential"],
        deliveryAccessorials: ["appointment"],
        readyDate: "2026-07-16",
        readyTime: "3:00 PM",
      },
      "CUST_001",
    );
    expect(body.pickup_accessorials).toEqual(["liftgate", "residential"]);
    expect(body.delivery_accessorials).toEqual(["appointment"]);
    expect(body.ready_date).toBe("2026-07-16");
    expect(body.ready_time).toBe("3:00 PM");
  });

  it("透传多行 cargo_lines", () => {
    const body = buildQuoteRequestBodyFromLoggedIn(
      payload([
        {
          cargoType: "pallet",
          quantity: 2,
          weightLb: 250,
          lengthIn: 48,
          widthIn: 40,
          heightIn: 48,
        },
        {
          cargoType: "box",
          quantity: 2,
          weightLb: 250,
          lengthIn: 50,
          widthIn: 40,
          heightIn: 50,
        },
      ]),
      "CUST_001",
    );
    expect(body.cargo_lines).toHaveLength(2);
    expect(body.cargo_lines?.[1]?.cargo_type).toBe("box");
    expect(body.pallet_count).toBe(4);
  });

  it("透传二级 details 字段", () => {
    const body = buildQuoteRequestBodyFromLoggedIn(
      payload([
        {
          cargoType: "pallet",
          quantity: 1,
          weightLb: 100,
          lengthIn: 48,
          widthIn: 40,
          heightIn: 48,
        },
      ]),
      "CUST_001",
      {
        pickup: {
          companyName: "Pickup Co",
          suite: "Ste 100",
          contactFirst: "A",
          contactLast: "B",
          contactEmail: "pickup@example.com",
          contactPhone: "5551112222",
          reference: "REF-1",
          notes: "note",
          opensAt: "8:00 AM",
          closesAt: "5:00 PM",
        },
        delivery: {
          companyName: "Delivery Co",
          suite: "",
          contactFirst: "C",
          contactLast: "D",
          contactEmail: "delivery@example.com",
          contactPhone: "5553334444",
          reference: "",
          notes: "",
          opensAt: "9:00 AM",
          closesAt: "6:00 PM",
        },
        requestDeliveryAppointment: true,
        fbaNumber: "FBA-123",
        fbaPoNumber: "PO-456",
        cargo: [
          {
            pieceCountType: "Pieces",
            pieceCountQty: 10,
            description: "General freight",
            nmfc: "",
            hazmat: false,
            alcohol: false,
            tobacco: false,
          },
        ],
      },
    );
    expect(body.mothership_details?.pickup.company_name).toBe("Pickup Co");
    expect(body.mothership_details?.delivery.contact_email).toBe(
      "delivery@example.com",
    );
    expect(body.mothership_details?.request_delivery_appointment).toBe(true);
    expect(body.mothership_details?.cargo?.[0]?.piece_count_type).toBe("Pieces");
  });
});
