Spain was a close ally of France and was helping in Napoleon's invasion of Portugal, so why would he invade them? I'm sure it's one of those moments in history like Hitler's invasion of the USSR that seem stupid at first, but when you look closer, there was actually a reason. So what was Napoleon's reason to invade Spain?
This older thread has the answers you're looking for, by /u/kieslowskifan and /u/ONeill94.