import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { InvalidSalespricelistStatusError } from "../lib/errors.generated";
import { basePriceList, draftPriceList } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deactivateSalesPriceList";

describe("deactivateSalesPriceList", () => {
  it("deactivates an active SalesPriceList", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);
    spies.update.mockReturnValueOnce({
      ...basePriceList,
      status: "INACTIVE",
      updatedAt: new Date(),
    });

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

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

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

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

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