To quit Safari instead of closing its window

I have a Safari window on my Mac with multiple tabs. If I press Command-W or click the red Close button instead of pressing Command-Q, the next time I click the Safari icon in the Dock, the tabs are lost. (Yes, I can restore them by clicking “History” > “Reopen Last Closed window” or “Reopen All Windows from Last Session”, but this is too easy to forget when I’m in a hurry, which is my usual state.)

Is there a way (using Automator, Shortcuts, or something else) to automatically quit Safari (as if I’ve pressed Command-Q) once I press Command-W or click the red button? So that I don’t lose tabs anymore.

edit: Sorry, I need to correct my question. I don’t need to quit Safari when I press Command-W (because Command-W is normally used to close the current tab, and I don’t want to change this behavior, of course). I only need to quit Safari when I click the red Close button.

I could think of one way (AppleScript + Keyboard Maestro), but instead, to automatically restore the tabs via the menu as you mentioned, if the tabs are gone. I don’t know if you can change the behavior when you click on the “red button” or if you can hook in to it to do a Cmd + Q instead.