Sorry, but I don't see where the problem is.
Things didn't go as you planned. Okay, that's par for the course with RPGs. This isn't a fail state. It's literally how the game works. No plan survives contact with the enemy. No, the players/PCs aren't your enemy. That's not what I'm saying.
The PCs nearly TPK'ed. Okay, sometimes that happens. This isn't a fail state. Even if they'd TPK'ed, that's not a fail state. It's a high fantasy game, you can bring those PCs back in a dozen different ways and continue the campaign. Or you can send another group to complete their mission. Etc. TPKs don't end campaigns unless you want them to.
No one did anything wrong. Not you, not the players. Set up the world and run it. That's all you have to do. Prep situations, not solutions. Solutions are for the players to come up with. And definitely don't prep plots. If the PCs decide to fight when they should run, that's on them. If they face-pull another encounter when they're already in the middle of one, that's on them. Either they'll learn or they won't. It's neither your job to punish them or be hard on them, nor is it your job to coddle them or take it easy on them. Moving encounters out of their way because they would have a hard time is just as bad as forcing encounters no matter what choices they make. They're both versions of the quantum ogre.
Sounds like a fun and harrowing experience.