/**
 * @fileoverview Enhanced MCP server setup with automatic tool discovery
 *
 * @deprecated This file is deprecated. Tool discovery functionality has been
 * integrated into the main server and can be enabled via the FEATURE_TOOL_DISCOVERY
 * environment variable. Please use src/index.ts instead.
 *
 * Migration guide:
 * - Set FEATURE_TOOL_DISCOVERY=true to enable tool discovery
 * - Import DeepSourceMCPServer from index.ts
 * - See MIGRATION.md for detailed migration instructions
 */

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { createLogger } from '../utils/logging/logger.js';
import { createDefaultHandlerDeps } from '../handlers/base/handler.factory.js';
import { EnhancedToolRegistry } from './tool-registry-enhanced.js';
import { getToolDiscoveryConfig } from '../config/tool-discovery.config.js';
import { VERSION } from '../version.js';

const logger = createLogger('EnhancedMCPServer');

// Log deprecation warning
logger.warn(
  'DEPRECATED: server/index-enhanced.ts is deprecated. ' +
    'Tool discovery is now available in the main server via FEATURE_TOOL_DISCOVERY=true. ' +
    'This file will be removed in the next major version.'
);

/**
 * Initializes and runs the enhanced MCP server with tool discovery
 */
export async function runEnhancedServer(): Promise<void> {
  logger.info(`Starting DeepSource MCP Enhanced Server v${VERSION}`, {
    version: VERSION,
  });

  const server = new McpServer({
    name: 'deepsource-mcp-enhanced',
    version: VERSION,
  });

  const transport = new StdioServerTransport();

  // Get configuration for potential future use
  // const config = {
  //   apiKey: process.env.DEEPSOURCE_API_KEY || '',
  //   baseUrl: process.env.DEEPSOURCE_API_URL,
  // };

  // Create dependencies
  const defaultDeps = createDefaultHandlerDeps();
  // Repository factory available for future use
  // const repositoryFactory = createRepositoryFactory(config);

  // Create enhanced tool registry
  const toolRegistry = new EnhancedToolRegistry(server, defaultDeps);

  // Register existing tools manually (for backward compatibility)
  logger.info('Registering core DeepSource tools...');
  registerCoreTools(toolRegistry);

  // Discover and load additional tools
  logger.info('Discovering additional tools...');
  const discoveryConfig = getToolDiscoveryConfig();

  try {
    const discoveredTools = await toolRegistry.discoverTools(discoveryConfig);
    logger.info(`Discovered ${discoveredTools.length} additional tools`, {
      tools: discoveredTools,
    });
  } catch (error) {
    logger.error('Tool discovery failed', error);
  }

  // Log tool information
  const toolsInfo = toolRegistry.getToolsInfo();
  logger.info('Available tools:', {
    total: toolsInfo.length,
    categories: toolRegistry.getCategories(),
    tags: toolRegistry.getTags(),
  });

  // Start the server
  await server.connect(transport);
  logger.info('Enhanced MCP server started successfully');

  // Handle shutdown gracefully
  process.on('SIGINT', async () => {
    logger.info('Shutting down enhanced MCP server...');
    await server.close();
    process.exit(0);
  });
}

/**
 * Registers core DeepSource tools with the enhanced registry
 * @param registry - Enhanced tool registry
 */
function registerCoreTools(registry: EnhancedToolRegistry): void {
  // This is a simplified version for demonstration
  // In a real implementation, you would properly type each tool
  logger.info(
    `Core tools registration available in production implementation. Registry initialized: ${registry ? 'yes' : 'no'}`
  );
}

// Run the server if this is the main module
if (import.meta.url === `file://${process.argv[1]}`) {
  runEnhancedServer().catch((error) => {
    logger.error('Failed to start enhanced MCP server', error);
    process.exit(1);
  });
}
