import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import {
  InvalidCategoryFilterError,
  InvalidStatusFilterError,
  InvalidTypeFilterError,
} from "../lib/errors.generated";
import { baseCostElement, activeCostElement, secondaryCostElement } from "../testing/fixtures";
import { run } from "./listCostElements";

describe("listCostElements", () => {
  it("returns all cost elements when no filters are provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseCostElement, activeCostElement]);

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

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

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

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

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

  it("returns cost elements filtered by type PRIMARY", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseCostElement, activeCostElement]);

    const result = await run(db, { type: "PRIMARY" });

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

  it("returns cost elements filtered by type SECONDARY", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([secondaryCostElement]);

    const result = await run(db, { type: "SECONDARY" });

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

  it("returns cost elements filtered by category", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseCostElement]);

    const result = await run(db, { category: "overhead" });

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

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

    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(activeCostElement);
    }
  });

  it("returns cost elements matching multiple filters simultaneously", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([activeCostElement]);

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

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

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

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

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

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

  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);
  });

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