import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  PurchaseRequisitionNotFoundError,
  PurchaseRequisitionNotSubmittedError,
  RejectionReasonRequiredError,
} from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { basePurchaseRequisition, submittedPurchaseRequisition } from "../testing/fixtures";
import { run } from "./rejectPurchaseRequisition";

describe("rejectPurchaseRequisition", () => {
  it("rejects a submitted purchase requisition back to draft", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [submittedPurchaseRequisition],
      [
        {
          ...submittedPurchaseRequisition,
          status: "DRAFT",
          rejectionReason: "Budget exceeded",
        },
      ],
    );

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

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

  it("records the rejection reason for audit", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [submittedPurchaseRequisition],
      [
        {
          ...submittedPurchaseRequisition,
          status: "DRAFT",
          rejectionReason: "Not justified",
        },
      ],
    );

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

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

  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", reason: "No match" }, commandCtx),
    );

    expectErr(result, PurchaseRequisitionNotFoundError);
  });

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

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

    expectErr(result, PurchaseRequisitionNotSubmittedError);
  });

  it("returns error when rejection reason is missing", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([submittedPurchaseRequisition]);

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

    expectErr(result, RejectionReasonRequiredError);
  });
});
