import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseActiveContact } from "../testing/fixtures";
import { run } from "./getPrimaryContactPerson";

describe("getPrimaryContactPerson", () => {
  it("returns primary contact when one exists", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveContact]);

    const result = await run(mock.db, { partnerId: "partner-2" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.contactPerson).toEqual(baseActiveContact);
    }
  });

  it("returns null when no primary is set", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { partnerId: "partner-2" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.contactPerson).toBeNull();
    }
  });

  it("returns null when partner has no contact persons", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, { partnerId: "partner-99" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.contactPerson).toBeNull();
    }
  });
});
