import { describe, expect, it, vi, beforeEach } from "vitest";

vi.mock("@/lib/axel/candidates", () => ({
  resolveAxelMothershipCandidates: vi.fn(),
}));

vi.mock("@/modules/address/candidates-cache", () => ({
  loadAddressCandidatesCache: vi.fn(),
  saveAddressCandidatesCache: vi.fn(),
}));

import { resolveMothershipCandidates } from "@/modules/address/mothership-candidates";
import { resolveAxelMothershipCandidates } from "@/lib/axel/candidates";
import {
  loadAddressCandidatesCache,
  saveAddressCandidatesCache,
} from "@/modules/address/candidates-cache";

describe("resolveMothershipCandidates", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(loadAddressCandidatesCache).mockResolvedValue(null);
    vi.mocked(saveAddressCandidatesCache).mockResolvedValue(undefined);
  });
  const input = {
    pickup: {
      street: "5678 Distribution Dr",
      city: "Dallas",
      state: "TX",
      zip: "75201",
    },
    delivery: {
      street: "1234 Warehouse Blvd",
      city: "Los Angeles",
      state: "CA",
      zip: "90001",
    },
  };

  it("非 mock 模式统一走 axel search", async () => {
    process.env.RPA_MOCK_MODE = "false";
    vi.mocked(resolveAxelMothershipCandidates).mockResolvedValue({
      pickup_candidates: [
        {
          option_id: "ChIJpickup",
          display_label: "5678 Distribution Dr, Dallas, TX 75201",
          formatted_address: "5678 Distribution Dr, Dallas, TX 75201",
          street: "5678 Distribution Dr",
          city: "Dallas",
          state: "TX",
          zip: "75201",
        },
      ],
      delivery_candidates: [
        {
          option_id: "ChIJdelivery",
          display_label: "1234 Warehouse Blvd, Los Angeles, CA 90001",
          formatted_address: "1234 Warehouse Blvd, Los Angeles, CA 90001",
          street: "1234 Warehouse Blvd",
          city: "Los Angeles",
          state: "CA",
          zip: "90001",
        },
      ],
    });

    const result = await resolveMothershipCandidates(input);
    expect(resolveAxelMothershipCandidates).toHaveBeenCalledWith(input);
    expect(saveAddressCandidatesCache).toHaveBeenCalled();
    expect(result.pickup_candidates[0].option_id).toBe("ChIJpickup");
  });

  it("命中缓存时不调用 axel", async () => {
    process.env.RPA_MOCK_MODE = "false";
    vi.mocked(loadAddressCandidatesCache).mockResolvedValue({
      pickup_candidates: [
        {
          option_id: "ChIJcached-pickup",
          display_label: "cached pickup",
          formatted_address: "cached pickup",
          street: "5678 Distribution Dr",
          city: "Dallas",
          state: "TX",
          zip: "75201",
        },
      ],
      delivery_candidates: [
        {
          option_id: "ChIJcached-delivery",
          display_label: "cached delivery",
          formatted_address: "cached delivery",
          street: "1234 Warehouse Blvd",
          city: "Los Angeles",
          state: "CA",
          zip: "90001",
        },
      ],
    });

    const result = await resolveMothershipCandidates(input);
    expect(resolveAxelMothershipCandidates).not.toHaveBeenCalled();
    expect(saveAddressCandidatesCache).not.toHaveBeenCalled();
    expect(result.pickup_candidates[0].option_id).toBe("ChIJcached-pickup");
  });
});
