/**
 * Intercom MCP Tools - Index
 * Aggregates all tool modules
 */

import { Tool } from '@modelcontextprotocol/sdk/types.js';
import type { IntercomClient } from '../clients/intercom.js';

import { getTools as getContactTools } from './contacts.js';
import { getTools as getConversationTools } from './conversations.js';
import { getTools as getCompanyTools } from './companies.js';
import { getTools as getArticleTools } from './articles.js';
import { getTools as getHelpCenterTools } from './help-center.js';
import { getTools as getTicketTools } from './tickets.js';
import { getTools as getTagTools } from './tags.js';
import { getTools as getSegmentTools } from './segments.js';
import { getTools as getEventTools } from './events.js';
import { getTools as getMessageTools } from './messages.js';
import { getTools as getTeamTools } from './teams.js';
import { getTools as getAdminTools } from './admins.js';

export interface ToolDefinition {
  definition: Tool;
  handler: (args: Record<string, unknown>) => Promise<unknown>;
}

/**
 * Get all Intercom MCP tools
 * @param client - Initialized IntercomClient instance
 * @returns Array of tool definitions with handlers
 */
export function getAllTools(client: IntercomClient): ToolDefinition[] {
  return [
    ...getContactTools(client),
    ...getConversationTools(client),
    ...getCompanyTools(client),
    ...getArticleTools(client),
    ...getHelpCenterTools(client),
    ...getTicketTools(client),
    ...getTagTools(client),
    ...getSegmentTools(client),
    ...getEventTools(client),
    ...getMessageTools(client),
    ...getTeamTools(client),
    ...getAdminTools(client),
  ];
}

/**
 * Get tool definitions only (without handlers)
 * @param client - Initialized IntercomClient instance
 * @returns Array of MCP tool definitions
 */
export function getToolDefinitions(client: IntercomClient): Tool[] {
  return getAllTools(client).map((t) => t.definition);
}

/**
 * Get a specific tool handler by name
 * @param client - Initialized IntercomClient instance
 * @param toolName - Name of the tool
 * @returns Tool handler function or undefined
 */
export function getToolHandler(
  client: IntercomClient,
  toolName: string
): ((args: Record<string, unknown>) => Promise<unknown>) | undefined {
  const tools = getAllTools(client);
  const tool = tools.find((t) => t.definition.name === toolName);
  return tool?.handler;
}
