import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import {
  InvalidCostObjectTypeFilterError,
  InvalidStatusFilterError,
} from "../lib/errors.generated";
import {
  baseBudget,
  activeBudget,
  baseFiscalYear,
  baseCostCenter,
  basePlanVersion,
} from "../testing/fixtures";
import { run } from "./listBudgets";

describe("listBudgets", () => {
  it("returns all budgets when no filters are provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseBudget, activeBudget]);

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

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

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

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

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

  it("returns budgets filtered by fiscalYearId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseBudget]);

    const result = await run(db, { fiscalYearId: baseFiscalYear.id });

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

  it("returns budgets filtered by costObjectType", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseBudget]);

    const result = await run(db, { costObjectType: "CostCenter" });

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

  it("returns budgets filtered by costObjectId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseBudget]);

    const result = await run(db, { costObjectId: baseCostCenter.id });

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

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

    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].status).toEqual("ACTIVE");
    }
  });

  it("returns budgets filtered by planVersionId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseBudget]);

    const result = await run(db, { planVersionId: basePlanVersion.id });

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

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

    const result = await run(db, {
      companyId: activeBudget.companyId,
      fiscalYearId: baseFiscalYear.id,
      status: "ACTIVE",
    });

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

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

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