import { describe, expect, it } from "vitest";
import {
  evaluateAddressesLockedForSubmit,
  evaluateCargoCommitted,
  type CargoCommitProbe,
} from "@/workers/rpa/cargo-lock";

describe("cargo commit barrier", () => {
  const committed: CargoCommitProbe = {
    hasSummaryChip: true,
    visibleEditableCargoInputs: 0,
    editorOpen: false,
    internalPalletCount: 2,
    internalWeight: 227,
    widgetTextPreview: "2 托盘s",
  };

  it("summary + 无 editable input + internal pallet → committed", () => {
    expect(evaluateCargoCommitted(committed, 2)).toBe(true);
  });

  it("内联步输入仍可见但摘要完整 → committed", () => {
    expect(
      evaluateCargoCommitted(
        {
          hasSummaryChip: true,
          visibleEditableCargoInputs: 5,
          editorOpen: true,
          internalPalletCount: 2,
          internalWeight: 226,
          widgetTextPreview:
            "2 托盘s 40W, x 48L, x 48H, x 226Lbs 货运详情",
        },
        2,
      ),
    ).toBe(true);
  });

  it("摘要不完整且仍有 editable → 未 committed", () => {
    expect(
      evaluateCargoCommitted(
        {
          hasSummaryChip: false,
          visibleEditableCargoInputs: 2,
          editorOpen: true,
          internalPalletCount: null,
          internalWeight: null,
          widgetTextPreview: "货运详情",
        },
        2,
      ),
    ).toBe(false);
  });

  it("摘要含托盘+尺寸但无重量 → 未 committed", () => {
    expect(
      evaluateCargoCommitted(
        {
          hasSummaryChip: true,
          visibleEditableCargoInputs: 0,
          editorOpen: true,
          internalPalletCount: 2,
          internalWeight: null,
          widgetTextPreview: "2 托盘s 40W, x 48L, x 48H, x -Lbs",
        },
        2,
      ),
    ).toBe(false);
  });

  it("摘要含 226Lbs 无空格 → committed", () => {
    expect(
      evaluateCargoCommitted(
        {
          hasSummaryChip: true,
          visibleEditableCargoInputs: 0,
          editorOpen: false,
          internalPalletCount: 2,
          internalWeight: 226,
          widgetTextPreview: "2 托盘s 40W, x 48L, x 48H, x 226Lbs",
        },
        2,
      ),
    ).toBe(true);
  });

  it("editor 打开且仍有 editable → 未 committed", () => {
    expect(
      evaluateCargoCommitted(
        { ...committed, editorOpen: true, visibleEditableCargoInputs: 2 },
        2,
      ),
    ).toBe(false);
  });

  it("摘要重量与请求单托不符 → 未 committed", () => {
    expect(
      evaluateCargoCommitted(
        {
          hasSummaryChip: true,
          visibleEditableCargoInputs: 0,
          editorOpen: false,
          internalPalletCount: 2,
          internalWeight: 2296,
          widgetTextPreview: "2 托盘s 40W, x 48L, x 48H, x 2296Lbs",
        },
        {
          palletCount: 2,
          weight: {
            displayValue: "500",
            unit: "lb",
            weightLbPerPallet: 500,
            palletCount: 2,
          },
        },
      ),
    ).toBe(false);
  });
});

describe("address lock barrier", () => {
  it("双州码 → locked", () => {
    expect(
      evaluateAddressesLockedForSubmit(
        "1234 Warehouse St, Los Angeles, CA 90021 → 5678 Distribution Way, Dallas, TX 75234",
        false,
      ),
    ).toBe(true);
  });

  it("单州码 → 未 locked", () => {
    expect(
      evaluateAddressesLockedForSubmit("Los Angeles, CA 90021 货运详情", false),
    ).toBe(false);
  });

  it("地址搜索仍可编辑 → 未 locked", () => {
    expect(
      evaluateAddressesLockedForSubmit(
        "1234 Warehouse St, CA 90021 5678 Distribution Way, TX 75234",
        true,
      ),
    ).toBe(false);
  });
});
