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

describe("listPartnerBankAccountsByPartner", () => {
  it("returns all bank accounts for a partner with multiple accounts", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseDefaultBankAccount, baseSecondBankAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseDefaultBankAccount, baseSecondBankAccount]);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("returns empty array when partner has no bank accounts", 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.items).toEqual([]);
      expect(result.value.hasNextPage).toBe(false);
    }
  });

  it("includes both default and non-default accounts in results", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseDefaultBankAccount, baseSecondBankAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      const defaults = result.value.items.map((a) => a.isDefault);
      expect(defaults).toContain(true);
      expect(defaults).toContain(false);
    }
  });

  it("returns accounts regardless of partner lifecycle status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseDefaultBankAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseDefaultBankAccount]);
    }
  });
});
