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

describe("listAccounts", () => {
  it("returns all accounts in a CoA when no filters applied", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDraftAccount, baseActiveAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2" });

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

  it("returns accounts filtered by account type", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDraftAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2", accountType: "ASSET" });

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

  it("returns accounts filtered by classification", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActiveAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2", classification: "RECEIVABLE" });

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

  it("returns accounts filtered by account group", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActiveAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2", accountGroupId: "group-2" });

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

  it("returns accounts filtered by tags (matches accounts having any of the provided tags)", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActiveAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2", tags: ["tag1"] });

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

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

    const result = await run(db, { chartOfAccountsId: "coa-2", status: "ACTIVE" });

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

  it("returns accounts filtered by currency restriction", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([]);

    const result = await run(db, {
      chartOfAccountsId: "coa-2",
      currencyRestriction: "currency-usd",
    });

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

  it("returns accounts filtered by multiple criteria combined", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseActiveAccount]);

    const result = await run(db, {
      chartOfAccountsId: "coa-2",
      accountType: "ASSET",
      status: "ACTIVE",
    });

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

  it("returns accounts sorted by code ascending", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValue([baseDraftAccount, baseActiveAccount]);

    const result = await run(db, { chartOfAccountsId: "coa-2" });

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

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

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

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