An observation: Bluetooth has evolved significantly over the years and Bluetooth 5 in current Macs is significantly more reliable than the Bluetooth 4.2 in your 2017 iMac. However, my long standing trick to encourage bluetooth device connections is to switch the device off for a few seconds, and then back on, and if that doesn’t work, plugging unplugging the device.
As far as charging too long goes, that is a recommendation that has gone back and forth over the years but as far as I know it is not an official Apple recommendation for current devices one way or the other. NOTE: that is current for currently sold devices and could be different for older devices. (Did I waffle that enough to make it totally indecisive?) Personally I have never worried about it and although I generally run my Magic keyboard, trackpad, & mouse cordless, they sometimes get plugged in for days on end and have never shown any ill effect.