import { describe, expect, it } from "vitest";
import {
  buildUnmatchedTeamResolutionView,
  rankParticipantMatches,
  type ReconciliationParticipant,
} from "../unmatched-team-reconciliation";

const participants: ReconciliationParticipant[] = [
  { id: "1", name: "Los Angeles Lakers" },
  { id: "2", name: "Golden State Warriors" },
  { id: "3", name: "Boston Celtics" },
  { id: "4", name: "New York Knicks" },
];

describe("unmatched team reconciliation helpers", () => {
  it("suggests an exact normalized match first", () => {
    const result = buildUnmatchedTeamResolutionView("los angeles lakers", participants);

    expect(result.confidence).toBe("exact");
    expect(result.suggestedParticipantName).toBe("Los Angeles Lakers");
    expect(result.suggestedParticipantId).toBe("1");
  });

  it("suggests a substring match when an exact match is absent", () => {
    const result = buildUnmatchedTeamResolutionView("Golden State", participants);

    expect(result.confidence).toBe("partial");
    expect(result.suggestedParticipantName).toBe("Golden State Warriors");
    expect(result.orderedParticipants[0]?.name).toBe("Golden State Warriors");
  });

  it("leaves the picker unselected when only review-ranked matches exist", () => {
    const result = buildUnmatchedTeamResolutionView("Boston Club", participants);

    expect(result.confidence).toBe("review");
    expect(result.suggestedParticipantId).toBeNull();
    expect(result.topCandidates.length).toBeGreaterThan(0);
  });

  it("keeps top candidates ahead of the full participant list", () => {
    const ranked = rankParticipantMatches("Boston", participants);
    const result = buildUnmatchedTeamResolutionView("Boston", participants);

    expect(ranked[0]?.participantName).toBe("Boston Celtics");
    expect(result.orderedParticipants[0]?.name).toBe("Boston Celtics");
    expect(result.orderedParticipants.map((participant) => participant.id)).toEqual([
      "3",
      "1",
      "2",
      "4",
    ]);
  });
});
