import { describe, expect, it } from "vitest";
import type { CargoCommitProbe } from "@/workers/rpa/cargo-lock";
import {
  buildWeightDisplayForUnit,
  inferWeightUnitFromFieldHint,
  isStaleCargoState,
  probeWeightMatchesExpected,
  resolveMotherShipWeightDisplay,
} from "@/workers/rpa/cargo-weight";

const expected500x2 = {
  ...resolveMotherShipWeightDisplay(500, "Weight (lbs)"),
  palletCount: 2,
};

function probe(
  overrides: Partial<CargoCommitProbe>,
): CargoCommitProbe {
  return {
    hasSummaryChip: true,
    visibleEditableCargoInputs: 0,
    editorOpen: false,
    internalPalletCount: 2,
    internalWeight: 500,
    widgetTextPreview: "2 托盘s 40W, x 48L, x 48H, x 500Lbs",
    ...overrides,
  };
}

describe("inferWeightUnitFromFieldHint", () => {
  it("lbs placeholder → lb", () => {
    expect(inferWeightUnitFromFieldHint("Weight (lbs)")).toBe("lb");
  });

  it("kg placeholder → kg", () => {
    expect(inferWeightUnitFromFieldHint("Weight (kg)")).toBe("kg");
  });

  it("合并多 spec 字符串含 kg 时仍优先 lb（有 lbs 标记）", () => {
    const joined = "Weight (lbs) Weight (kg) 重量（公斤）";
    expect(inferWeightUnitFromFieldHint(joined)).toBe("lb");
  });
});

describe("buildWeightDisplayForUnit", () => {
  it("lb 页直接填原始 lb，不换算", () => {
    expect(buildWeightDisplayForUnit(503, "lb")).toEqual({
      displayValue: "503",
      unit: "lb",
      weightLbPerPallet: 503,
    });
  });

  it("kg 页才换算", () => {
    const r = buildWeightDisplayForUnit(500, "kg");
    expect(r.unit).toBe("kg");
    expect(Number(r.displayValue)).toBeCloseTo(226.8, 0);
  });
});

describe("resolveMotherShipWeightDisplay", () => {
  it("lbs 字段填单托 lb", () => {
    expect(resolveMotherShipWeightDisplay(500, "Weight (lbs)")).toEqual({
      displayValue: "500",
      unit: "lb",
      weightLbPerPallet: 500,
    });
  });

  it("kg 字段填单托 kg", () => {
    const r = resolveMotherShipWeightDisplay(500, "Weight (kg)");
    expect(r.unit).toBe("kg");
    expect(Number(r.displayValue)).toBeCloseTo(226.8, 0);
  });

  it("503lb 在 lbs 页不得变成 228.16", () => {
    expect(resolveMotherShipWeightDisplay(503, "Weight (lbs)").displayValue).toBe(
      "503",
    );
  });
});

describe("probeWeightMatchesExpected", () => {
  it("单托 500lb × 2 托 → 匹配", () => {
    expect(probeWeightMatchesExpected(probe({}), expected500x2)).toBe(true);
  });

  it("widget 残留 2296lb → 不匹配", () => {
    expect(
      probeWeightMatchesExpected(
        probe({
          internalWeight: 2296,
          widgetTextPreview: "2 托盘s x 2296Lbs",
        }),
        expected500x2,
      ),
    ).toBe(false);
  });

  it("托盘数不一致 → 不匹配", () => {
    expect(
      probeWeightMatchesExpected(
        probe({ internalPalletCount: 4 }),
        expected500x2,
      ),
    ).toBe(false);
  });

  it("摘要总重匹配 → 通过", () => {
    expect(
      probeWeightMatchesExpected(
        {
          hasSummaryChip: true,
          visibleEditableCargoInputs: 0,
          editorOpen: false,
          internalPalletCount: 3,
          internalWeight: null,
          widgetTextPreview: "3 托盘s x 1371Lbs",
        },
        {
          displayValue: "457",
          unit: "lb",
          weightLbPerPallet: 457,
          palletCount: 3,
        },
      ),
    ).toBe(true);
  });
});

describe("isStaleCargoState", () => {
  it("无摘要 → 非残留", () => {
    expect(
      isStaleCargoState(
        probe({ hasSummaryChip: false, internalWeight: null }),
        expected500x2,
      ),
    ).toBe(false);
  });

  it("摘要重量错误 → 残留", () => {
    expect(
      isStaleCargoState(
        probe({ internalWeight: 2296, widgetTextPreview: "x 2296Lbs" }),
        expected500x2,
      ),
    ).toBe(true);
  });
});
