import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { InvalidSalespriceruleStatusError } from "../lib/errors.generated";
import { basePriceRule, draftPriceRule } from "../testing/fixtures";
import { commandCtx, expectErr, expectOk } from "../testing/commandTestUtils";
import { run } from "./deactivateSalesPriceRule";

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

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

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

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

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

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