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 { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import {
  neverOpenedPeriod,
  futureEnterablePeriod,
  baseAccountingPeriod,
  closedPeriod,
  permanentlyClosedPeriod,
} from "../testing/fixtures";
import { run } from "./openPeriod";

describe("openPeriod", () => {
  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 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 directly to OPEN", async () => {
    const updatedPeriod = { ...neverOpenedPeriod, status: "OPEN" };
    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("OPEN");
  });

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

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

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

  it("emits audit event recording status transition and acting user", async () => {
    const updatedPeriod = { ...neverOpenedPeriod, status: "OPEN" };
    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();
  });
});
