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

describe("closePurchaseRequisition", () => {
  it("closes an approved purchase requisition", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const closed = { ...approvedPurchaseRequisition, status: "CLOSED" };
    spies.select.mockReturnValueOnce(approvedPurchaseRequisition); // selectOne PurchaseRequisition
    spies.update.mockReturnValueOnce(closed);

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

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

  it("closes an approved requisition with a close reason", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const closed = {
      ...approvedPurchaseRequisition,
      status: "CLOSED",
      closeReason: "No longer needed",
    };
    spies.select.mockReturnValueOnce(approvedPurchaseRequisition); // selectOne PurchaseRequisition
    spies.update.mockReturnValueOnce(closed);

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

    expect(expectOk(result).purchaseRequisition.status).toBe("CLOSED");
    expect(spies.set).toHaveBeenCalledWith(
      expect.objectContaining({ closeReason: "No longer needed" }),
    );
  });

  it("closes an approved requisition without a close reason", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const closed = { ...approvedPurchaseRequisition, status: "CLOSED", closeReason: null };
    spies.select.mockReturnValueOnce(approvedPurchaseRequisition); // selectOne PurchaseRequisition
    spies.update.mockReturnValueOnce(closed);

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

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

  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 not in approved status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce({ ...approvedPurchaseRequisition, status: "CANCELLED" });

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

    expectErr(result, PurchaseRequisitionNotClosableError);
  });

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

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

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

    expectErr(result, PurchaseRequisitionNotClosableError);
  });

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

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

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