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

describe("deleteBudget", () => {
  it("deletes a DRAFT budget successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);

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

    const value = expectOk(result);
    expect(value.budgetId).toBe(baseBudget.id);
  });

  it("deletes a DRAFT budget and removes all associated line items", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);

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

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

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

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

    expectErr(result, InvalidStateError);
  });

  it("emits audit event recording the deletion and acting user", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudget);

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

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