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

describe("listFiscalYears", () => {
  it("returns all fiscal years for a company", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFiscalYear, baseFiscalYear2]);

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

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

  it("returns fiscal years filtered by startDate", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFiscalYear2]);

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

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

  it("returns fiscal years filtered by endDate", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseFiscalYear]);

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

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

  it("returns empty list when no fiscal years 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);
    }
  });
});
