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

describe("listFinancialStatements", () => {
  it("returns all financial statements for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFinancialStatement, finalizedFinancialStatement]);

    const result = await run(db, { companyId: "company-1" });

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

  it("returns financial statements filtered by statement type", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFinancialStatement]);

    const result = await run(db, {
      companyId: "company-1",
      statementType: "BALANCE_SHEET",
    });

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

  it("returns financial statements filtered by status", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([finalizedFinancialStatement]);

    const result = await run(db, { companyId: "company-1", status: "FINALIZED" });

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

  it("returns financial statements filtered by dateRange", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFinancialStatement]);

    const result = await run(db, {
      companyId: "company-1",
      startDate: new Date("2024-01-01"),
      endDate: new Date("2024-01-31"),
    });

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

  it("returns empty list when no financial statements match", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([]);

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

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