The storm gusting through Northern Ireland could not dampen the cheer on the steps of Stormont on Monday.

“We are very concerned about the weather on your hair, Boris,” Arlene Foster joked with the prime minister as she, Michelle O’Neill, the deputy first minister, and Julian Smith, the secretary of state, smiled for photographs outside the revived assembly.

It was a striking tableau, an expression of progress and optimism in a region unaccustomed to either.

Storm Brendan’s driving wind felt apt. After three years of doldrums, of becalmed and paralysed politics, Stormont was suddenly functional.

A deal had restored Northern Ireland’s power-sharing government on Saturday, installing Foster, the Democratic Unionist party (DUP) leader, as first minister with O’Neill of Sinn Féin governing alongside her as an equal.

Three smaller parties – the Ulster Unionists, the SDLP and Alliance – joined the governing coalition after signing up to the deal brokered by the British and Irish governments.

The 62-page document, entitled New Decade, New Approach, broke a bitter stalemate dating from January 2017 when Sinn Féin withdrew from power-sharing, accusing the DUP of arrogance, bad faith and sleaze.

Three years of polarisation, disillusionment and public service atrophy followed, leaving Stormont mothballed and Northern Ireland a political zombie just as Brexit raised profound constitutional and economic questions.

How the deal was done is a story of missed opportunities, fresh starts, manoeuvring and arm-twisting played out against a political landscape transformed by last month’s general election.

The ringmaster was Smith, the Tory MP for Skipton and Ripon in Yorkshire, who since his appointment last July has provided Northern Ireland with the unfamiliar spectacle of a deft and engaged secretary of state.

He inherited a mess. Relations between the DUP and Sinn Féin were toxic, contaminated by mistrust and disputes over Brexit, an Irish language act and prosecutions of Troubles-era crimes. The 1998 Good Friday agreement and the rapprochement between previous party leaders seemed lost, halcyon days.

The DUP’s apparent leverage over the Conservative government at Westminster insulated senior members from Stormont’s dysfunction. Sinn Féin, meanwhile, sought to leverage Brexit into a referendum on Irish unity.

Civil servants ran Northern Ireland in the vacuum but could not take big decisions, leading to pent-up crises in public services, especially clinics and hospitals. The spectre of direct rule from London, anathema to nationalists, loomed.

Several factors helped Smith break the logjam. The outlines of a deal were already visible from previous, unsuccessful negotiations between Sinn Féin and the DUP. An Irish language act could be fudged, letting both sides claim vindication, and the “petition of concern” – a mechanism that gave each party a veto – could be tweaked.

As pragmatists with same goal, Smith also forged an effective partnership with Simon Coveney, Ireland’s foreign minister. Coveney focused on bringing Sinn Féin and other nationalists to negotiations while Smith focused on the DUP and other unionists.

The election transformed the calculus. Voters fed up with paralysis punished the DUP and Sinn Féin, which each suffered significant drops in support. The Conservative landslide also ended the DUP’s sway at Westminster. Suddenly all roads led to Stormont.

Smith pressured Foster and O’Neill by threatening to call an assembly election, where their parties risked another hiding, if power-sharing was not restored by 13 January.

Smith and Coveney took another gamble by publishing the text of the deal before all parties had signed up to it. In a move that some compared to blackmail, Smith also made a cash injection for public services contingent on a deal.

Last week all five main party leaders signed up to it. Reaching this point was the easy part. The bonhomie on display on Monday will soon be tested. Disputes over public funding and Troubles-era “legacy” investigations loom. And Brexit’s impact on Northern Ireland remains unclear.

Smith, if kept on in Belfast in a pending cabinet reshuffle, will find that in Northern Ireland winds can swiftly turn against you.