import { describe, expect, it } from "vitest";
import { PermissionKey, SystemRole, type PermissionKey as PermissionKeyValue } from "@capakraken/shared";
import { getAvailableAssistantTools } from "../router/assistant-tool-policy.js";
import { selectAssistantToolsForRequest } from "../router/assistant-tool-selection.js";

function getSelectedToolNames(
  permissions: PermissionKeyValue[],
  messages: Array<{ role: "user" | "assistant"; content: string }>,
  userRole: SystemRole = SystemRole.ADMIN,
  pageContext?: string,
) {
  return selectAssistantToolsForRequest(
    getAvailableAssistantTools(new Set(permissions), userRole),
    messages,
    pageContext,
  ).map((tool) => tool.function.name);
}

describe("assistant tool selection", () => {
  it("caps the OpenAI tool payload to 128 definitions even for fully privileged admins", () => {
    const allPermissions = Object.values(PermissionKey);
    const selectedNames = getSelectedToolNames(
      allPermissions,
      [{ role: "user", content: "Bitte gib mir einen Überblick über das System." }],
      SystemRole.ADMIN,
    );

    expect(selectedNames.length).toBeLessThanOrEqual(128);
    expect(selectedNames).toContain("get_current_user");
    expect(selectedNames).toContain("search_resources");
    expect(selectedNames).toContain("search_projects");
  });

  it("prioritizes holiday and resource tools for German holiday questions", () => {
    const allPermissions = Object.values(PermissionKey);
    const selectedNames = getSelectedToolNames(
      allPermissions,
      [{ role: "user", content: "Kannst du mir alle Feiertage nennen, die Peter Parker in 2026 zustehen?" }],
      SystemRole.ADMIN,
    );

    expect(selectedNames.length).toBeLessThanOrEqual(128);
    expect(selectedNames).toContain("search_resources");
    expect(selectedNames).toContain("get_resource");
    expect(selectedNames).toContain("get_resource_holidays");
    expect(selectedNames).toContain("list_holidays_by_region");
    expect(selectedNames).toContain("list_holiday_calendars");
    expect(selectedNames).toContain("get_vacation_balance");
    expect(selectedNames).toContain("get_entitlement_summary");
    expect(selectedNames).toContain("list_vacations_upcoming");
  });

  it("prioritizes report and dashboard tools for reporting requests", () => {
    const allPermissions = Object.values(PermissionKey);
    const selectedNames = getSelectedToolNames(
      allPermissions,
      [{ role: "user", content: "Build me a dashboard report for monthly SAH, budget forecast and project health." }],
      SystemRole.ADMIN,
      "/dashboard",
    );

    expect(selectedNames.length).toBeLessThanOrEqual(128);
    expect(selectedNames).toContain("get_dashboard_detail");
    expect(selectedNames).toContain("get_budget_forecast");
    expect(selectedNames).toContain("get_project_health");
    expect(selectedNames).toContain("run_report");
    expect(selectedNames).toContain("get_statistics");
  });
});
