I had a similar experience with a Logitech keyboard on my iPad Pro. I finally cleaned the contacts on the bottom go the iPAD with a damp cloth and that seemed to reestablish the connection. I am not overly familiar with how the Apple Keyboard hooks up, but I hope this helps
Be prepared for a new Smart Keyboard.
They aren’t very long lasting.
Apple will replace it I hope, but class the replacement as a repair so they don’t have to give you full warranty on the new one (UK).
I now treat my Smart Keyboard with Kidd gloves and (ha ha) have bought a cover to use until I need the Keyboard at which point I take it from the original box and clip it on.
The Smart Keyboard is weird and works well. Just not for very long.