import { beforeEach, describe, expect, it, vi } from "vitest";
import { countPlanningEntries } from "@capakraken/application";
import { PermissionKey, SystemRole } from "@capakraken/shared";

vi.mock("@capakraken/application", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@capakraken/application")>();
  return {
    ...actual,
    countPlanningEntries: vi.fn().mockResolvedValue({ countsByRoleId: new Map() }),
    getDashboardBudgetForecast: vi.fn().mockResolvedValue([]),
    getDashboardPeakTimes: vi.fn().mockResolvedValue([]),
    listAssignmentBookings: vi.fn().mockResolvedValue([]),
  };
});

import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-master-data-read-test-helpers.js";

describe("assistant master data management and utilization read tools", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(countPlanningEntries).mockResolvedValue({ countsByRoleId: new Map() });
  });

  it("routes management level and utilization category reads through their backing routers", async () => {
    const db = {
      managementLevelGroup: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "mlg_exec",
            name: "Executive",
            targetPercentage: 75,
            levels: [{ id: "ml_partner", name: "Partner" }],
          },
        ]),
      },
      utilizationCategory: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "util_billable",
            code: "BILLABLE",
            name: "Billable",
            description: "Client work",
            isActive: true,
            sortOrder: 1,
          },
        ]),
        findUnique: vi.fn().mockResolvedValue({
          id: "util_billable",
          code: "BILLABLE",
          name: "Billable",
          description: "Client work",
          isActive: true,
          sortOrder: 1,
          _count: { projects: 3 },
        }),
      },
    };
    const ctx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_PLANNING],
    });

    const managementLevelsResult = await executeTool("list_management_levels", "{}", ctx);
    const utilizationCategoriesResult = await executeTool("list_utilization_categories", "{}", ctx);

    expect(db.managementLevelGroup.findMany).toHaveBeenCalledWith({
      include: { levels: { orderBy: { name: "asc" } } },
      orderBy: { sortOrder: "asc" },
    });
    expect(db.utilizationCategory.findMany).toHaveBeenCalledWith({
      where: {},
      orderBy: { sortOrder: "asc" },
    });
    expect(db.utilizationCategory.findUnique).toHaveBeenCalledWith({
      where: { id: "util_billable" },
      include: { _count: { select: { projects: true } } },
    });

    expect(JSON.parse(managementLevelsResult.content)).toEqual([
      {
        id: "mlg_exec",
        name: "Executive",
        target: "75%",
        levels: [{ id: "ml_partner", name: "Partner" }],
      },
    ]);
    expect(JSON.parse(utilizationCategoriesResult.content)).toEqual([
      {
        id: "util_billable",
        code: "BILLABLE",
        name: "Billable",
        description: "Client work",
        projectCount: 3,
      },
    ]);
  });
});
