Messages suddenly won't connect on iMac
2019 iMac running Monterey. Everything was working fine until this past Saturday. Suddenly, Messages stopped working just on the Mac. Next time I was on the desktop, it asked me to sign in with my Apple ID, but when I did it gave me a spinning wheel that went on and on until I finally had "an unknown error has occurred" message. The weird part is that after that when I open preferences, it shows my Apple ID as logged in, but there is a spinning wheel next to "Sign Out." If I click that it doesn't do anything (just continues to show the progress wheel), but when I restart Messages it has signed out and prompts me to login with my Apple ID. Wash, rinse, repeat. Basically I have to force quit the app to see if I am logged in, but even if I appear to be logged in, it isn't actually communicating with a server or syncing messages.
I have tried several things and keep rebooting to see if they work, but to no avail. The weird part is that when I restart the system doesn't give me any error messages or other issues, but when I login after the Mac reboots, I get a system message that says "Your computer restarted because of an error" every time, even though I have restarted it three times now and don't have any other software open. Help?
(Messages and iCloud are working fine on my phone.)
iMac 21.5″, macOS 12.5