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

describe("deactivateSalesPriceRule", () => {
  it("deactivates an active SalesPriceRule", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [basePriceRule],
      [{ ...basePriceRule, status: "INACTIVE", updatedAt: new Date() }],
    );

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

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

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

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

    expectErr(result, InvalidSalespriceruleStatusError);
  });
});
