…computers don’t “restart automatically”. There are three things I can think of that might force a restart.
1. Did you have “download and install updates automatically” checked in your Software Update settings? It typically does this at night, but it could happen at any time.
2. Were you manually installing a software update while watching YouTube? This would have restarted your mac.
3. Did you have a lot of other things running in the background during this time? It’s unlikely, but your mac might have crashed.
Cheers,
Macnerd01