import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { InvalidStatusFilterError } from "../lib/errors.generated";
import { baseProfitCenter, activeProfitCenter } from "../testing/fixtures";
import { run } from "./listProfitCenters";

describe("listProfitCenters", () => {
  it("returns all profit centers when no filters are provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseProfitCenter, activeProfitCenter]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(2);
    }
  });

  it("returns profit centers filtered by companyId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseProfitCenter]);

    const result = await run(db, { companyId: baseProfitCenter.companyId });

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

  it("returns profit centers filtered by status ACTIVE", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([activeProfitCenter]);

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

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

  it("returns profit centers filtered by status DRAFT", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseProfitCenter]);

    const result = await run(db, { status: "DRAFT" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.items).toHaveLength(1);
      expect(result.value.items[0].status).toEqual("DRAFT");
    }
  });

  it("returns profit centers matching companyId and status filters simultaneously", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([activeProfitCenter]);

    const result = await run(db, {
      companyId: baseProfitCenter.companyId,
      status: "ACTIVE",
    });

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

  it("returns an empty list when no profit centers match the given filters", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([]);

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

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

  it("returns error when status filter is invalid", async () => {
    const { db } = createMockDb<DB>();
    const result = await run(db, { status: "INVALID" });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toBeInstanceOf(InvalidStatusFilterError);
  });
});
