import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  PurchaseRequisitionNotCancellableError,
  PurchaseRequisitionNotFoundError,
} from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import {
  approvedPurchaseRequisition,
  basePurchaseRequisition,
  cancelledPurchaseRequisition,
  closedPurchaseRequisition,
  convertedPurchaseRequisition,
  submittedPurchaseRequisition,
} from "../testing/fixtures";
import { run } from "./cancelPurchaseRequisition";

describe("cancelPurchaseRequisition", () => {
  it("cancels a draft purchase requisition", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const cancelled = { ...basePurchaseRequisition, status: "CANCELLED" };
    spies.select.mockReturnValueOnce(basePurchaseRequisition); // selectOne PurchaseRequisition
    spies.update.mockReturnValueOnce(cancelled);

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

    expect(expectOk(result).purchaseRequisition.status).toBe("CANCELLED");
    expect(spies.set).toHaveBeenCalledWith(expect.objectContaining({ status: "CANCELLED" }));
  });

  it("cancels a submitted purchase requisition", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const cancelled = { ...submittedPurchaseRequisition, status: "CANCELLED" };
    spies.select.mockReturnValueOnce(submittedPurchaseRequisition); // selectOne PurchaseRequisition
    spies.update.mockReturnValueOnce(cancelled);

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

    expect(expectOk(result).purchaseRequisition.status).toBe("CANCELLED");
  });

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

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

    expectErr(result, PurchaseRequisitionNotFoundError);
  });

  it("returns error when purchase requisition is approved", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(approvedPurchaseRequisition);

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

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is already converted", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(convertedPurchaseRequisition);

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

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is closed", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(closedPurchaseRequisition);

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

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is already cancelled", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(cancelledPurchaseRequisition);

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

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });
});
