import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { baseActiveProduct, baseChildCategory, baseCategoryAssignment } from "../testing/fixtures";
import { ProductNotFoundError, CategoryNotFoundError } from "../lib/errors.generated";
import type { AssignProductToCategoryInput } from "./assignProductToCategory";
import { run } from "./assignProductToCategory";

describe("assignProductToCategory", () => {
  it("should return ProductNotFoundError when product does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // getProduct -> null
    spies.select.mockReturnValueOnce(undefined);
    const input: AssignProductToCategoryInput = {
      productId: "non-existent",
      categoryId: baseChildCategory.id,
    };
    const result = await run(db, input);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(ProductNotFoundError);
    }
  });

  it("should return CategoryNotFoundError when category does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: getProduct -> found
    spies.select.mockReturnValueOnce(baseActiveProduct);
    // 2nd select: getProductCategory -> null
    spies.select.mockReturnValueOnce(undefined);
    const input: AssignProductToCategoryInput = {
      productId: baseActiveProduct.id,
      categoryId: "non-existent",
    };
    const result = await run(db, input);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(CategoryNotFoundError);
    }
  });

  it("should create assignment successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: getProduct -> found
    spies.select.mockReturnValueOnce(baseActiveProduct);
    // 2nd select: getProductCategory -> found
    spies.select.mockReturnValueOnce(baseChildCategory);
    // 3rd select: getProductCategoryAssignment -> null (no existing)
    spies.select.mockReturnValueOnce(undefined);
    // insert returns new assignment
    const newAssignment = {
      ...baseCategoryAssignment,
      id: "new-assign-id",
      productId: baseActiveProduct.id,
      categoryId: baseChildCategory.id,
    };
    spies.insert.mockReturnValueOnce(newAssignment);

    const input: AssignProductToCategoryInput = {
      productId: baseActiveProduct.id,
      categoryId: baseChildCategory.id,
    };
    const result = await run(db, input);
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.productCategoryAssignment).toEqual(newAssignment);
    }
  });

  it("should return existing assignment when already assigned (idempotent)", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: getProduct -> found
    spies.select.mockReturnValueOnce(baseActiveProduct);
    // 2nd select: getProductCategory -> found
    spies.select.mockReturnValueOnce(baseChildCategory);
    // 3rd select: getProductCategoryAssignment -> existing
    spies.select.mockReturnValueOnce(baseCategoryAssignment);

    const input: AssignProductToCategoryInput = {
      productId: baseActiveProduct.id,
      categoryId: baseChildCategory.id,
    };
    const result = await run(db, input);
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.productCategoryAssignment).toEqual(baseCategoryAssignment);
    }
    expect(spies.insert).not.toHaveBeenCalled();
  });
});
