import { describe, expect, it } from "vitest";
import {
  DEFAULT_FTL_DEMO,
  DEFAULT_FTL_FTL_EXPEDITED_DEMO,
  DEFAULT_PRIORITY1_DEMO,
  PRIORITY1_CANONICAL_PATHS,
} from "@/workers/rpa/priority1/demo-input";
import {
  buildPriority1FormPlan,
  collectFieldOptionsCatalog,
  formatCompleteFormLogicReference,
  formatFormLogicReference,
  getNextFields,
  SCENARIO_CHEATSHEET,
  STEP2_HASH_FALLBACK,
} from "@/workers/rpa/priority1/form-logic-map";

describe("priority1 form-logic-map", () => {
  it("LTL 计划包含包装 Pallet 与 Step1 邮编", () => {
    const plan = buildPriority1FormPlan(DEFAULT_PRIORITY1_DEMO);
    const keys = plan.map((p) => p.field.key);
    expect(keys).toContain("packaging");
    expect(plan.find((p) => p.field.key === "packaging")?.value).toBe("Pallet");
    expect(plan.find((p) => p.field.key === "originZip")?.value).toBe("10118");
  });

  it("FTL Dry Van 分支含拖车、货描、重量、托盘", () => {
    const plan = buildPriority1FormPlan(DEFAULT_FTL_DEMO);
    const labels = plan.map((p) => p.field.label);
    expect(labels.some((l) => /Dry Van/i.test(l))).toBe(true);
    expect(plan.find((p) => p.field.key === "commodity")?.value).toBe("Electronics");
    expect(plan.find((p) => p.field.key === "weightLb")?.value).toBe("15000");
    expect(plan.find((p) => p.field.key === "palletCount")?.value).toBe("18");
  });

  it("getNextFields FTL 未选拖车时只返回拖车卡", () => {
    const next = getNextFields({ shipmentType: "ftl", step: 2 });
    expect(next.every((f) => f.key.startsWith("ftlTrailer_"))).toBe(true);
  });

  it("Open Deck 分支追加子类型字段", () => {
    const next = getNextFields({
      shipmentType: "ftl",
      step: 2,
      ftlTrailer: "open_deck",
    });
    expect(next.some((f) => f.key === "openDeckSubtype")).toBe(true);
  });

  it("场景速查与哈希回退齐全", () => {
    expect(SCENARIO_CHEATSHEET.length).toBeGreaterThanOrEqual(6);
    expect(STEP2_HASH_FALLBACK.ftl).toContain("Truckload");
    expect(formatFormLogicReference(DEFAULT_FTL_DEMO)).toContain("Dry Van");
  });

  it("FTL 内嵌 Expedited 计划不含托盘字段", () => {
    const plan = buildPriority1FormPlan(DEFAULT_FTL_FTL_EXPEDITED_DEMO);
    expect(plan.some((p) => p.field.key === "palletCount")).toBe(false);
  });

  it("canonical 路径覆盖三种主类型与全部 FTL 拖车", () => {
    expect(PRIORITY1_CANONICAL_PATHS.length).toBeGreaterThanOrEqual(20);
    const types = new Set(PRIORITY1_CANONICAL_PATHS.map((p) => p.input.shipmentType));
    expect(types).toEqual(new Set(["ltl", "ftl", "expedited"]));
    const ftlTrailers = PRIORITY1_CANONICAL_PATHS.filter(
      (p) => p.input.shipmentType === "ftl",
    ).map((p) => p.input.ftlTrailer);
    expect(new Set(ftlTrailers)).toEqual(
      new Set(["dry_van", "open_deck", "temperature_controlled", "ftl_expedited"]),
    );
  });

  it("完整参考含路径矩阵与字段目录", () => {
    const md = formatCompleteFormLogicReference(PRIORITY1_CANONICAL_PATHS);
    expect(md).toContain("路径矩阵");
    expect(md).toContain("全字段可选值");
    expect(md).toContain("P01-ltl-residential");
    expect(collectFieldOptionsCatalog().some((r) => r.key === "packaging")).toBe(
      true,
    );
  });
});
