import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { InternalOrderNotFoundError, InvalidStateError } from "../lib/errors.generated";
import {
  baseInternalOrder,
  releasedInternalOrder,
  techCompletedInternalOrder,
  closedInternalOrder,
} from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deleteInternalOrder";

describe("deleteInternalOrder", () => {
  it("deletes a CREATED internal order successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseInternalOrder);

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

    const value = expectOk(result);
    expect(value.internalOrder).toEqual(baseInternalOrder);
    expect(spies.delete).toHaveBeenCalled();
  });

  it("deletes a CREATED internal order and removes associated budget assignment", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseInternalOrder);

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

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

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

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

    expectErr(result, InternalOrderNotFoundError);
  });

  it("returns error when internal order is in RELEASED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(releasedInternalOrder);

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

    expectErr(result, InvalidStateError);
  });

  it("returns error when internal order is in TECHNICALLY_COMPLETED status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(techCompletedInternalOrder);

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

    expectErr(result, InvalidStateError);
  });

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

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

    expectErr(result, InvalidStateError);
  });

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

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

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