/**
 * Monday.com MCP Tools Index
 * Aggregates all tool modules
 */

import { Tool } from "@modelcontextprotocol/sdk/types.js";
import { MondayClient } from "../clients/monday.js";

import * as boards from "./boards.js";
import * as items from "./items.js";
import * as columns from "./columns.js";
import * as groups from "./groups.js";
import * as updates from "./updates.js";
import * as users from "./users.js";
import * as teams from "./teams.js";
import * as workspaces from "./workspaces.js";
import * as folders from "./folders.js";
import * as webhooks from "./webhooks.js";
import * as automations from "./automations.js";

/**
 * Get all Monday.com tools
 */
export function getAllTools(client: MondayClient): Tool[] {
  return [
    ...boards.getTools(client),
    ...items.getTools(client),
    ...columns.getTools(client),
    ...groups.getTools(client),
    ...updates.getTools(client),
    ...users.getTools(client),
    ...teams.getTools(client),
    ...workspaces.getTools(client),
    ...folders.getTools(client),
    ...webhooks.getTools(client),
    ...automations.getTools(client),
  ];
}

/**
 * Execute a Monday.com tool
 */
export async function executeTool(
  client: MondayClient,
  toolName: string,
  args: any
): Promise<any> {
  // Board tools
  if (toolName.startsWith("monday_list_boards") || 
      toolName.startsWith("monday_get_board") || 
      toolName.startsWith("monday_create_board") ||
      toolName.startsWith("monday_update_board") ||
      toolName.startsWith("monday_delete_board") ||
      toolName.startsWith("monday_archive_board") ||
      toolName.startsWith("monday_duplicate_board")) {
    return await boards.executeBoardTool(client, toolName, args);
  }

  // Item tools
  if (toolName.startsWith("monday_list_items") || 
      toolName.startsWith("monday_get_item") || 
      toolName.startsWith("monday_create_item") ||
      toolName.startsWith("monday_update_item") ||
      toolName.startsWith("monday_delete_item") ||
      toolName.startsWith("monday_move_item") ||
      toolName.startsWith("monday_duplicate_item") ||
      toolName.startsWith("monday_archive_item") ||
      toolName.startsWith("monday_create_subitem") ||
      toolName.startsWith("monday_list_subitems") ||
      toolName.startsWith("monday_clear_item") ||
      toolName.startsWith("monday_change_item")) {
    return await items.executeItemTool(client, toolName, args);
  }

  // Column tools
  if (toolName.startsWith("monday_list_columns") || 
      toolName.startsWith("monday_get_column") || 
      toolName.startsWith("monday_create_column") ||
      toolName.startsWith("monday_update_column") ||
      toolName.startsWith("monday_delete_column") ||
      toolName.startsWith("monday_change_column") ||
      toolName.startsWith("monday_change_simple") ||
      toolName.startsWith("monday_change_multiple")) {
    return await columns.executeColumnTool(client, toolName, args);
  }

  // Group tools
  if (toolName.startsWith("monday_list_groups") || 
      toolName.startsWith("monday_get_group") || 
      toolName.startsWith("monday_create_group") ||
      toolName.startsWith("monday_update_group") ||
      toolName.startsWith("monday_delete_group") ||
      toolName.startsWith("monday_duplicate_group") ||
      toolName.startsWith("monday_archive_group")) {
    return await groups.executeGroupTool(client, toolName, args);
  }

  // Update tools
  if (toolName.startsWith("monday_list_updates") || 
      toolName.startsWith("monday_get_update") || 
      toolName.startsWith("monday_create_update") ||
      toolName.startsWith("monday_delete_update") ||
      toolName.startsWith("monday_like_update") ||
      toolName.startsWith("monday_list_replies") ||
      toolName.startsWith("monday_edit_update")) {
    return await updates.executeUpdateTool(client, toolName, args);
  }

  // User tools
  if (toolName.startsWith("monday_list_users") || 
      toolName.startsWith("monday_get_user") ||
      toolName.startsWith("monday_get_current_user")) {
    return await users.executeUserTool(client, toolName, args);
  }

  // Team tools
  if (toolName.startsWith("monday_list_teams") || 
      toolName.startsWith("monday_get_team")) {
    return await teams.executeTeamTool(client, toolName, args);
  }

  // Workspace tools
  if (toolName.startsWith("monday_list_workspaces") || 
      toolName.startsWith("monday_get_workspace") ||
      toolName.startsWith("monday_create_workspace")) {
    return await workspaces.executeWorkspaceTool(client, toolName, args);
  }

  // Folder tools
  if (toolName.startsWith("monday_list_folders") || 
      toolName.startsWith("monday_get_folder") ||
      toolName.startsWith("monday_create_folder") ||
      toolName.startsWith("monday_update_folder") ||
      toolName.startsWith("monday_delete_folder")) {
    return await folders.executeFolderTool(client, toolName, args);
  }

  // Webhook tools
  if (toolName.startsWith("monday_create_webhook") || 
      toolName.startsWith("monday_delete_webhook") ||
      toolName.startsWith("monday_list_webhooks")) {
    return await webhooks.executeWebhookTool(client, toolName, args);
  }

  // Automation tools
  if (toolName.startsWith("monday_list_automations") || 
      toolName.startsWith("monday_get_automation")) {
    return await automations.executeAutomationTool(client, toolName, args);
  }

  throw new Error(`Unknown tool: ${toolName}`);
}

/**
 * Export individual tool modules for direct access
 */
export {
  boards,
  items,
  columns,
  groups,
  updates,
  users,
  teams,
  workspaces,
  folders,
  webhooks,
  automations,
};
