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

describe("deleteBudgetLineItem", () => {
  it("deletes a line item from a DRAFT budget", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseBudgetLineItem).mockReturnValueOnce(baseBudget);

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

    const value = expectOk(result);
    expect(value.budgetLineItemId).toBe(baseBudgetLineItem.id);
  });

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

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

    expectErr(result, BudgetLineItemNotFoundError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

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