import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    const cancelled = { ...basePurchaseRequisition, status: "CANCELLED" };
    mock.enqueueResults([basePurchaseRequisition], [cancelled]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: basePurchaseRequisition.id }, commandCtx),
    );

    expect(expectOk(result).purchaseRequisition.status).toBe("CANCELLED");
    expect(mock.updates[0].updateValues()).toEqual(
      expect.objectContaining({ status: "CANCELLED" }),
    );
  });

  it("cancels a submitted purchase requisition", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const cancelled = { ...submittedPurchaseRequisition, status: "CANCELLED" };
    mock.enqueueResults([submittedPurchaseRequisition], [cancelled]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: submittedPurchaseRequisition.id }, commandCtx),
    );

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

  it("returns error when purchase requisition does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { id: "missing-requisition" }, commandCtx));

    expectErr(result, PurchaseRequisitionNotFoundError);
  });

  it("returns error when purchase requisition is approved", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([approvedPurchaseRequisition]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: approvedPurchaseRequisition.id }, commandCtx),
    );

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is already converted", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([convertedPurchaseRequisition]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: convertedPurchaseRequisition.id }, commandCtx),
    );

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is closed", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([closedPurchaseRequisition]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: closedPurchaseRequisition.id }, commandCtx),
    );

    expectErr(result, PurchaseRequisitionNotCancellableError);
  });

  it("returns error when purchase requisition is already cancelled", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([cancelledPurchaseRequisition]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: cancelledPurchaseRequisition.id }, commandCtx),
    );

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