import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { ScopeRequiredError } from "../lib/errors.generated";
import { baseAccountingPeriod, closedPeriod } from "../testing/fixtures";
import { run } from "./listAccountingPeriods";

describe("listAccountingPeriods", () => {
  it("returns error when neither companyId nor fiscalYearId is provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, {});

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(ScopeRequiredError);
    }
    expect(mock.selects).toHaveLength(0);
  });

  it("returns all accounting periods for a company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAccountingPeriod, closedPeriod]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseAccountingPeriod, closedPeriod]);
      expect(result.value.hasNextPage).toBe(false);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].sql).toMatch(/"companyId"/);
    expect(mock.selects[0].sql).toMatch(/order by .*"startDate" asc/i);
  });

  it("returns accounting periods filtered by fiscal year", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAccountingPeriod]);

    const result = await run(mock.db, { fiscalYearId: "fiscal-year-1" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseAccountingPeriod]);
    }
    expect(mock.selects[0].parameters).toContain("fiscal-year-1");
    expect(mock.selects[0].sql).toMatch(/"fiscalYearId"/);
  });

  it("returns accounting periods filtered by status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([closedPeriod]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([closedPeriod]);
    }
    expect(mock.selects[0].parameters).toContain("CLOSED");
    expect(mock.selects[0].sql).toMatch(/"status"/);
  });

  it("returns accounting periods filtered by period type", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseAccountingPeriod]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseAccountingPeriod]);
    }
    expect(mock.selects[0].parameters).toContain("OPERATING");
    expect(mock.selects[0].sql).toMatch(/"periodType"/);
  });

  it("returns empty list when no accounting periods match", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

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