import { describe, expect, it } from "vitest";
import type { QuoteRequest } from "@/modules/providers/quote-provider";
import {
  evaluateAddressesVisuallyComplete,
  widgetContainsConfirmedAddress,
  widgetShowsDeliverySectionCue,
} from "@/workers/rpa/address-commit";

function basePickup(overrides?: Partial<QuoteRequest["pickup"]>) {
  return {
    street: "1234 Warehouse Street",
    city: "Farmers Branch",
    state: "TX",
    zip: "75234",
    placeId: "p1",
    formattedAddress: "1234 Warehouse Street Farmers Branch TX",
    selectedFromSuggestions: true,
    mothershipOptionId: "o1",
    mothershipDisplayLabel: "1234 Warehouse Road Farmers Branch TX",
    selectedFromMothership: true,
    ...overrides,
  };
}

describe("widgetContainsConfirmedAddress", () => {
  it("匹配门牌号 + 州", () => {
    const text =
      "1234 Warehouse Road Farmers Branch, TX 5678 Distribution Drive Fort Worth, TX";
    expect(widgetContainsConfirmedAddress(text, basePickup())).toBe(true);
  });

  it("错误 street 不匹配", () => {
    const text = "999 Wrong Street Fort Worth, TX";
    expect(widgetContainsConfirmedAddress(text, basePickup())).toBe(false);
  });

  it("多词城市须完整匹配（West Seattle）", () => {
    const address = basePickup({
      street: "3131 Western Avenue",
      city: "West Seattle",
      state: "WA",
      mothershipDisplayLabel: "3131 Western AvenueWest Seattle, WA",
    });
    expect(
      widgetContainsConfirmedAddress(
        "3131 Western Avenue West Seattle, WA",
        address,
      ),
    ).toBe(true);
    expect(
      widgetContainsConfirmedAddress(
        "3131 Western Avenue, Seattle, WA, USA",
        address,
      ),
    ).toBe(false);
    expect(
      widgetContainsConfirmedAddress("3131 Western Ave Seattle, WA", address),
    ).toBe(false);
  });

  it("确认标签无门牌号时不强制 widget 含用户输入门牌号", () => {
    const address = basePickup({
      street: "5678 Distribution Way",
      city: "Farmers Branch",
      state: "TX",
      mothershipDisplayLabel: "Distribution Way, Farmers Branch, TX, USA",
    });
    const widget =
      "1234 Warehouse Street Los Angeles, CA Distribution Way Farmers Branch, TX";
    expect(widgetContainsConfirmedAddress(widget, address)).toBe(true);
  });

  it("chip 折叠仅 City, ST 无门牌行不算 widgetContainsConfirmedAddress", () => {
    const address = basePickup({
      street: "1234 Warehouse Street",
      city: "Los Angeles",
      state: "California",
      mothershipDisplayLabel:
        "1234 Warehouse Street, Los Angeles, California, USA",
    });
    const widget =
      "Los Angeles, CA Where to? Pick up from Deliver to Freight details";
    expect(widgetContainsConfirmedAddress(widget, address)).toBe(false);
  });
});

describe("widgetShowsDeliverySectionCue", () => {
  it("识别 Where to? 派送标签", () => {
    expect(
      widgetShowsDeliverySectionCue("Los Angeles, CA Where to? Pick up from"),
    ).toBe(true);
    expect(widgetShowsDeliverySectionCue("Deliver to Search")).toBe(true);
  });
});

describe("evaluateAddressesVisuallyComplete", () => {
  it("截图态：双州码 + Freight details 视为地址完成", () => {
    const text =
      "Seattle, WA 293 East Pike Street Seattle, WA Pelican Hotel Miami Beach, FL Freight details Get a freight quote";
    expect(evaluateAddressesVisuallyComplete(text)).toBe(true);
  });

  it("仅单地址不算完成", () => {
    expect(evaluateAddressesVisuallyComplete("293 East Pike Street Seattle, WA")).toBe(
      false,
    );
  });
});
