import { describe, expect, it } from "vitest";
import {
  assertFourTiers,
  normalizeToFourTiers,
  prepareMotherShipStorageQuotes,
} from "@/modules/quote/quote-completeness";

const FOUR_TIERS = [
  { service_level: "standard", rate_option: "lowest" },
  { service_level: "standard", rate_option: "fastest" },
  { service_level: "guaranteed", rate_option: "lowest" },
  { service_level: "guaranteed", rate_option: "fastest" },
];

describe("assertFourTiers", () => {
  it("4 档 → 通过", () => {
    expect(() => assertFourTiers(FOUR_TIERS)).not.toThrow();
  });

  it("仅 2 档 lowest → normalize 后通过", () => {
    expect(() =>
      assertFourTiers([
        { service_level: "standard", rate_option: "lowest" },
        { service_level: "guaranteed", rate_option: "lowest" },
      ]),
    ).not.toThrow();
  });

  it("缺 guaranteed 整档 → 仍通过", () => {
    expect(() =>
      assertFourTiers(
        FOUR_TIERS.filter((t) => t.service_level !== "guaranteed"),
      ),
    ).not.toThrow();
  });

  it("有 guaranteed/lowest 无 fastest → 通过", () => {
    const incomplete = FOUR_TIERS.filter(
      (t) => !(t.service_level === "guaranteed" && t.rate_option === "fastest"),
    );
    expect(() => assertFourTiers(incomplete)).not.toThrow();
  });

  it("仅 bestValue 三 Tab（Denver→Boston 类路线）→ 通过", () => {
    const tiers = [
      { service_level: "standard", rate_option: "bestValue" },
      { service_level: "guaranteed", rate_option: "bestValue" },
      { service_level: "dedicated", rate_option: "bestValue" },
    ];
    expect(() => assertFourTiers(tiers)).not.toThrow();
    const ui = prepareMotherShipStorageQuotes(tiers);
    expect(ui).toHaveLength(3);
  });

  it("含 bestValue 扩展档 → 保留 API 返回的全部档位", () => {
    const six = [
      ...FOUR_TIERS,
      { service_level: "standard", rate_option: "bestValue" },
      { service_level: "guaranteed", rate_option: "bestValue" },
    ];
    const normalized = normalizeToFourTiers(six);
    expect(normalized).toHaveLength(6);
    expect(() => assertFourTiers(six)).not.toThrow();
  });

  it("2+1 动态档位全部保留", () => {
    const tiers = [
      { service_level: "standard", rate_option: "lowest" },
      { service_level: "standard", rate_option: "fastest" },
      { service_level: "guaranteed", rate_option: "lowest" },
    ];
    expect(normalizeToFourTiers(tiers)).toHaveLength(3);
  });

  it("3+3 动态档位全部保留", () => {
    const tiers = [
      { service_level: "standard", rate_option: "lowest" },
      { service_level: "standard", rate_option: "fastest" },
      { service_level: "standard", rate_option: "bestValue" },
      { service_level: "guaranteed", rate_option: "lowest" },
      { service_level: "guaranteed", rate_option: "fastest" },
      { service_level: "guaranteed", rate_option: "bestValue" },
    ];
    expect(normalizeToFourTiers(tiers)).toHaveLength(6);
  });
});

describe("prepareMotherShipStorageQuotes", () => {
  it("含 dedicated 且 Standard 保留 absolute lowest", () => {
    const ui = prepareMotherShipStorageQuotes([
      { service_level: "standard", rate_option: "lowest" },
      { service_level: "standard", rate_option: "bestValue" },
      { service_level: "standard", rate_option: "fastest" },
      { service_level: "guaranteed", rate_option: "lowest" },
      { service_level: "guaranteed", rate_option: "fastest" },
      { service_level: "dedicated", rate_option: "bestValue" },
    ]);
    expect(ui.some((q) => q.service_level === "dedicated")).toBe(true);
    expect(ui.some((q) => q.service_level === "standard" && q.rate_option === "lowest")).toBe(true);
    expect(ui.some((q) => q.service_level === "standard" && q.rate_option === "bestValue")).toBe(true);
  });
});
