import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { baseLeafCategory, baseChildCategory, baseCategoryAssignment } from "../testing/fixtures";
import {
  CategoryNotFoundError,
  HasChildrenError,
  HasAssignmentsError,
} from "../lib/errors.generated";
import type { DeleteProductCategoryInput } from "./deleteProductCategory";
import { run } from "./deleteProductCategory";

describe("deleteProductCategory", () => {
  it("should return CategoryNotFoundError when category does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // select for category -> not found
    spies.select.mockReturnValueOnce(undefined);
    const input: DeleteProductCategoryInput = { id: "non-existent" };
    const result = await run(db, input);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(CategoryNotFoundError);
    }
  });

  it("should return HasChildrenError when category has children", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: category found
    spies.select.mockReturnValueOnce(baseChildCategory);
    // 2nd select: child exists (executeTakeFirst)
    spies.select.mockReturnValueOnce(baseLeafCategory);
    const input: DeleteProductCategoryInput = { id: baseChildCategory.id };
    const result = await run(db, input);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(HasChildrenError);
    }
  });

  it("should return HasAssignmentsError when category has assignments", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: category found
    spies.select.mockReturnValueOnce(baseLeafCategory);
    // 2nd select: no children (executeTakeFirst)
    spies.select.mockReturnValueOnce(undefined);
    // 3rd select: assignment exists (executeTakeFirst)
    spies.select.mockReturnValueOnce(baseCategoryAssignment);
    const input: DeleteProductCategoryInput = { id: baseLeafCategory.id };
    const result = await run(db, input);
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(HasAssignmentsError);
    }
  });

  it("should delete leaf category successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    // 1st select: category found
    spies.select.mockReturnValueOnce(baseLeafCategory);
    // 2nd select: no children (executeTakeFirst)
    spies.select.mockReturnValueOnce(undefined);
    // 3rd select: no assignments (executeTakeFirst)
    spies.select.mockReturnValueOnce(undefined);
    // delete executes
    spies.delete.mockReturnValueOnce(undefined);

    const input: DeleteProductCategoryInput = { id: baseLeafCategory.id };
    const result = await run(db, input);
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.id).toBe(baseLeafCategory.id);
    }
  });
});
