Yeah as others have pointed out, Stalin was many things, a complicated man, a ruthless dictator, he got none of it being an idiot.
1. I've noticed for a while now that you consider Stalin's purges of the Red Army to be the height of idiocy, though you have repeatedly been taken to task for it by myself and others here on the site. While the purges did kill some genuinely skilled people like Tukhachevsky, you can't neglect the fact that the Red Army was swollen with people who were politically untrustworthy, Stalin's choice comes down to securing his power base that would eventually allow him to be the undisputed director of the entire Soviet war effort later on down the line by purging everyone of a questionable allegiance in the Red Army (and that's a lot when you factor in how many officers participated in exchange programs with Germany, had loyalties to Stalin's rivals, or some combination of the two) or leaving the officer corps around and quite possibly making the military a bastion of resistance to Stalin's rule... generally when the people with the biggest guns don't support a ruler it's bad for stability.
This is not to say that no mistakes were made in the purges or in their aftermath. Budyonny should never have gotten his thirty cavalry divisions, someone should have listened to Tukhachevsky, and Voroshilov should never have been let anywhere near the planning table for the Winter War (keep in mind that initially horrendous Soviet performances turned right around when Timoshenko was put in charge).
So in short, purges =/= 100% unjustified and bad.
2. Barbarossa preparations, Ivan got this one better than I did but I'll touch on it briefly for the sake of posterity:
A. Stalin knew probably better than anyone that Hitler's word wasn't worth... to use a Calbearism, the ink it was printed on let alone the paper. Only blustering dolts like Molotov wanted the Soviet Union to actually join or make any kind of long-term rapprochement with the Axis, Stalin knew better, but signed the treaty to keep Germany off of the USSR's back as well as to facilitate Soviet territorial goals, the Soviets wouldn't have had a head-start in Poland without the Molotov-Ribbentrop Pact and wouldn't have been able to pull something like the annexation of the Baltic States either.
B. Sorge had made a bad guess on Barbarossa's start date, the Soviets were ready for an attack on the 19th but stood down by the time of the real attack on the 21st, Stalin had less reason to suspect that the attack on the 21st was genuine. In any case, he also believed that a preliminary mobilization was part of a ploy to make him the aggressor by Hitler... not at all an unreasonable fear: Stalin mobilizes, Hitler declares war citing that the Bolsheviks plan to overrun Europe, and Stalin would have been playing right into his hands.
3. He knew how to listen to his generals when the time was right, this was a skill that escaped most if not all of his Axis enemies, and to a lesser degree some of his allies, or rather, Allies. People like Zhukov and Timoshenko deserve every bit of their reputation as some of the best generals of Russian/Soviet history and as some of the best WWII generals. At the end of the day, Stalin was their boss, could have had either of them executed for recommending something he didn't personally support, or just never listened to them the way Hitler tended to do as the war went on, but he didn't, that says a lot.