import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } 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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDefaultBankAccount, baseSecondBankAccount]);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([]);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDefaultBankAccount, baseSecondBankAccount]);

    const result = await run(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 { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDefaultBankAccount]);

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

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