import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import {
  AccountingPeriodNotFoundError,
  InvalidStatusFilterError,
  InvalidTypeFilterError,
} from "../lib/errors.generated";
import {
  baseAllocationCycle,
  postedAllocationCycle,
  baseAccountingPeriod,
} from "../testing/fixtures";
import { run } from "./listAllocationCycles";

const distributionAllocationCycle = {
  ...baseAllocationCycle,
  id: "allocation-cycle-distribution",
  allocationType: "DISTRIBUTION" as const,
};

describe("listAllocationCycles", () => {
  it("returns all allocation cycles when no filters are provided", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseAllocationCycle, postedAllocationCycle]);

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

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

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

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

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

  it("returns allocation cycles filtered by type ASSESSMENT", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([baseAllocationCycle]);

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

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

  it("returns allocation cycles filtered by type DISTRIBUTION", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([distributionAllocationCycle]);

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

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

  it("returns allocation cycles filtered by status POSTED", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce([postedAllocationCycle]);

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

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

  it("returns allocation cycles filtered by accountingPeriodId", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce({ id: baseAccountingPeriod.id })
      .mockReturnValueOnce([baseAllocationCycle]);

    const result = await run(db, { accountingPeriodId: baseAccountingPeriod.id });

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

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

    const result = await run(db, {
      companyId: postedAllocationCycle.companyId,
      type: "ASSESSMENT",
      status: "POSTED",
    });

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

  it("returns an empty list when no allocation cycles 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 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 accountingPeriodId does not exist", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);
    const result = await run(db, { accountingPeriodId: "missing-period" });
    expect(result.ok).toBe(false);
    if (!result.ok) expect(result.error).toBeInstanceOf(AccountingPeriodNotFoundError);
  });
});
