import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  PeriodCloseNotFoundError,
  InvalidStatusTransitionError,
  PeriodPermanentlyClosedError,
} from "../lib/errors.generated";
import {
  softLockedPeriodClose,
  inProgressPeriodClose,
  underReviewPeriodClose,
  permanentlyClosedPeriodClose,
  basePeriodClose,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./revertSoftLock";

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

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

    expectErr(result, PeriodCloseNotFoundError);
  });

  it("returns error when PeriodClose is in NOT_STARTED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePeriodClose);

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

    expectErr(result, InvalidStatusTransitionError);
  });

  it("returns error when PeriodClose is in IN_PROGRESS status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(inProgressPeriodClose);

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

    expectErr(result, InvalidStatusTransitionError);
  });

  it("returns error when PeriodClose is in UNDER_REVIEW status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(underReviewPeriodClose);

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

    expectErr(result, InvalidStatusTransitionError);
  });

  it("returns error when PeriodClose is PERMANENTLY_CLOSED", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(permanentlyClosedPeriodClose);

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

    expectErr(result, PeriodPermanentlyClosedError);
  });

  it("transitions SOFT_LOCKED period close to IN_PROGRESS", async () => {
    const updatedPeriodClose = { ...softLockedPeriodClose, status: "IN_PROGRESS" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(softLockedPeriodClose);
    spies.update.mockReturnValueOnce(updatedPeriodClose);

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

    const value = expectOk(result);
    expect(value.periodClose.status).toBe("IN_PROGRESS");
  });

  it("emits audit event recording status transition and acting user", async () => {
    const updatedPeriodClose = { ...softLockedPeriodClose, status: "IN_PROGRESS" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(softLockedPeriodClose);
    spies.update.mockReturnValueOnce(updatedPeriodClose);

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

    const value = expectOk(result);
    expect(value.auditTrail).toEqual([
      {
        type: "STATUS_TRANSITION",
        from: "SOFT_LOCKED",
        to: "IN_PROGRESS",
        actorId: commandCtx.actorId,
      },
    ]);
    expect(spies.update).toHaveBeenCalled();
  });
});
