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

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

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

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

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

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

    expectErr(result, InvalidSalespricelistStatusError);
  });
});
