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

describe("listAccounts", () => {
  it("returns all company accounts when no filters applied", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount, baseInactiveAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) expect(result.value.items).toEqual([baseActiveAccount, baseInactiveAccount]);
    expect(mock.selects[0].parameters).toContain("company-1");
    expect(mock.selects[0].sql).toMatch(/"companyId"/);
  });

  it("returns accounts filtered by account type", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) expect(result.value.items).toEqual([baseActiveAccount]);
    expect(mock.selects[0].parameters).toContain("ASSET");
    expect(mock.selects[0].sql).toMatch(/"accountType"/);
  });

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

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

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

  it("returns accounts filtered by account ids", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

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

    expect(result.ok).toBe(true);
    if (result.ok) expect(result.value.items).toEqual([baseActiveAccount]);
    expect(mock.selects[0].parameters).toContain(baseActiveAccount.id);
    expect(mock.selects[0].sql).toMatch(/"id" in/i);
  });

  it("returns an empty page when account ids filter is empty", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

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

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

  it("orders accounts by code ascending by default", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

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

    expect(result.ok).toBe(true);
    expect(mock.selects[0].sql).toMatch(/order by .*"code" asc/i);
  });
});
