import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
    expect(mock.selects).toHaveLength(0);
  });

  it("detects moving department under its child as circular", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // activeDepartment -> childDepartment
    // Walk from childDepartment: parentDepartmentId = activeDepartment.id === departmentId -> circular
    mock.enqueueResults([childDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(childDepartment.id);
    expect(mock.selects[0].sql).toMatch(/"id"/);
  });

  it("detects moving department under its grandchild as circular", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // activeDepartment -> childDepartment -> grandchildDepartment
    // Walk: grandchild -> child -> activeDepartment.id === departmentId -> circular
    mock.enqueueResults([grandchildDepartment], [childDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(true);
    }
    expect(mock.selects).toHaveLength(2);
    expect(mock.selects[0].parameters).toContain(grandchildDepartment.id);
    expect(mock.selects[1].parameters).toContain(grandchildDepartment.parentDepartmentId);
  });

  it("returns not circular for valid move", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const otherRoot = { ...activeDepartment, id: "other-root", parentDepartmentId: null };
    mock.enqueueResults([otherRoot]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(otherRoot.id);
  });

  it("returns not circular when new parent is root", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // activeDepartment is root (parentDepartmentId = null)
    mock.enqueueResults([activeDepartment]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain(activeDepartment.id);
  });

  it("handles broken ancestor chain gracefully (not circular)", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.isCircular).toBe(false);
    }
    expect(mock.selects).toHaveLength(1);
    expect(mock.selects[0].parameters).toContain("some-department-id");
  });
});
