import type { Tool, ToolResult } from '../types.js';
import type { AgentConfigRegistry } from '../../agents/registry.js';

/**
 * Creates an agents.list tool bound to the given AgentConfigRegistry.
 * Lists all registered agent configurations with their settings.
 */
export function createAgentsListTool(registry: AgentConfigRegistry): Tool {
  return {
    name: 'agents.list',
    description:
      'List all registered agent configurations. Shows agent names, model tiers, tool profiles, and sandbox status.',
    inputSchema: {
      type: 'object',
      properties: {},
    },
    execute: async (_rawArgs: unknown): Promise<ToolResult> => {
      try {
        const configs = registry.list();

        if (configs.length === 0) {
          return {
            success: true,
            output: 'No agent configurations registered.',
          };
        }

        const lines = configs.map((c) => {
          const parts = [`- **${c.name}**`];
          if (c.modelTier) {parts.push(`tier=${c.modelTier}`);}
          if (c.toolProfile) {parts.push(`profile=${c.toolProfile}`);}
          if (c.sandbox) {parts.push('sandboxed');}
          if (c.systemPrompt) {
            const preview = c.systemPrompt.slice(0, 80).replace(/\n/g, ' ');
            parts.push(`prompt="${preview}${c.systemPrompt.length > 80 ? '...' : ''}"`);
          }
          return parts.join(' | ');
        });

        return {
          success: true,
          output: `${configs.length} agent(s) registered:\n\n${lines.join('\n')}`,
        };
      } catch (error) {
        return {
          success: false,
          output: '',
          error: error instanceof Error ? error.message : String(error),
        };
      }
    },
  };
}
