As has been explained to you, ad nauseum, the invasion of Russia was a preemptive strike. Alexander planned war against France as early as 1810. Napoleon decided to strike first. And his aim was not to conquer Russia, but to bring Alexander to the peace table.
Portugal was trading with Great Britain and both France and Spain cooperated in the invasion, Spain with the promise of new territory.
Spain was invaded and occupied because Napoleon discovered that his 'ally' planned to support Prussia against France if the Prussians were successful in 1806.
Spain and Russia were both great mistakes on Napoleon's part, and that has also been noted before more than once.