import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { AssignmentNotFoundError } from "../lib/errors.generated";
import { baseCategoryAssignment } from "../testing/fixtures";
import type { RemoveProductFromCategoryInput } from "./removeProductFromCategory";
import { run } from "./removeProductFromCategory";

describe("removeProductFromCategory", () => {
  it("should return AssignmentNotFoundError when assignment does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);
    const input: RemoveProductFromCategoryInput = {
      productId: "non-existent",
      categoryId: "non-existent",
    };
    const result = await mock.withTx((trx) => run(trx, input));
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(AssignmentNotFoundError);
    }
  });

  it("should remove assignment successfully", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseCategoryAssignment], []);

    const input: RemoveProductFromCategoryInput = {
      productId: baseCategoryAssignment.productId,
      categoryId: baseCategoryAssignment.categoryId,
    };
    const result = await mock.withTx((trx) => run(trx, input));
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.id).toBe(baseCategoryAssignment.id);
    }
  });
});
