import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { BudgetNotFoundError, InvalidStateError } from "../lib/errors.generated";
import {
  activeBudget,
  baseBudget,
  submittedBudget,
  approvedBudget,
  closedBudget,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./closeBudget";

describe("closeBudget", () => {
  it("closes an ACTIVE budget transitioning it to CLOSED status", async () => {
    const closedResult = { ...activeBudget, status: "CLOSED" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(activeBudget);
    spies.update.mockReturnValueOnce(closedResult);

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

    const value = expectOk(result);
    expect(value.budget.status).toBe("CLOSED");
  });

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

    const result = await run(db, { budgetId: "missing" }, commandCtx);

    expectErr(result, BudgetNotFoundError);
  });

  it("returns error when budget is not in ACTIVE status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(approvedBudget);

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

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