The whole purpose of creation is for God to give, which is a result of His grace, and the problems and hardships this world are accompanied with are in no contradiction with this purpose because first of all, the world we live in is a material one and one of the characteristics of material, is clash and conflict and imbalances and inequalities that we call “bad” things. Second of all, all “bad” things have been created because of the good sides to them which is dominant to their bad sides. Third of all, bad things have many benefits like:
1- They show the beauties of this world and as long as good and bad don’t face each other, we can't distinguish between the two.
2- Hardship has a training effect on the individual and society. Hardship and blessing are two proportional concepts, depending on how one experiencing them sees and makes use of them, something considered as a hardship by others can be seen as a blessing and vice versa. Allah (swt) has given His apostles hardship because development is a result of going through hardship and is how man reaches moral perfection, causing him to be rewarded by Allah (swt).
The whole purpose of creation is the benefit of creatures themselves  and according to Imam Sadiq (as), God’s grace is the sole purpose for the creation of this universe.  Hardship and tragedies which we consider bad things are in no way in contradiction with this purpose because first of all, these things are the inevitable result of a material world. One of the characteristics of a material world is clash and interference. Therefore, there is no way that one can stay away from such hardships, unless the world just doesn’t get created in its entirety which in the case of Allah (swt) not creating this world, many of the creatures who are capable of dominating the “bad” won't be created, and this is in contradiction with His wisdom. 
Secondly, Allah’s reason for creating things that we consider bad, is their good side. For instance, the sun has many benefits that are the main cause of Allah creating it, although if someone stands under the sun for a long time will definitely be harmed by it. Another example is fire, which has numerous benefits such as the heat it makes for man, saving him from cold weather, but as you know, it can also be very dangerous, burning both good and bad people. The same goes for tragedies that we might consider completely bad. Therefore, what is intended by creating things that might seem to have a bad side to them, is their good side. 
Thirdly, things that we might consider “bad”, are usually of many benefits like:
1- Badness and ugliness are the ones that show us what true beauty is, if it wasn’t for us having bad things in this world, we wouldn’t be able to comprehend the beauties of the world. If we were to live in a world of only beautiful people, no one would ever seem beautiful in our opinion, and the same goes if we lived in a world full of ugly people, no one would seem ugly anymore. Also, if Mu’awiyyah was never to exist, no one would ever recognize Imam Ali’s virtues and everyone would take him for granted.
The reason why people are pulled and attracted to beauties is because they see ugliness and dislike it. In reality, the attraction we have towards beauties, is powered by our repulsion regarding ugliness, therefore one of the benefits of “bad” things is that they show us the beauties. 
2- Hardships and tragedies are a prerequisite to prosperity and happiness and are actually their cause. Bliss and harmony hide behind hardship, just as sometimes hardship is hidden behind happiness. This is one of the laws and formulas of the material world. The Holy Quran says: “Allah merges the night into the day and merges the day into the night”  , as if darkness is a result of light and that light is the result of darkness. 
The famous German philosopher, Hegel, says that quarrel and badness aren’t negative matters that come from one’s imagination, on the contrary they are true and realistic matters and are philosophically considered steps of perfection and goodness. Disputes and differences in law are actually the development of law, and virtues perfect through the hardships of this world and it is only through these hardships, responsibilities and sufferings that one can reach high perfection. Pain and suffering are all accepted issues that are signs of life and push towards development and perfection. 
In his letter to Uthman ibn Hunayf, the governor of Basra, Imam Ali (as) says that living in comfort and ease and staying away from hardship makes one weak, and on the other hand, living under hard conditions makes one strong and lively. 
“Bad” things and deprivation are a must for things to complete and reach perfection; if it wasn’t for enmity and rivalry, there wasn’t competition and movement, without war and bloodshed there isn’t development and civilization (not that we fund or support war and bloodshed, yet this is a true reality), therefore one must keep in mind that this world is one of gradualness, perfection and movement from imperfection to high virtue and perfection, and also that all movement and elevation towards perfection isn’t possible without conflict , interference, falsification and abolishment. All of these perfections depend on these so called “bad” things and it is because of this fact that we come upon the true reasoning behind the creation of “bad” things. Another issue that clears up here is that these bad things aren’t absolutely bad, they are partially bad, but as a whole they are good, not bad. 
3- Hardship and pain train the individual and awaken society, they awaken those who are sleeping in ignorance and motivate them. Hardships are like sandpaper that polishes iron and metal; the more they come in contact with an individual, the more he/she is polished and becomes sharper, because the property and characteristic of life is that one has to be ready and stand up and resist hardships. Hardship is like an alchemy that transforms normal things into valuable ones, changing the individual for the better. There are two elixirs of life; love and hardship, and these two cause one to be extraordinary and make shiny and glowing gems out of dull material. 
4- Hardship and blessings are both proportional (meaning that they aren’t absolute and depend on how they are looked at), and sometimes a hardship can turn into a true blessing depending on how the individual reacts to it and if he/she makes good use of it, turning it into an opportunity by having patience and tolerance, but if one chooses not to make use of these hardships, then these hardships will truly be hardships for him/her.
The same goes for worldly blessings; they might be a means of comfort and prosperity for the individual, and can also be a means of sorrow and misery. Neither poverty and hardship are absolutely bad, nor blessings and comfort are absolutely good. In many cases, poverty and hardship result in one’s perfection, turning into a true blessing, while sometimes richness and comfort end in one’s misery and downfall. Therefore, it is true to say that it depends on one’s reaction to different things that take place for him/her for them to be good or bad. The same thing can be good for one person while being bad for another. This is what we mean when we say that good and bad are proportional. 
As for why hardship and tragedy can be found in the lives of the imams and Allah’s apostles:
1- In order for us to develop, Allah (swt) has placed hardship (of course this hardship is tolerable and Allah (swt) never makes intolerable things wajib for us) in both the laws that He has legislated and made mandatory for us to follow, and in the outside world where we live in. He has made different forms of worship wajib upon us such as fasting, Hajj, jihad, giving charity and prayer in which we need to be patient and perform accordingly and thus grow and perfect. In addition to these laws, hunger, fear and the loss of belongings and sometimes even lives are all hardships that take place in this natural world for every person that we have no other choice but to tolerate and be patient with. These hardships are all laws of nature which occur for everyone, regardless of how good or bad they are and Allah’s apostles and prophets aren’t an exception and since they too live in this natural world of ours, they have no other choice but to go by its rules and not to forget that as was mentioned, hardship polishes one’s soul and since Allah’s chosen ones are to be of the highest virtues, they too will be put through hardship because when Allah loves someone, He will put him/her through hardship as the famous saying goes “البلاء للولاء”  . It has been stated in a hadith by Imam Baqir (as) that Allah (swt) looks after His servant and grants him hardship the same way that one brings gifts for one’s family when coming back from a trip.  Imam Sadiq (as) also says: “When Allah (swt) loves His servant, He immerses him in the sea of hardship.” 
Sa’d ibn Tarif reports that he was with Imam Baqir (as). Jamil al-Azraq came in and began complaining about the many problems that the Shia were suffering at that time, asking why the friends of the Ahlul-Bayt have to go through such hardships. Imam Sadiq (as) narrates from Imamm Sajjad (as) that a group of people also came to Imam Husein and Ibn Abbas with the same question and complaints, asking for an answer. Imam Husein (as) answered: “By God, poverty, poorness and murder (being killed by the oppressors) rush to our followers quicker than the running of camels and floodwater to the sea…and if it wasn’t so, we would be sure that you aren’t our true followers.” 
Imam Sadiq (as) states that among all people, the most hardships take place for the prophets, then their successors and followers. 
Hardship resembles the waves of the ocean. Just like how we learn to swim by having a hands on experience with its waves, our growth and development is a result of how much hardship we face and how much we have a hands on experience with it, and that is why when Allah (swt) loves a servant, He gives him hardship in order for him to grow and reach perfection.
2- There is a tight relationship between hardship and great rewards, to the extent that Imam Ali (as) says: “The great reward is always accompanied by the great hardship, so whenever Allah (swt) loves a people and nation, He gives them hardship.” 
Imam Ali (as) also says: “Hardship is a means of discipline for the oppressor, a test for the believer, a means of elevation of ranks for prophets and a means of respect and highness for (Allah’s) apostles. 
Suleiman ibn Khaled reports that Imam Sadiq (as) said that Allah’s believing servant has a high rank in His eyes, and this rank can only be obtained through two ways; through the loss of one’s possessions and hardship in the body. 
In another hadith by Imam Ridha (as), he says: “The sickness of the believer is a means of mercy and purification of one’s sins, while the sickness of the disbeliever is a means of curse and chastisement, and the believer will continue to suffer from sickness until no sin remains for him/her (so that he/she is completely purified before death). 
Concerning the reason behind why Allah’s apostles go through hardship, Imam Ali (as) says in the Nahjul-Balaghah: “If Allah, the Glorified, had placed His sacred House and His great signs among plantations, streams, soft and level plains, plenty of trees, an abundance of fruits, a thick population, close habitats, golden wheat, lush gardens, green land, watered plains, thriving orchards and crowded streets, the amount of recompense would have decreased because of the lightness of the trial. If the foundation on which the House is borne and the stones with which it has been raised had been of green emerald and red rubies, and there had been brightness and effulgence, then this would have lessened the action of doubts in the breasts, would have dismissed the effect of Satan's activity from the hearts, and would have stopped the surging of misgivings in people. But Allah tries His creatures by means of different troubles, wants them to render worship through hardships and involves them in distresses, all in order to extract out vanity from their hearts, to settle down humbleness in their spirits and to make all this an open door for His favours and an easy means for His forgiveness (for their sins).” 
So the reason behind why Allah’s apostles go through hardship is the growth and development of their souls and for them to reach high levels of perfection. 
4- Another point that must be noted regarding why Allah’s apostles go through hardship is that they are to be role models to others, and this matter can only take place if they are role models in all dimensions of life. How can they be role models for those who are living in pain and hardship when they themselves have only lived in comfort and ease, without experiencing any hardship at all?
 Sheikh Tusi, Al-Ightisadul-Hadi, pg. 31.
 Translation of Bidayatul-Ma’ariful-Ilahiyyah work of Murtadha Muttaqi Nijad, vol. 1,
 Abu Ali-Sina, Ilahiyyatul-Shifa’, pg. 117-119.
 Murtadha Mutahhari, Adle Ilahi, pg. 164-166.
 Murtadha Mutahhari, Adle Ilahi, pg. 173-174.
 Tarikhe Falsafeye Vil, translation of Dr. Abbas Zaryab Khu’I, pg. 249-250.
 Nahjul-Balaghah, letter 45.
 Usule Falsafeh va Raveshe Realism, vol. 5, pg. 69; Al-Asfarul-Arba’atul-Aqliyyah, vol. 7, pg. 59-95.
 Mortedha Mutahhari, Adle Ilahi, pg. 181.
 Ibid, pg. 185.
 Ibid, pg. 178-181.
 Kuleini, Usulu-Kafi, vol. 2, pg. 255.
 Muhammad Dashti, Farhange Sokhanane Imam Hosein (as), pg. 145, hadith 91.
 Ibid and Akhlaqe Eslami (translation of the fifteenth volume of Biharul-Anwar), pg. 194.
 ان اشد الناس بلاءً الانبیاء ثم الذین یلونهم ثم الامثل فالامثل Kuleini, Usulul-Kafi, vol. 2, chapter: Shiddatu Ibtila’ul-Mu’min, vol. 1.
 Abdul-Wahed ibn Muhammad Tamimi Amedi, Ghurarul-Hikam wa Durrul-Kalim, vol. 2.
 Mustadrakul-Wasa’il, vol. 2, pg. 438.
 Kuleini, Usulul-Kafi, vol. 2, chapter: Shiddatu Ibtila’ul-Mu’min, hadith 23.
 Majlisi, Biharul-Anwar, vol. 81, pg. 183.
 Nahjul-Balaghah, pg. 389-391, sermon 191.
 For further information, see questions: 2886 (website: 434), 936 (website: 1166), 169 (website: 1244), and 107 (website: 2231).