import { describe, expect, it } from "vitest";
import {
  buildLoggedInAddressSearchQuery,
  formatLoggedInReadyGridCell,
  MOTHERSHIP_CREATE_SHIPMENT_URL,
  parseLoggedInRateCardText,
} from "@/workers/rpa/mothership-logged-in-quote";
import { filterQuotesForMotherShipUiDisplay } from "@/lib/constants/mothership-ui-tiers";
import { validateQuoteSchema } from "@/workers/rpa/quote-capture/quote-schema-validator";

describe("MOTHERSHIP_CREATE_SHIPMENT_URL", () => {
  it("登录态一级表单直达 /ship（避免菜单文案超时）", () => {
    expect(MOTHERSHIP_CREATE_SHIPMENT_URL).toBe(
      "https://dashboard.mothership.com/ship",
    );
  });
});

describe("parseLoggedInRateCardText", () => {
  it("解析 ABF Direct 卡片文本", () => {
    const item = parseLoggedInRateCardText(
      "Selected ABF Direct $835.61 Estimated 5 business days",
    );
    expect(item?.carrier).toBe("ABF Direct");
    expect(item?.rawTotal).toBe(835.61);
    expect(item?.transitDays).toBe("5");
    expect(item?.serviceLevel).toBe("standard");
  });

  it("解析 XPO Direct", () => {
    const item = parseLoggedInRateCardText(
      "XPO Direct $1,015.28 Estimated 4 business days",
    );
    expect(item?.carrier).toBe("XPO Direct");
    expect(item?.rawTotal).toBe(1015.28);
  });

  it("解析区间时效 5-7 business days", () => {
    const item = parseLoggedInRateCardText(
      "SAIA Direct $900.00 5-7 business days",
    );
    expect(item?.transitDays).toBe("5-7");
  });

  it("无时效文案时用待确认，且通过 schema 校验（避免 standard/bestValue 时效为空）", () => {
    const item = parseLoggedInRateCardText("Daylight Direct $733.98");
    expect(item?.transitDays).toBe("待确认");
    expect(item?.rateOption).toBe("bestValue");
    expect(validateQuoteSchema([item!])).toHaveLength(1);
  });
});

describe("formatLoggedInReadyGridCell", () => {
  it("ISO 日期转为 gridcell 名", () => {
    expect(formatLoggedInReadyGridCell("2026-07-16")).toBe("Thu Jul 16");
  });
});

describe("buildLoggedInAddressSearchQuery", () => {
  it("优先用 street/city/state/zip，避开 Places description 的 floor/suite", () => {
    const q = buildLoggedInAddressSearchQuery({
      street: "2020 West Washington Boulevard",
      city: "Los Angeles",
      state: "CA",
      zip: "",
      placeId: "ChIJ-test",
      formattedAddress:
        "2020 West Washington Boulevard floor 5 rm 508, Los Angeles, CA, USA",
      selectedFromSuggestions: true,
      mothershipOptionId: "ChIJ-test",
      mothershipDisplayLabel:
        "2020 West Washington Boulevard floor 5 rm 508, Los Angeles, CA, USA",
      selectedFromMothership: true,
    });
    expect(q).toBe("2020 West Washington Boulevard, Los Angeles, CA");
    expect(q).not.toMatch(/floor|rm 508/i);
  });

  it("ste/suite 也会从 street 剥离", () => {
    const q = buildLoggedInAddressSearchQuery({
      street: "555 Market St ste 1001",
      city: "San Francisco",
      state: "CA",
      zip: "94105",
      placeId: "ChIJ-sf",
      formattedAddress: "555 Market St ste 1001, San Francisco, CA, USA",
      selectedFromSuggestions: true,
      mothershipOptionId: "ChIJ-sf",
      mothershipDisplayLabel: "555 Market St ste 1001, San Francisco, CA, USA",
      selectedFromMothership: true,
    });
    expect(q).toBe("555 Market St, San Francisco, CA, 94105");
    expect(q).not.toMatch(/ste|1001/i);
  });
});

describe("filterQuotesForMotherShipUiDisplay 多承运商", () => {
  it("同档不同承运商均保留", () => {
    const ui = filterQuotesForMotherShipUiDisplay([
      {
        service_level: "standard",
        rate_option: "bestValue",
        carrier: "ABF Direct",
        raw: 835,
      },
      {
        service_level: "standard",
        rate_option: "bestValue",
        carrier: "XPO Direct",
        raw: 1015,
      },
    ]);
    expect(ui).toHaveLength(2);
  });
});
