import {
  McpToolProvider,
  McpToolDefinition,
  McpToolHandler,
  McpContext,
} from '../interfaces';

/**
 * Customers Tools Service
 * Provides MCP tools for customer management.
 *
 * TODO: Connect to actual CustomersService when available.
 */
export class CustomersToolsService implements McpToolProvider {
  getTools(): McpToolDefinition[] {
    return [
      {
        name: 'search_customers',
        description: 'Busca clientes por nombre, telefono o email',
        category: 'customers',
        parameters: {
          type: 'object',
          properties: {
            query: { type: 'string', description: 'Texto de busqueda' },
            limit: { type: 'number', description: 'Limite de resultados', default: 10 },
          },
          required: ['query'],
        },
        returns: { type: 'array' },
      },
      {
        name: 'get_customer_balance',
        description: 'Obtiene el saldo actual de un cliente',
        category: 'customers',
        parameters: {
          type: 'object',
          properties: {
            customer_id: { type: 'string', format: 'uuid' },
          },
          required: ['customer_id'],
        },
        returns: {
          type: 'object',
          properties: {
            balance: { type: 'number' },
            credit_limit: { type: 'number' },
          },
        },
      },
    ];
  }

  getHandler(toolName: string): McpToolHandler | undefined {
    const handlers: Record<string, McpToolHandler> = {
      search_customers: this.searchCustomers.bind(this),
      get_customer_balance: this.getCustomerBalance.bind(this),
    };
    return handlers[toolName];
  }

  private async searchCustomers(
    params: { query: string; limit?: number },
    context: McpContext
  ): Promise<any[]> {
    // TODO: Connect to actual customers service
    return [
      {
        id: 'customer-1',
        name: 'Juan Perez',
        phone: '+52 55 1234 5678',
        email: 'juan@example.com',
        balance: 500.00,
        credit_limit: 5000.00,
        message: 'Conectar a CustomersService real',
      },
    ];
  }

  private async getCustomerBalance(
    params: { customer_id: string },
    context: McpContext
  ): Promise<any> {
    // TODO: Connect to actual customers service
    return {
      customer_id: params.customer_id,
      customer_name: 'Cliente ejemplo',
      balance: 500.00,
      credit_limit: 5000.00,
      available_credit: 4500.00,
      last_purchase: new Date().toISOString(),
      message: 'Conectar a CustomersService real',
    };
  }
}
