import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { DB } from "../generated/kysely-tailordb";
import {
  baseCostCenter,
  baseCostCenter2,
  baseCostCenterHierarchy,
  childCostCenterNode,
  childCostCenterNode2,
  rootCostCenterNode,
} from "../testing/fixtures";
import { run } from "./getCostCenterHierarchy";

describe("getCostCenterHierarchy", () => {
  it("returns hierarchy with all nodes in tree structure when found by id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseCostCenterHierarchy)
      .mockReturnValueOnce([rootCostCenterNode, childCostCenterNode, childCostCenterNode2])
      .mockReturnValueOnce([])
      .mockReturnValueOnce([baseCostCenter])
      .mockReturnValueOnce([baseCostCenter2]);

    const result = await run(db, { id: baseCostCenterHierarchy.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.costCenterHierarchy).toEqual(baseCostCenterHierarchy);
      expect(result.value.nodes).toHaveLength(1);
      expect(result.value.nodes[0].children).toHaveLength(2);
    }
  });

  it("returns hierarchy with root node and nested child nodes", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseCostCenterHierarchy)
      .mockReturnValueOnce([rootCostCenterNode, childCostCenterNode])
      .mockReturnValueOnce([])
      .mockReturnValueOnce([baseCostCenter]);

    const result = await run(db, { id: baseCostCenterHierarchy.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.nodes[0]).toMatchObject({
        id: rootCostCenterNode.id,
        parentNodeId: null,
      });
      expect(result.value.nodes[0].children[0]).toMatchObject({
        id: childCostCenterNode.id,
        parentNodeId: rootCostCenterNode.id,
      });
    }
  });

  it("returns hierarchy nodes with assigned cost centers populated", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseCostCenterHierarchy)
      .mockReturnValueOnce([childCostCenterNode])
      .mockReturnValueOnce([baseCostCenter]);

    const result = await run(db, { id: baseCostCenterHierarchy.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.nodes[0]).toMatchObject({ id: childCostCenterNode.id });
      expect(result.value.nodes[0].costCenters).toEqual([baseCostCenter]);
    }
  });

  it("returns leaf nodes with no children", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select
      .mockReturnValueOnce(baseCostCenterHierarchy)
      .mockReturnValueOnce([childCostCenterNode])
      .mockReturnValueOnce([]);

    const result = await run(db, { id: baseCostCenterHierarchy.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.nodes[0].children).toEqual([]);
    }
  });

  it("returns null when no hierarchy matches the given id", async () => {
    const { db, spies } = createMockDb<DB>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, { id: "non-existent" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.costCenterHierarchy).toBeNull();
      expect(result.value.nodes).toEqual([]);
    }
  });
});
