import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { InvalidSalespaymenttermStatusError } from "../lib/errors.generated";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { basePaymentTerm, inactivePaymentTerm } from "../testing/fixtures";
import { run } from "./deactivateSalesPaymentTerm";

describe("deactivateSalesPaymentTerm", () => {
  it("deactivates an active SalesPaymentTerm", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePaymentTerm], [inactivePaymentTerm]);

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

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

  it("throws when SalesPaymentTerm is not active", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactivePaymentTerm]);

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

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