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

describe("rejectBudget", () => {
  it("rejects a SUBMITTED budget transitioning it to DRAFT status with reason", async () => {
    const rejectedBudget = { ...submittedBudget, status: "DRAFT" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(submittedBudget);
    spies.update.mockReturnValueOnce(rejectedBudget);

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

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

  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", reason: "Reason" }, commandCtx);

    expectErr(result, BudgetNotFoundError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

  it("returns error when reason text is missing", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(submittedBudget);

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

    expectErr(result, ReasonRequiredError);
  });

  it("reason is persisted to the budget", async () => {
    const rejectionReason = "Needs revision";
    const rejectedBudget = { ...submittedBudget, status: "DRAFT", rejectionReason };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(submittedBudget);
    spies.update.mockReturnValueOnce(rejectedBudget);

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

    const value = expectOk(result);
    expect(value.budget.rejectionReason).toBe(rejectionReason);
  });

  it("rejected budget can be updated and resubmitted", async () => {
    const rejectedBudget = { ...submittedBudget, status: "DRAFT" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(submittedBudget);
    spies.update.mockReturnValueOnce(rejectedBudget);

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

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

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

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

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