iMac goes to sleep because the battery has 0% left
I would recommend performing a re-install of macOS. This does not require that you delete your existing data and should be non-destructive. However, backups are always good to have.
Reboot into recovery mode (instructions are available on Apple's Website). Ensure that the installer shown in recovery mode is for Mojave (if not, then the system may be updated or downgraded when you do this!). Run the Install macOS option in Recovery mode and choose your existing disk.
It will install macOS on top of your disk, but leave your data in place.