import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { AllocationCycleNotFoundError, InvalidStateError } from "../lib/errors.generated";
import {
  baseAllocationCycle,
  validatedAllocationCycle,
  executedAllocationCycle,
  postedAllocationCycle,
  reversedAllocationCycle,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deleteAllocationCycle";

describe("deleteAllocationCycle", () => {
  it("deletes a DRAFT allocation cycle successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);

    const result = await run(db, { allocationCycleId: baseAllocationCycle.id }, commandCtx);

    const value = expectOk(result);
    expect(value.allocationCycleId).toBe(baseAllocationCycle.id);
  });

  it("deletes a DRAFT allocation cycle and removes associated rules", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);

    const result = await run(db, { allocationCycleId: baseAllocationCycle.id }, commandCtx);

    expectOk(result);
    expect(spies.delete).toHaveBeenCalled();
  });

  it("returns error when allocation cycle does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, { allocationCycleId: "missing-id" }, commandCtx);

    expectErr(result, AllocationCycleNotFoundError);
  });

  it("returns error when allocation cycle is in VALIDATED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(validatedAllocationCycle);

    const result = await run(db, { allocationCycleId: validatedAllocationCycle.id }, commandCtx);

    expectErr(result, InvalidStateError);
  });

  it("returns error when allocation cycle is in EXECUTED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(executedAllocationCycle);

    const result = await run(db, { allocationCycleId: executedAllocationCycle.id }, commandCtx);

    expectErr(result, InvalidStateError);
  });

  it("returns error when allocation cycle is in POSTED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(postedAllocationCycle);

    const result = await run(db, { allocationCycleId: postedAllocationCycle.id }, commandCtx);

    expectErr(result, InvalidStateError);
  });

  it("returns error when allocation cycle is in REVERSED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(reversedAllocationCycle);

    const result = await run(db, { allocationCycleId: reversedAllocationCycle.id }, commandCtx);

    expectErr(result, InvalidStateError);
  });

  it("emits audit event recording the deletion and acting user", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);

    const result = await run(db, { allocationCycleId: baseAllocationCycle.id }, commandCtx);

    expectOk(result);
    expect(spies.delete).toHaveBeenCalled();
  });
});
