import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  AccountingPeriodNotFoundError,
  InvalidStatusTransitionError,
} from "../lib/errors.generated";
import {
  neverOpenedPeriod,
  futureEnterablePeriod,
  baseAccountingPeriod,
  closedPeriod,
  permanentlyClosedPeriod,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./permanentlyClosePeriod";

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

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

    expectErr(result, AccountingPeriodNotFoundError);
  });

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

  it("transitions CLOSED period to PERMANENTLY_CLOSED", async () => {
    const updatedPeriod = { ...closedPeriod, status: "PERMANENTLY_CLOSED" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(closedPeriod);
    spies.update.mockReturnValueOnce(updatedPeriod);

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

    const value = expectOk(result);
    expect(value.accountingPeriod.status).toBe("PERMANENTLY_CLOSED");
  });

  it("emits audit event recording the irreversible transition and acting user", async () => {
    const updatedPeriod = { ...closedPeriod, status: "PERMANENTLY_CLOSED" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(closedPeriod);
    spies.update.mockReturnValueOnce(updatedPeriod);

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

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