A mix of conditions and enemy state. You can set an enemy state with a variable and based on this decide what action it will perform. Then you change this state based on factors, whether it chooses random actions or it does something when attacked etc.