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

describe("activatePurchasePriceList", () => {
  it("activates an inactive price list", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactivePriceList], [{ ...inactivePriceList, status: "ACTIVE" }]);

    const result = await mock.withTx((trx) => run(trx, { id: inactivePriceList.id }, commandCtx));
    const value = expectOk(result);

    expect(value.purchasePriceList.status).toBe("ACTIVE");
  });

  it("returns success when the list is already active", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([basePriceList]);

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

    expect(value.purchasePriceList).toEqual(basePriceList);
  });

  it("returns error when price list does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { id: "missing-list" }, commandCtx));

    expectErr(result, PriceListNotFoundError);
  });
});
