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 {
  baseBudget,
  submittedBudget,
  approvedBudget,
  activeBudget,
  closedBudget,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./updateBudget";

describe("updateBudget", () => {
  it("updates name on a DRAFT budget", async () => {
    const updatedBudget = { ...baseBudget, name: "Updated Name" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);
    spies.update.mockReturnValueOnce(updatedBudget);

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

    const value = expectOk(result);
    expect(value.budget.name).toBe("Updated Name");
  });

  it("updates description on a DRAFT budget", async () => {
    const updatedBudget = { ...baseBudget, description: "New description" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);
    spies.update.mockReturnValueOnce(updatedBudget);

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

    const value = expectOk(result);
    expect(value.budget.description).toBe("New description");
  });

  it("updates budget holder on a DRAFT budget", async () => {
    const updatedBudget = { ...baseBudget, budgetHolderId: "user-2" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);
    spies.update.mockReturnValueOnce(updatedBudget);

    const result = await run(db, { budgetId: baseBudget.id, budgetHolderId: "user-2" }, commandCtx);

    const value = expectOk(result);
    expect(value.budget.budgetHolderId).toBe("user-2");
  });

  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 in SUBMITTED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(submittedBudget);

    const result = await run(db, { budgetId: submittedBudget.id, name: "X" }, 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, name: "X" }, commandCtx);

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

  it("emits audit event recording previous and new field values", async () => {
    const updatedBudget = { ...baseBudget, name: "Audited" };
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);
    spies.update.mockReturnValueOnce(updatedBudget);

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

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