import { z } from 'zod';
import type { ToolRegistry } from '../tool-registry.js';
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';

/**
 * Register the meta tools (list_tool_packs, enable_tool_pack) directly
 * on the server, since they interact with the registry itself rather
 * than making API calls.
 */
export function registerMetaTools(server: McpServer, registry: ToolRegistry): void {
  server.tool(
    'list_tool_packs',
    'List available tool packs that can be enabled on demand. ' +
      'Tool packs add specialized capabilities (email, SMS, payments, media, admin). ' +
      'Use enable_tool_pack to activate a pack and access its tools.',
    {},
    async () => {
      const packs = registry.getPackStatus();
      const text = packs
        .map(
          (p) =>
            `${p.active ? '[ACTIVE]' : '[AVAILABLE]'} ${p.name} (${p.toolCount} tools) — ${p.description}`
        )
        .join('\n');
      return {
        content: [{ type: 'text' as const, text: text || 'No tool packs available.' }],
      };
    }
  );

  server.tool(
    'enable_tool_pack',
    'Enable an on-demand tool pack to access specialized tools. ' +
      'Once enabled, the new tools become available for use. ' +
      'Use list_tool_packs to see available packs first.',
    {
      pack: z.string().describe('Name of the tool pack to enable (e.g., "email", "sms", "payments", "media", "admin")'),
    },
    async (args) => {
      const result = registry.enablePack(args.pack as string);
      return {
        content: [{ type: 'text' as const, text: result.message }],
      };
    }
  );
}
