import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseJournalEntry, postedJournalEntry } from "../testing/fixtures";
import { run } from "./listJournalEntries";

describe("listJournalEntries", () => {
  it("returns all journal entries for a company", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseJournalEntry, postedJournalEntry]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toEqual([baseJournalEntry, postedJournalEntry]);
      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 .*"entryDate" desc/i);
  });

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

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

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

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

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

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

  it("returns journal entries filtered by date range", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseJournalEntry]);

    const result = await run(mock.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([baseJournalEntry]);
    }
    expect(mock.selects[0].sql).toMatch(/"entryDate"/);
  });

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