import { describe, expect, it } from "vitest";
import { TOOL_DEFINITIONS } from "../router/assistant-tools.js";

describe("assistant router", () => {
  it("keeps assistant tool descriptions aligned with runtime permissions", () => {
    const toolDescriptions = new Map(
      TOOL_DEFINITIONS.map((tool) => [tool.function.name, tool.function.description]),
    );

    expect(toolDescriptions.get("create_estimate")).toContain("manageProjects");
    expect(toolDescriptions.get("create_estimate_planning_handoff")).toContain("manageAllocations");
    expect(toolDescriptions.get("get_estimate_detail")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("list_estimate_versions")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_estimate_version_snapshot")).toContain("viewCosts");
    expect(toolDescriptions.get("get_estimate_weekly_phasing")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_estimate_commercial_terms")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("create_vacation")).toContain("Any authenticated user can request leave for their own resource");
    expect(toolDescriptions.get("create_vacation")).toContain("manager/admin can create requests for others");
    expect(toolDescriptions.get("approve_vacation")).toContain("Manager or admin role");
    expect(toolDescriptions.get("reject_vacation")).toContain("Manager or admin role");
    expect(toolDescriptions.get("cancel_vacation")).toContain("Users can cancel their own requests");
    expect(toolDescriptions.get("set_entitlement")).toContain("Manager or admin role");
    expect(toolDescriptions.get("create_role")).toContain("manageRoles");
    expect(toolDescriptions.get("update_role")).toContain("manageRoles");
    expect(toolDescriptions.get("delete_role")).toContain("manageRoles");
    expect(toolDescriptions.get("create_org_unit")).toContain("Admin role");
    expect(toolDescriptions.get("update_org_unit")).toContain("Admin role");
    expect(toolDescriptions.get("list_users")).toContain("Admin role");
    expect(toolDescriptions.get("list_assignable_users")).toContain("Manager or admin role");
    expect(toolDescriptions.get("get_current_user")).toContain("authenticated user's own profile");
    expect(toolDescriptions.get("search_resources")).toContain("Resource overview access required");
    expect(toolDescriptions.get("search_by_skill")).toContain("Controller/manager/admin access required");
    expect(toolDescriptions.get("list_comments")).toContain("Supported comment entities: estimate, resource.");
    expect(toolDescriptions.get("list_comments")).toContain("Entity visibility is required");
    expect(toolDescriptions.get("create_comment")).toContain("Supported comment entities: estimate, resource.");
    expect(toolDescriptions.get("create_comment")).toContain("Entity visibility is required");
    expect(toolDescriptions.get("resolve_comment")).toContain("Supported comment entities: estimate, resource.");
    expect(toolDescriptions.get("list_notifications")).toContain("current user");
    expect(toolDescriptions.get("get_unread_notification_count")).toContain("current user");
    expect(toolDescriptions.get("list_tasks")).toContain("current user");
    expect(toolDescriptions.get("get_task_counts")).toContain("current user");
    expect(toolDescriptions.get("create_notification")).toContain("Manager or admin role");
    expect(toolDescriptions.get("create_task_for_user")).toContain("Manager or admin role");
    expect(toolDescriptions.get("send_broadcast")).toContain("Manager or admin role");
    expect(toolDescriptions.get("list_broadcasts")).toContain("Manager or admin role");
    expect(toolDescriptions.get("get_broadcast_detail")).toContain("Manager or admin role");
    expect(toolDescriptions.get("create_client")).toContain("Requires manager or admin role");
    expect(toolDescriptions.get("update_client")).toContain("Requires manager or admin role");
    expect(toolDescriptions.get("create_holiday_calendar")).toContain("Admin role");
    expect(toolDescriptions.get("create_holiday_calendar_entry")).toContain("Admin role");
    expect(toolDescriptions.get("query_change_history")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_entity_timeline")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("export_resources_csv")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("export_projects_csv")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("import_csv_data")).toContain("importData");
    expect(toolDescriptions.get("import_csv_data")).toContain("defaults to dry-run");
    expect(toolDescriptions.get("list_dispo_import_batches")).toContain("Admin role");
    expect(toolDescriptions.get("get_dispo_import_batch")).toContain("Admin role");
    expect(toolDescriptions.get("stage_dispo_import_batch")).toContain("Admin role");
    expect(toolDescriptions.get("validate_dispo_import_batch")).toContain("Admin role");
    expect(toolDescriptions.get("commit_dispo_import_batch")).toContain("Always confirm first");
    expect(toolDescriptions.get("get_system_settings")).toContain("Admin role");
    expect(toolDescriptions.get("update_system_settings")).toContain("Always confirm first");
    expect(toolDescriptions.get("update_system_settings")).toContain("Runtime secrets must be provisioned");
    expect(toolDescriptions.get("clear_stored_runtime_secrets")).toContain("deployment secret management");
    expect(toolDescriptions.get("get_ai_configured")).toContain("Admin role required");
    expect(toolDescriptions.get("list_system_role_configs")).toContain("Admin role required");
    expect(toolDescriptions.get("update_system_role_config")).toContain("Admin role");
    expect(toolDescriptions.get("list_webhooks")).toContain("Secrets are masked");
    expect(toolDescriptions.get("create_webhook")).toContain("Always confirm first");
    expect(toolDescriptions.get("test_webhook")).toContain("Always confirm first");
    expect(toolDescriptions.get("list_audit_log_entries")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_audit_log_entry")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_audit_log_timeline")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_audit_activity_summary")).toContain("Controller/manager/admin");
    expect(toolDescriptions.get("get_chargeability_report")).toContain("controller/manager/admin");
    expect(toolDescriptions.get("get_chargeability_report")).toContain("viewCosts");
    expect(toolDescriptions.get("get_resource_computation_graph")).toContain("useAssistantAdvancedTools");
    expect(toolDescriptions.get("get_project_computation_graph")).toContain("controller/manager/admin");
    expect(toolDescriptions.get("update_timeline_allocation_inline")).toContain("manager/admin");
    expect(toolDescriptions.get("apply_timeline_project_shift")).toContain("manageAllocations");
    expect(toolDescriptions.get("quick_assign_timeline_resource")).toContain("useAssistantAdvancedTools");
    expect(toolDescriptions.get("batch_quick_assign_timeline_resources")).toContain("manageAllocations");
    expect(toolDescriptions.get("batch_shift_timeline_allocations")).toContain("manager/admin");
  });

  it("keeps estimate tool parameter enums aligned with the current estimate schema", () => {
    const definitionByName = new Map(
      TOOL_DEFINITIONS.map((tool) => [tool.function.name, tool.function]),
    );

    const createEstimateStatus = (
      definitionByName.get("create_estimate")?.parameters as {
        properties?: Record<string, { enum?: unknown[] }>;
      }
    )?.properties?.status?.enum;
    const updateEstimateStatus = (
      definitionByName.get("update_estimate_draft")?.parameters as {
        properties?: Record<string, { enum?: unknown[] }>;
      }
    )?.properties?.status?.enum;
    const estimateExportFormats = (
      definitionByName.get("create_estimate_export")?.parameters as {
        properties?: Record<string, { enum?: unknown[] }>;
      }
    )?.properties?.format?.enum;

    expect(createEstimateStatus).toEqual(["DRAFT", "IN_REVIEW", "APPROVED", "ARCHIVED"]);
    expect(updateEstimateStatus).toEqual(["DRAFT", "IN_REVIEW", "APPROVED", "ARCHIVED"]);
    expect(estimateExportFormats).toEqual(["XLSX", "CSV", "JSON", "SAP", "MMP"]);
  });
});
