import { z } from 'zod';
import { IToolDefinition, ToolType } from '../../lib/types/index';

/**
 * Knowledge Domain Configuration & Trace Tool Definitions
 */
export const configSchema: Record<string, IToolDefinition> = {
  prioritizeMemory: {
    type: ToolType.FUNCTION,
    requiresApproval: false,
    requiredPermissions: [],
    name: 'prioritizeMemory',
    description:
      'Adjusts the priority, urgency, and impact scores of a memory insight or capability gap.',
    parameters: {
      type: 'object',
      properties: {
        timestamp: {
          type: 'number',
          description: 'The timestamp (sort key) of the memory item.',
        },
        priority: {
          type: 'number',
          description: 'New priority score (0-10).',
        },
        urgency: {
          type: 'number',
          description: 'New urgency score (0-10).',
        },
        impact: {
          type: 'number',
          description: 'New impact score (0-10).',
        },
      },
      required: ['timestamp'],
      additionalProperties: false,
    },
    argSchema: z.object({
      userId: z.string(),
      timestamp: z.number(),
      priority: z.number().min(0).max(10).optional(),
      urgency: z.number().min(0).max(10).optional(),
      impact: z.number().min(0).max(10).optional(),
    }),
    connectionProfile: ['memory'],
  },
  deleteTraces: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'deleteTraces',
    description:
      'Deletes execution traces. Pass "all" to purge all traces, or a specific trace ID to delete one.',
    parameters: {
      type: 'object',
      properties: {
        traceId: {
          type: 'string',
          description: 'The trace ID to delete, or "all" to purge all traces.',
        },
      },
      required: ['traceId'],
      additionalProperties: false,
    },
    connectionProfile: ['trace'],
  },
  refineMemory: {
    type: ToolType.FUNCTION,
    requiresApproval: false,
    requiredPermissions: [],
    name: 'refineMemory',
    description:
      'Updates or corrects an existing memory item. Use this when you have new information that refines a previous conclusion or lesson.',
    parameters: {
      type: 'object',
      properties: {
        timestamp: {
          type: 'number',
          description: 'The exact timestamp (sort key) of the memory item to refine.',
        },
        content: {
          type: 'string',
          description: 'The new or updated content for the memory.',
        },
        tags: {
          type: 'array',
          items: { type: 'string' },
          description: 'Updated tags for the memory.',
        },
        priority: {
          type: 'number',
          description: 'New priority score (0-10).',
        },
      },
      required: ['timestamp'],
      additionalProperties: false,
    },
    argSchema: z.object({
      userId: z.string(),
      timestamp: z.number(),
      content: z.string().optional(),
      tags: z.array(z.string()).optional(),
      priority: z.number().min(0).max(10).optional(),
    }),
    connectionProfile: ['memory'],
  },
  forceReleaseLock: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'forceReleaseLock',
    description: 'Force-releases a distributed session lock by deleting it from memory.',
    parameters: {
      type: 'object',
      properties: {
        lockId: { type: 'string', description: 'ID of the lock to release.' },
      },
      required: ['lockId'],
      additionalProperties: false,
    },
    connectionProfile: ['memory'],
  },
  technicalResearch: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'technicalResearch',
    description:
      'Dispatches a technical research task. Supports single-step discovery or parallel multi-agent exploration.',
    parameters: {
      type: 'object',
      properties: {
        goal: {
          type: 'string',
          description:
            'The technical research goal or question (e.g., "Research Auth0 vs Cognito").',
        },
        agentId: {
          type: 'string',
          description: 'The research agent to use. Defaults to "researcher".',
        },
        parallel: {
          type: 'boolean',
          description:
            'Whether to allow the agent to decompose this into parallel sub-tasks. Recommended for complex comparisons.',
        },
        depth: {
          type: 'number',
          description: 'Current recursion depth.',
        },
      },
      required: ['goal'],
      additionalProperties: false,
    },
    connectionProfile: ['bus'],
  },
  requestResearch: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'requestResearch',
    description:
      'Dispatches a technical research mission to the Researcher Agent. The current agent execution will pause until research is completed.',
    parameters: {
      type: 'object',
      properties: {
        goal: {
          type: 'string',
          description:
            'The technical research goal or question (e.g., "Research Auth0 vs Cognito").',
        },
        parallel: {
          type: 'boolean',
          description: 'Whether to allow the agent to decompose this into parallel sub-tasks.',
        },
      },
      required: ['goal'],
      additionalProperties: false,
    },
    connectionProfile: ['bus'],
  },

  // MCP (from mcp.ts)
  registerMCPServer: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'registerMCPServer',
    description: 'Registers a new Model Context Protocol (MCP) server for dynamic tool discovery.',
    parameters: {
      type: 'object',
      properties: {
        serverName: {
          type: 'string',
          description: 'A unique name for the server (e.g., git, search).',
        },
        type: {
          type: 'string',
          enum: ['local', 'remote', 'managed'],
          description:
            'The connection type: "local" (default), "remote" (SSE), or "managed" (OpenAI Connector).',
        },
        command: {
          type: 'string',
          description: 'The command to run the server (required for type: "local").',
        },
        url: {
          type: 'string',
          description: 'The URL of the remote MCP server (required for type: "remote").',
        },
        connector_id: {
          type: 'string',
          description: 'The ID of the managed connector (required for type: "managed").',
        },
        env: {
          type: 'string',
          description:
            'Optional environment variables for the server (JSON stringified object, local only).',
        },
      },
      required: ['serverName'],
      additionalProperties: false,
    },
    connectionProfile: ['storage'],
  },
  unregisterMCPServer: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'unregisterMCPServer',
    description: 'Removes an MCP server and all its associated tools from the system.',
    parameters: {
      type: 'object',
      properties: {
        serverName: {
          type: 'string',
          description: 'The name of the MCP server to remove.',
        },
      },
      required: ['serverName'],
      additionalProperties: false,
    },
    connectionProfile: ['storage'],
  },
  getMcpConfig: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'getMcpConfig',
    description: 'Retrieves the current Model Context Protocol (MCP) servers configuration.',
    parameters: {
      type: 'object',
      properties: {},
      additionalProperties: false,
    },
    connectionProfile: ['storage'],
  },

  // Runtime Config (from config.ts)
  checkConfig: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'checkConfig',
    description:
      'Retrieves the current runtime configuration, including active LLM provider and model.',
    parameters: {
      type: 'object',
      properties: {},
      required: [],
      additionalProperties: false,
    },
    connectionProfile: ['config'],
  },
  setSystemConfig: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'setSystemConfig',
    description: 'Updates a system-wide configuration value in the ConfigTable.',
    parameters: {
      type: 'object',
      properties: {
        key: {
          type: 'string',
          description: 'The configuration key (e.g. evolution_mode, deploy_limit).',
        },
        value: {
          type: 'string',
          description: 'The new value for the configuration (JSON stringified if object).',
        },
      },
      required: ['key', 'value'],
      additionalProperties: false,
    },
    connectionProfile: ['config'],
  },
  listSystemConfigs: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'listSystemConfigs',
    description: 'Lists all available runtime configuration keys and their current values.',
    parameters: {
      type: 'object',
      properties: {},
      required: [],
      additionalProperties: false,
    },
    connectionProfile: ['config'],
  },

  // Metadata (from metadata.ts)
  getSystemConfigMetadata: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'getSystemConfigMetadata',
    description:
      'Retrieves technical documentation, implications, and risks for all system configuration keys.',
    parameters: {
      type: 'object',
      properties: {},
      required: [],
      additionalProperties: false,
    },
    connectionProfile: ['config'],
  },
  inspectTrace: {
    type: ToolType.FUNCTION,
    argSchema: z.any(),
    requiresApproval: false,
    requiredPermissions: [],
    name: 'inspectTrace',
    description: 'Retrieves the full execution trace for a given trace ID.',
    parameters: {
      type: 'object',
      properties: {
        traceId: { type: 'string', description: 'The trace ID to inspect.' },
      },
      required: ['traceId'],
      additionalProperties: false,
    },
    connectionProfile: ['trace'],
  },
};
