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 "./invalidateAllocationCycle";

describe("invalidateAllocationCycle", () => {
  it("invalidates a VALIDATED allocation cycle transitioning it to DRAFT status", async () => {
    const draft = { ...validatedAllocationCycle, status: "DRAFT" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(validatedAllocationCycle);
    spies.update.mockReturnValueOnce(draft);

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

    const value = expectOk(result);
    expect(value.allocationCycle.status).toBe("DRAFT");
  });

  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 DRAFT status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);

    const result = await run(db, { allocationCycleId: baseAllocationCycle.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 status transition and acting user", async () => {
    const draft = { ...validatedAllocationCycle, status: "DRAFT" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(validatedAllocationCycle);
    spies.update.mockReturnValueOnce(draft);

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

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