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

describe("activatePurchasePriceList", () => {
  it("activates an inactive price list", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(inactivePriceList);
    spies.update.mockReturnValueOnce({ ...inactivePriceList, status: "ACTIVE" });

    const result = await run(db, { id: inactivePriceList.id }, commandCtx);
    const value = expectOk<{ purchasePriceList: typeof inactivePriceList }>(result);

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

  it("returns success when the list is already active", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(basePriceList);

    const result = await run(db, { id: basePriceList.id }, commandCtx);
    const value = expectOk<{ purchasePriceList: typeof basePriceList }>(result);

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

  it("returns error when price list does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, { id: "missing-list" }, commandCtx);

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