What was the rationale of subjecting Job to pain and misery though he was a pious man of G-d. Please explain with logical reasons.
here's one reason from Rabeinu Bachye in Chovos Halevavos gate 4.
If one asks: Behold we see some tzadikim (very righteous people) which do not receive their livelihood except after hard and strenuous toil, while many transgressors are at ease, living a good, pleasant life? We will say: The prophets and the chasidim already investigated this matter. One of them said "[Righteous are you, O L-rd, when I plead with You: yet let me talk with You of your judgments:] Why does the way of the wicked prosper? why are all they happy that deal very treacherously?" (Yirmiyahu 12:1), and another "Why do You show me iniquity and look upon mischief; and plunder and violence are before me; and the one who bears quarrel and strife endures." (Chavakuk 1:3), and "for a wicked man surrounds the righteous; therefore, justice emerges perverted." (Chavakuk 1:4), and "Why should You be silent when a wicked man swallows up one more righteous than he?" (Chavakuk 1:13), and another one said "Behold these are wicked, yet they are tranquil in the world and have increased wealth." (Tehilim 73:12), and "But for naught I cleansed my heart and bathed my hands with cleanliness" (Tehilim 73:13), and another said "And now we praise the bold transgressors, those who work wickedness are built up, they tempt G-d, and they have, nevertheless, escaped." (Malachi 3:15), and many more like this.
But the prophet refrained from giving an answer because each specific case has its own particular reason . Therefore Moshe Rabeinu commented on this in the torah saying (Devarim 29:28) "the hidden things belong to G-d"..and the wise man said in connection to this "If you see the oppression of the poor, and perverting of justice and righteousness in a province, marvel not at the matter" (Koheles 5:7), and the verse says: "the Rock, His deeds are perfect for all His ways are justice" (Devarim 32:4).
WHY THE RIGHTEOUS SOMETIMES SUFFER
Nevertheless, I saw fitting to attempt to clarify this matter that should be to some extent satisfactory . The possible reasons why a tzadik is prevented from obtaining his livelihood without effort and must instead exert himself for it and be tested by it is as follows.
A previous sin for which it is necessary to pay him for it, as written "the tzadik will pay in the land" (Mishlei 11:31)
In the way of exchanging, to pay him more good in Olam Haba (the afterlife), as written "to benefit you in your end" (Devarim 8:16)
3. To demonstrate his good bearing and good acceptance of suffering in the service of G-d, so that others will learn from him, as you know from the matter of Job.
Due to the wickedness of his generation, G-d tests him with poverty, hardship, or sickness to demonstrate/contrast his piety and service of G-d unlike them, as written "Indeed, he bore our illnesses, and our pains he carried them" (Yeshaya 53:4).
Due to his not being sufficiently zealous in standing up for G-d, and exacting justice from men of his generation, as you know from the story of Eli and his sons, as the verse says "And it will be that everyone who is left in your house, will come to prostrate himself before him for a silver piece and a morsel of bread" (Shmuel 2:36).
commentaries there on #3
Marpe Lenefesh: That he receives everything with a good countenance, and he does not rebel in sufferings so that others will learn from him and see him and notice him and desire to serve G-d even though they don't have all their lusts, and even though they are in poverty, hardship or in painful sicknesses as we find with Iyov (Job) and many other sages, who were in suffering yet did not stop from torah and service, and as is known of Hillel who was extremely poor, and others who needed to labor for their livelihoods and nevertheless studied the torah.
Pas Lechem: To show the world that the Tzadik bears the bad of this world, with good patience and a good countenance, and despite his suffering, he does not budge from the service of his Creator in order to ease the bad. Unlike the wicked, that even for something he is forced to bear, he bears it with an angry heart, and therefore he throws off the yoke of service in order to ease his troubles.
Tov Halevanon: He bears his sufferings and is not begrudging towards G-d and accepts them with a good countenance
You're assuming that Job was a completely pious individual. The Talmud seems to think otherwise.
א"ר חייא בר אבא א"ר סימאי שלשה היו באותה עצה בלעם ואיוב ויתרו
Rabbi Ḥiyya bar Abba says that Rabbi Simai says: Three noteworthy people were consulted by Pharaoh in that counsel where Pharaoh questioned what should be done with the Jewish people. They were Balaam, and Job, and Yitro.
בלעם שיעץ נהרג איוב ששתק נידון ביסורין יתרו שברח זכו מבני בניו שישבו בלשכת הגזית שנאמר (דברי הימים א ב, נה) ומשפחות סופרים יושבי יעבץ תרעתים שמעתים סוכתים המה הקנים הבאים מחמת אבי בית רכב וכתיב (שופטים א, טז) ובני קיני חתן משה וגו'
Rabbi Ḥiyya bar Abba teaches what occurred to each of them: Balaam, who advised Pharaoh to kill all sons born to the Jewish people, was punished by being killed in the war with Midian (see Numbers 31:8). Job, who was silent and neither advised nor protested, was punished by suffering, as detailed in the eponymous book in the Bible. Yitro, who ran away as a sign of protest, merited that some of his children’s children sat in the Sanhedrin in the Chamber of Hewn Stone, as it is stated: “And the families of scribes who dwelt at Jabez, Tirathites, Shimeathites, and Sucathites, these were the Kenites who descended from Hammath, the father of the house of Rechab” (I Chronicles 2:55). And it is written: “The children of the Kenite, Moses’ father-in-law” (Judges 1:16). This teaches that the Kenites, descendants of Yitro, the father-in-law of Moses, dwelt at Jabez [Yabetz], referring to the place where the Jewish people go for advice [eitza], i.e., the Chamber of Hewn Stone.
What these commentators seem to overlook or address is what we find in the opening of Job: This good man's misery is instigated entirely from a bet, if you will, between G-d and the Accuser (Satan). Were there verses that communicated HaShem's intention to use Job as an important lesson for others that we must all bear the miseries of life with continued faith despite those miseries, or some such lesson, I could be more satisfied. Instead the book tells us that HaShem entered into this wager simply to satisfy the Accuser's accusation. HaShem's treatment of Job reminds me of a passage in Shakespeare's KING LEAR: "As flies to wanton boys are we to the gods: They kill us for their sport."