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

describe("updateAllocationCycle", () => {
  it("updates name on a DRAFT allocation cycle", async () => {
    const updated = { ...baseAllocationCycle, name: "Updated Name" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);
    spies.update.mockReturnValueOnce(updated);

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

    const value = expectOk(result);
    expect(value.allocationCycle.name).toBe("Updated Name");
  });

  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", name: "Updated Name" },
      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, name: "Updated Name" },
      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, name: "Updated Name" },
      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, name: "Updated Name" },
      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, name: "Updated Name" },
      commandCtx,
    );

    expectErr(result, InvalidStateError);
  });

  it("emits audit event recording previous and new field values", async () => {
    const updated = { ...baseAllocationCycle, name: "Updated Name" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseAllocationCycle);
    spies.update.mockReturnValueOnce(updated);

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

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