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

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

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

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

    expectErr(result, InvalidStatusTransitionError);
  });

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

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

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

  it("emits audit event recording status transition from NEVER_OPENED to FUTURE_ENTERABLE and acting user", async () => {
    const updatedPeriod = { ...neverOpenedPeriod, status: "FUTURE_ENTERABLE" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(neverOpenedPeriod);
    spies.update.mockReturnValueOnce(updatedPeriod);

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

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