2D Rotation collision problem

Doesn’t need an explanation, you can see everything on this gif
I want the object to stop rotation after collision, but still be able to rotate it back when key is pressed.
Object has PolygonCollider2D and Rigidbody2D with freezed position X and Y

     Vector2 mainPos;
     private void Start()
     { 
         mainPos = transform.position;
     }
     void Update()
     {
         transform.position = mainPos;
         if(Input.GetKey(KeyCode.D))
         {
             transform.Rotate(Vector3.back * 150f * Time.deltaTime);
         }
         else if(Input.GetKey(KeyCode.A))
         {
             transform.Rotate(Vector3.back * -150f * Time.deltaTime);
         }
     }

Try using Rigidbody2D.MoveRotation() instead of Transform.Rotate().

As well as Rigidbody2D.MovePosition() instead of changing the Transform.Position.