import { describe, expect, it } from "vitest";
import { mothershipSuggestBodySchema } from "@/modules/address/validation";
import { axelSearchRowToCandidate } from "@/lib/axel/candidates";
import { buildDetailsStateFromL1 } from "@/components/mothership/mothership-logged-in-details-form";
import { buildQuoteRequestBodyFromLoggedIn } 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 pickupConfirmed: MothershipAddressCandidate = {
  option_id: "place_pickup",
  display_label: "456 Oak Avenue unit 207, Fairhope, AL, USA",
  formatted_address: "456 Oak Avenue unit 207, Fairhope, AL, USA",
  street: "456 Oak Avenue unit 207",
  city: "Fairhope",
  state: "AL",
  zip: "36532",
  selectable: true,
};

const deliveryConfirmed: MothershipAddressCandidate = {
  option_id: "place_delivery",
  display_label: "789 East Elm Street suite 301, Brea, CA, USA",
  formatted_address: "789 East Elm Street suite 301, Brea, CA, USA",
  street: "789 East Elm Street suite 301",
  city: "Brea",
  state: "CA",
  zip: "92821",
  selectable: true,
};

const sample: MothershipLoggedInShipmentPayload = {
  pickupQuery: pickupConfirmed.display_label,
  deliveryQuery: deliveryConfirmed.display_label,
  pickupConfirmed,
  deliveryConfirmed,
  pickupAccessorials: ["cfs", "liftgate"],
  deliveryAccessorials: ["fbaAppointment"],
  readyDate: "2026-07-16",
  readyTime: "12:00 PM",
  timezone: "GMT+8",
  cargo: [
    {
      cargoType: "pallet",
      quantity: 2,
      weightLb: 250,
      lengthIn: 48,
      widthIn: 48,
      heightIn: 48,
    },
  ],
};

describe("mothershipSuggestBodySchema", () => {
  it("query 至少 3 字符", () => {
    expect(
      mothershipSuggestBodySchema.safeParse({
        customer_id: "CUST_001",
        query: "ab",
      }).success,
    ).toBe(false);
    expect(
      mothershipSuggestBodySchema.safeParse({
        customer_id: "CUST_001",
        query: "456 Oak",
      }).success,
    ).toBe(true);
  });
});

describe("axelSearchRowToCandidate（纯 query 兜底）", () => {
  it("用 mainText/secondaryText 解析候选", () => {
    const row = axelSearchRowToCandidate(
      {
        placeId: "ChIJtest",
        description: "456 Oak Avenue unit 207, Fairhope, AL, USA",
        mainText: "456 Oak Avenue unit 207",
        secondaryText: "Fairhope, AL",
      },
      { street: "456 Oak Avenue, Unit 207", city: "", state: "", zip: "" },
    );
    expect(row).not.toBeNull();
    expect(row!.option_id).toBe("ChIJtest");
    expect(row!.city).toBe("Fairhope");
    expect(row!.state).toBe("AL");
  });
});

describe("mothership logged-in → 真实询价映射", () => {
  it("二级从 confirmed candidate 预填地址", () => {
    const state = buildDetailsStateFromL1(sample);
    expect(state.pickup.address).toBe(pickupConfirmed.formatted_address);
    expect(state.delivery.address).toBe(deliveryConfirmed.formatted_address);
    expect(state.pickup.accessorials).toEqual(["cfs", "liftgate"]);
    expect(state.cargo[0]?.quantity).toBe("2");
    expect(state.cargo[0]?.weightLb).toBe("250");
    expect(state.requestDeliveryAppointment).toBe(true);
  });

  it("buildQuoteRequestBodyFromLoggedIn 对齐截图样例", () => {
    const body = buildQuoteRequestBodyFromLoggedIn(sample, "CUST_001");
    expect(body.customer_id).toBe("CUST_001");
    expect(body.pallet_count).toBe(2);
    expect(body.weight).toEqual({ value: 250, unit: "lb" });
    expect(body.dimensions).toEqual({
      length: 48,
      width: 48,
      height: 48,
      unit: "in",
    });
    expect(body.pickup_address.selected_from_mothership).toBe(true);
    expect(body.pickup_address.mothership_option_id).toBe("place_pickup");
    expect(body.delivery_address.mothership_option_id).toBe("place_delivery");
    expect(body.pickup_address.city).toBe("Fairhope");
    expect(body.delivery_address.city).toBe("Brea");
    expect(body.cargo_type).toBe("general_freight");
  });
});
