// src/tools/index.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import type { ToolDefinition } from "../lib/ai-client.js";

import { DtpTool } from "../lib/tool.js";
import { ChatSessionMode } from "../models/chat-session.js";
export { DtpTool } from "../lib/tool.js";
export type { ToolArguments, ToolContext, ToolMetadata } from "../lib/tool.js";
export type { ToolDefinition } from "../lib/ai-client.js";

const tools = new Map<string, DtpTool>();

export function registerTool(tool: DtpTool): void {
  if (tools.has(tool.slug)) {
    // Tool already registered - this is expected during development/testing
  }
  tools.set(tool.slug, tool);
}

export function getTool(slug: string): DtpTool | undefined {
  return tools.get(slug);
}

export function getToolByName(name: string): DtpTool | undefined {
  for (const tool of tools.values()) {
    if (tool.definition.function.name === name) {
      return tool;
    }
  }
  return undefined;
}

export function getAllTools(): DtpTool[] {
  return Array.from(tools.values());
}

export function getToolDefinitions(): ToolDefinition[] {
  return getAllTools().map((t) => t.definition);
}

export function getToolsBySlugs(slugs: string[]): DtpTool[] {
  return slugs
    .map((slug) => tools.get(slug))
    .filter((t): t is DtpTool => t !== undefined);
}

export function getToolsByCategory(category: string): DtpTool[] {
  return getAllTools().filter((t) => t.metadata.category === category);
}

export function getToolsExcludingCategory(category: string): DtpTool[] {
  return getAllTools().filter((t) => t.metadata.category !== category);
}

export function getToolsByMode(mode: ChatSessionMode): DtpTool[] {
  return getAllTools().filter((t) => {
    const modes = t.metadata.modes;
    if (!modes || modes.length === 0) {
      return true;
    }
    return modes.includes(mode);
  });
}
