import type { MothershipAddressCandidate } from "@/lib/frontend/types";

export type ProofAddressInput = {
  street: string;
  city: string;
  state: string;
  zip?: string;
};

const STATE_ZIP: Record<string, string> = {
  CA: "90001",
  TX: "75234",
  FL: "33101",
  WA: "98101",
  CO: "80205",
  NC: "28202",
};

/** 链路探通：不调用 MotherShip 联想 API，直接合成已确认候选 */
export function buildSyntheticCandidates(
  pairId: string,
  pickup: ProofAddressInput,
  delivery: ProofAddressInput,
): {
  pickup: MothershipAddressCandidate;
  delivery: MothershipAddressCandidate;
} {
  const mk = (
    side: "pickup" | "delivery",
    addr: ProofAddressInput,
  ): MothershipAddressCandidate => {
    const zip = addr.zip?.trim() || STATE_ZIP[addr.state] || "10001";
    const display = `${addr.street} ${addr.city} ${addr.state}`.replace(
      /\s+/g,
      " ",
    );
    return {
      option_id: `proof-syn-${pairId}-${side}`,
      display_label: display,
      formatted_address: `${addr.street}, ${addr.city}, ${addr.state} ${zip}, USA`,
      street: addr.street,
      city: addr.city,
      state: addr.state,
      zip,
    };
  };

  return {
    pickup: mk("pickup", pickup),
    delivery: mk("delivery", delivery),
  };
}
