When the Iranian president, Hassan Rouhani, is sworn in again on Saturday, the EU’s foreign policy chief Federica Mogherini and ministers from Britain, France and Germany will be in Tehran to watch; an indication of how far relations with the west warmed in his first term. Yet as he embarks upon his second, he may feel the chill. Despite defeating his conservative rival by a landslide in May’s elections, opposition is ranged against him at home and abroad. The great domestic uncertainty he faces – supreme leader Ayatollah Ali Khamenei is 78 and without a clear successor – is for now overshadowed by Donald Trump’s threat to pull out of the landmark nuclear deal signed in 2015.

The International Atomic Energy Agency, which monitors its implementation, says Iran is complying with the requirements to curb its nuclear programme and accept inspections in return for sanctions relief. But Mr Trump has vowed to overturn the Obama administration’s stand-out foreign policy achievement. He has twice signed the sanctions waiver, but with extreme reluctance. He has asked aides to find a way to ditch the deal and says he expects Iran to be declared non-compliant next month. Officials say it has breached the pact “in spirit”.

Mr Rouhani promised that the agreement would bring prosperity. Iran’s economy is growing again; he has slashed inflation and stabilised the currency. But poverty has risen, a quarter of young people are unemployed, and foreign investment remains well below projections. Iranians have not yet felt the benefits they expected. Alongside the economic dissatisfaction run other concerns. Younger people especially want reform, and to see more women in government, and better links with the outside world. Meanwhile, the president’s hardline opponents, including the Revolutionary Guard and the judiciary, have portrayed the deal as a capitulation for little reward – not least, because it challenges their vested interests. They sense opportunity; note the recent detention of Mr Rouhani’s brother. Iranian presidents are usually weakened in their second term and Ayatollah Ali Khamenei has fired a shot across the president’s bows by implying a parallel with Abolhassan Banisadr, a reformist removed from office early.

For now, Iran’s elite seems to be playing down divisions. That may make it harder for the US to push Tehran into abandoning the agreement, as Mr Trump apparently hoped. Iran says the US is breaching it by applying separate sanctions relating to missiles and human rights, but does not want to walk away. European parties to the deal want to shore it up; France’s Total signed a multibillion-dollar gas deal with Iran last month. But should the US pull out, secondary sanctions would hobble foreign companies seeking to do business with Iran.

Iran has much to answer for; most of all, assisting Bashar al-Assad’s crimes against his people in Syria. The country’s grim human rights record at home is further tarnished by the deteriorating health of opposition leaders Mehdi Karroubi and Mir Hossein Mousavi, held under house arrest since the popular protests of 2011. Its elections are anything but free and fair – yet they are competitive, meaningful and much more than most US allies in the region offer. No one could deny that Mr Rouhani is a very different president to his predecessor Mahmoud Ahmadinejad. Yet Mr Trump greeted his re-election by throwing US weight more fully behind Saudi Arabia in its struggle with Iran for regional hegemony.

American hostility can only bolster the isolationist, hardline forces ranged against Mr Rouhani; against the wishes and instincts of the Iranian people; against stability in the region and indeed against the interests of the US. Even defence secretary James Mattis – who has defined the three gravest threats facing the US as “Iran, Iran, Iran” – is among those pressing to maintain the deal. The president he serves calls the agreement a disaster. But Mr Trump is, as usual, wrong. It is scrapping it that could court catastrophe.