import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { baseDraftAccount, baseActiveAccount } from "../testing/fixtures";
import { run } from "./getAccount";

describe("getAccount", () => {
  it("returns account when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseDraftAccount);

    const result = await run(db, { id: baseDraftAccount.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.account).toEqual(baseDraftAccount);
    }
  });

  it("returns null when account not found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { id: "nonexistent" });

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

  it("returns account when found by code within CoA", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(baseActiveAccount);

    const result = await run(db, { chartOfAccountsId: "coa-2", code: "1200" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.account).toEqual(baseActiveAccount);
    }
  });

  it("returns null when account not found by code within CoA", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { chartOfAccountsId: "coa-2", code: "9999" });

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