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

describe("closePurchaseRequisition", () => {
  it("closes an approved purchase requisition", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const closed = { ...approvedPurchaseRequisition, status: "CLOSED" };
    mock.enqueueResults([approvedPurchaseRequisition], [closed]);

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

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

  it("closes an approved requisition with a close reason", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const closed = {
      ...approvedPurchaseRequisition,
      status: "CLOSED",
      closeReason: "No longer needed",
    };
    mock.enqueueResults([approvedPurchaseRequisition], [closed]);

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

    expect(expectOk(result).purchaseRequisition.status).toBe("CLOSED");
    expect(mock.updates[0].updateValues()).toEqual(
      expect.objectContaining({ closeReason: "No longer needed" }),
    );
  });

  it("closes an approved requisition without a close reason", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const closed = { ...approvedPurchaseRequisition, status: "CLOSED", closeReason: null };
    mock.enqueueResults([approvedPurchaseRequisition], [closed]);

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

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

  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 not in approved status", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([{ ...approvedPurchaseRequisition, status: "CANCELLED" }]);

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

    expectErr(result, PurchaseRequisitionNotClosableError);
  });

  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, PurchaseRequisitionNotClosableError);
  });

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

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

    expectErr(result, PurchaseRequisitionNotClosableError);
  });

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

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

    expectErr(result, PurchaseRequisitionNotClosableError);
  });
});
