import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { InvalidSalespaymenttermStatusError } from "../lib/errors.generated";
import { basePaymentTerm, inactivePaymentTerm } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deactivateSalesPaymentTerm";

describe("deactivateSalesPaymentTerm", () => {
  it("deactivates an active SalesPaymentTerm", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePaymentTerm);
    spies.update.mockReturnValueOnce(inactivePaymentTerm);

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

    const value = expectOk(result);
    expect(value.salesPaymentTerm.status).toBe("INACTIVE");
  });

  it("throws when SalesPaymentTerm is not active", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(inactivePaymentTerm);

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

    expectErr(result, InvalidSalespaymenttermStatusError);
  });
});
