import { beforeEach, describe, expect, it, vi } from "vitest";
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 { countPlanningEntries } from "@capakraken/application";
import { executeTool } from "../router/assistant-tools.js";
import { createToolContext } from "./assistant-tools-master-data-read-test-helpers.js";

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

  it("routes role reads through their backing router", async () => {
    const db = {
      role: {
        findMany: vi.fn().mockResolvedValue([
          {
            id: "role_anim",
            name: "Animation",
            color: "#112233",
            _count: { resourceRoles: 2 },
          },
        ]),
      },
    };
    const ctx = createToolContext(db, {
      userRole: SystemRole.CONTROLLER,
      permissions: [PermissionKey.VIEW_PLANNING],
    });

    const rolesResult = await executeTool("list_roles", "{}", ctx);

    expect(db.role.findMany).toHaveBeenCalledWith({
      where: {},
      include: {
        _count: {
          select: { resourceRoles: true },
        },
      },
      orderBy: { name: "asc" },
    });
    expect(JSON.parse(rolesResult.content)).toEqual([
      {
        id: "role_anim",
        name: "Animation",
        color: "#112233",
      },
    ]);
  });
});
