import {
  McpToolDefinition,
  McpToolHandler,
  McpToolProvider,
  ToolCategory,
} from '../interfaces';

export class ToolRegistryService {
  private tools: Map<string, McpToolDefinition> = new Map();
  private handlers: Map<string, McpToolHandler> = new Map();
  private providers: McpToolProvider[] = [];

  registerProvider(provider: McpToolProvider): void {
    this.providers.push(provider);
    const tools = provider.getTools();

    for (const tool of tools) {
      this.tools.set(tool.name, tool);
      const handler = provider.getHandler(tool.name);
      if (handler) {
        this.handlers.set(tool.name, handler);
      }
    }
  }

  getAllTools(): McpToolDefinition[] {
    return Array.from(this.tools.values());
  }

  getTool(name: string): McpToolDefinition | null {
    return this.tools.get(name) || null;
  }

  getHandler(name: string): McpToolHandler | null {
    return this.handlers.get(name) || null;
  }

  getToolsByCategory(category: ToolCategory): McpToolDefinition[] {
    return Array.from(this.tools.values()).filter((t) => t.category === category);
  }

  hasTool(name: string): boolean {
    return this.tools.has(name);
  }

  getCategories(): ToolCategory[] {
    const categories = new Set<ToolCategory>();
    for (const tool of this.tools.values()) {
      categories.add(tool.category);
    }
    return Array.from(categories);
  }
}
