import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { baseRootNode, baseChildNode, baseLeafNode } from "../testing/fixtures";
import { run } from "./detectCircularReference";

describe("detectCircularReference", () => {
  it("detects self-reference as circular", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();

    const result = await run(mock.db, { nodeId: "node-1", newParentId: "node-1" });

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

    expect(mock.selects).toHaveLength(0);
  });

  it("detects moving node under its child as circular", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseChildNode]);

    const result = await run(mock.db, { nodeId: "node-1", newParentId: "node-2" });

    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("node-2");
    expect(mock.selects[0]?.sql).toMatch(/"id"/);
  });

  it("detects moving node under its grandchild as circular", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseLeafNode], [baseChildNode]);

    const result = await run(mock.db, { nodeId: "node-1", newParentId: "node-3" });

    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("node-3");
    expect(mock.selects[1]?.parameters).toContain(baseLeafNode.parentId);
  });

  it("returns not circular for valid move", async () => {
    const secondRoot = { ...baseRootNode, id: "node-4", parentId: null };
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([secondRoot]);

    const result = await run(mock.db, { nodeId: "node-3", newParentId: "node-4" });

    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("node-4");
  });

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

    const result = await run(mock.db, { nodeId: "node-2", newParentId: "node-1" });

    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("node-1");
  });

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

    const result = await run(mock.db, { nodeId: "node-1", newParentId: "missing-node" });

    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("missing-node");
  });
});
