import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import { activeDepartment, childDepartment, grandchildDepartment } from "../testing/fixtures";
import { run } from "./detectDepartmentCircularReference";

describe("detectDepartmentCircularReference", () => {
  it("detects self-reference as circular", async () => {
    const { db } = createMockDb<DB>();

    const result = await run(db, {
      departmentId: activeDepartment.id,
      newParentId: activeDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
  });

  it("detects moving department under its child as circular", async () => {
    const { db, spies } = createMockDb<DB>();
    // activeDepartment -> childDepartment
    // Walk from childDepartment: parentDepartmentId = activeDepartment.id === departmentId -> circular
    spies.select.mockReturnValueOnce(childDepartment);

    const result = await run(db, {
      departmentId: activeDepartment.id,
      newParentId: childDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
  });

  it("detects moving department under its grandchild as circular", async () => {
    const { db, spies } = createMockDb<DB>();
    // activeDepartment -> childDepartment -> grandchildDepartment
    // Walk: grandchild -> child -> activeDepartment.id === departmentId -> circular
    spies.select.mockReturnValueOnce(grandchildDepartment).mockReturnValueOnce(childDepartment);

    const result = await run(db, {
      departmentId: activeDepartment.id,
      newParentId: grandchildDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
  });

  it("returns not circular for valid move", async () => {
    const { db, spies } = createMockDb<DB>();
    const otherRoot = { ...activeDepartment, id: "other-root", parentDepartmentId: null };
    spies.select.mockReturnValueOnce(otherRoot);

    const result = await run(db, {
      departmentId: grandchildDepartment.id,
      newParentId: otherRoot.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
  });

  it("returns not circular when new parent is root", async () => {
    const { db, spies } = createMockDb<DB>();
    // activeDepartment is root (parentDepartmentId = null)
    spies.select.mockReturnValueOnce(activeDepartment);

    const result = await run(db, {
      departmentId: childDepartment.id,
      newParentId: activeDepartment.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
  });

  it("handles broken ancestor chain gracefully (not circular)", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, {
      departmentId: activeDepartment.id,
      newParentId: "some-department-id",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
  });
});
