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

describe("getFinancialStatement", () => {
  it("returns financial statement with line items when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseFinancialStatement)
      .mockReturnValueOnce([baseFinancialStatementLineItem]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.financialStatement).toEqual(baseFinancialStatement);
      expect(result.value.lineItems).toEqual([baseFinancialStatementLineItem]);
    }
  });

  it("returns null with empty line items when not found", 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.financialStatement).toBeNull();
      expect(result.value.lineItems).toEqual([]);
    }
  });
});
