I'm a little late to this, but look at lines 40-45: if the ai is left and colliding, it is set to face right and then immediately checked again for facing right and colliding - before it's had a chance to move - and set to face left again.
At the end of the first frame where ai is facing left and colliding, both facing_right and facing_left will be true. Then, on the next frame, facing_right is set to false in lines 35-40, and now ai is facing left and colliding again, and the process repeats.
I would get rid of the facing_left and facing_right variables, you already have one variable for the direction - motion. Just use that and test it directly, it will be a lot easier to keep track of what's going on in your code!