/**
 * MCP Tools Index
 *
 * Exports all MT4 MCP tools and their schemas for registration
 */

// Import handlers for use in toolHandlers map
import { handleMt4GetAccount } from './account';
import { handleMt4GetPositions, handleMt4ClosePosition } from './positions';
import { handleMt4ExecuteTrade, handleMt4ModifyPosition } from './trading';
import { handleMt4GetQuote } from './quotes';

// Account tools
export {
  mt4GetAccountSchema,
  mt4_get_account,
  handleMt4GetAccount,
  Mt4GetAccountInputSchema,
  type Mt4GetAccountInput,
  type Mt4GetAccountResult,
} from './account';

// Position tools
export {
  mt4GetPositionsSchema,
  mt4_get_positions,
  handleMt4GetPositions,
  Mt4GetPositionsInputSchema,
  mt4ClosePositionSchema,
  mt4_close_position,
  handleMt4ClosePosition,
  Mt4ClosePositionInputSchema,
  type Mt4GetPositionsInput,
  type Mt4GetPositionsResult,
  type Mt4ClosePositionInput,
  type Mt4ClosePositionResult,
} from './positions';

// Trading tools
export {
  mt4ExecuteTradeSchema,
  mt4_execute_trade,
  handleMt4ExecuteTrade,
  Mt4ExecuteTradeInputSchema,
  mt4ModifyPositionSchema,
  mt4_modify_position,
  handleMt4ModifyPosition,
  Mt4ModifyPositionInputSchema,
  type Mt4ExecuteTradeInput,
  type Mt4ExecuteTradeResult,
  type Mt4ModifyPositionInput,
  type Mt4ModifyPositionResult,
} from './trading';

// Quote tools
export {
  mt4GetQuoteSchema,
  mt4_get_quote,
  handleMt4GetQuote,
  Mt4GetQuoteInputSchema,
  type Mt4GetQuoteInput,
  type Mt4GetQuoteResult,
} from './quotes';

// ==========================================
// Tool Registry
// ==========================================

/**
 * All available MCP tools with their schemas
 */
export const mcpToolSchemas = [
  {
    name: 'mt4_get_account',
    description: 'Get MT4 account information including balance, equity, margin, and broker details',
    inputSchema: {
      type: 'object' as const,
      properties: {},
      required: [] as string[],
    },
  },
  {
    name: 'mt4_get_positions',
    description: 'List all open trading positions from MT4. Optionally filter by symbol.',
    inputSchema: {
      type: 'object' as const,
      properties: {
        symbol: {
          type: 'string',
          description: 'Optional: Filter positions by symbol (e.g., XAUUSD, EURUSD)',
        },
      },
      required: [] as string[],
    },
  },
  {
    name: 'mt4_execute_trade',
    description: 'Execute a market order (BUY or SELL) on MT4 with optional stop loss and take profit',
    inputSchema: {
      type: 'object' as const,
      properties: {
        symbol: {
          type: 'string',
          description: 'Trading symbol (e.g., XAUUSD, EURUSD, GBPUSD)',
        },
        action: {
          type: 'string',
          enum: ['buy', 'sell'],
          description: 'Trade direction: buy or sell',
        },
        lots: {
          type: 'number',
          description: 'Volume in lots (e.g., 0.01, 0.1, 1.0)',
        },
        stopLoss: {
          type: 'number',
          description: 'Optional: Stop loss price level',
        },
        takeProfit: {
          type: 'number',
          description: 'Optional: Take profit price level',
        },
        slippage: {
          type: 'number',
          description: 'Optional: Maximum slippage in points (default: 3)',
        },
        magic: {
          type: 'number',
          description: 'Optional: Magic number for EA identification (default: 12345)',
        },
        comment: {
          type: 'string',
          description: 'Optional: Order comment (max 31 chars)',
        },
      },
      required: ['symbol', 'action', 'lots'] as string[],
    },
  },
  {
    name: 'mt4_close_position',
    description: 'Close an open trading position by ticket number. Can close partially.',
    inputSchema: {
      type: 'object' as const,
      properties: {
        ticket: {
          type: 'number',
          description: 'Position ticket number to close',
        },
        lots: {
          type: 'number',
          description: 'Optional: Partial volume to close. If not specified, closes entire position.',
        },
        slippage: {
          type: 'number',
          description: 'Optional: Maximum slippage in points (default: 3)',
        },
      },
      required: ['ticket'] as string[],
    },
  },
  {
    name: 'mt4_modify_position',
    description: 'Modify stop loss and/or take profit of an existing position',
    inputSchema: {
      type: 'object' as const,
      properties: {
        ticket: {
          type: 'number',
          description: 'Position ticket number to modify',
        },
        stopLoss: {
          type: 'number',
          description: 'New stop loss price level (optional)',
        },
        takeProfit: {
          type: 'number',
          description: 'New take profit price level (optional)',
        },
      },
      required: ['ticket'] as string[],
    },
  },
  {
    name: 'mt4_get_quote',
    description: 'Get current price quote (bid/ask/spread) for a trading symbol',
    inputSchema: {
      type: 'object' as const,
      properties: {
        symbol: {
          type: 'string',
          description: 'Trading symbol to get quote for (e.g., XAUUSD, EURUSD, GBPUSD)',
        },
      },
      required: ['symbol'] as string[],
    },
  },
];

/**
 * Tool handler routing map
 */
export const toolHandlers: Record<
  string,
  (params: unknown) => Promise<{ content: Array<{ type: string; text: string }> }>
> = {
  mt4_get_account: handleMt4GetAccount,
  mt4_get_positions: handleMt4GetPositions,
  mt4_execute_trade: handleMt4ExecuteTrade,
  mt4_close_position: handleMt4ClosePosition,
  mt4_modify_position: handleMt4ModifyPosition,
  mt4_get_quote: handleMt4GetQuote,
};
