For the last three years, the Turkish president has been methodically moving to take over the nodes of power.

What happens in Turkey matters. It is a G20 economy in a sensitive part of the world, sharing borders with Iraq, Iran and Syria. Turkey is an asset to its Nato partners when it is able to exercise a leadership role. It can be a liability when its own problems – like the tension with its Kurdish population – spill over those frontiers. And it can be a millstone around the world’s neck when it decides, as it did on Friday, to self-harm.

The coup attempt that night was, by any account, a cack-handed affair. It was an attempt to grab the reins of a complex society with the almost quaintly antediluvian tactics of seizing the state television station and rolling some tanks on to the streets. It is as if the plotters had never heard of social media, while the Turkish president himself to addressed his supporters via FaceTime, urging them out on the streets. Crowds played chicken with the putschists, betting they would return to their barracks rather than have the streets run red with blood. Even then, at least 180 people – civilians, police and coup makers – died.

Indeed, the question is less why the coup failed than why it was ever carried out. If it had an air of amateur desperation, it is because its perpetrators probably assumed that this was their last chance to stop the government of President Recep Tayyip Erdoğan from getting the military completely under its control. At the beginning of August, the military high council will meet, as it does every year, to consider who gets promoted, retired or pushed aside. In the last few days, the pro-government press has been more than hinting that a spring cleaning of the ranks is long overdue.

Indeed, many would argue that Turkey was already in the throes of a slow motion coup d’état, not by the military but by Erdoğan himself. For the last three years, he has been moving, and methodically, to take over the nodes of power.

The pressures on the media have been well documented, as the country slides in international ratings by organisations such as Freedom House, from partly free to not free at all. Opposition newspapers have been taken over by court-appointed administrators. Dissident television stations have had the plug pulled from satellites; digital platforms are no longer seen in people’s homes. Erdoğan curses the very social media which this weekend helped to save his skin.

Increasingly, the government has put the judiciary under its thumb. It is now a brave judge who rules in a way he knows will give official offence. So while the Turkish parliament congratulated itself on a long night’s defence of democracy, many wonder why its members connived in the decline of the rule of law.

And still Erdoğan craves greater authority. Last May, he discarded one prime minister in favour of another more sympathetic to his plans to change the parliamentary system into a strong executive presidency. When the coup plotters stand trial, they may suffer the additional indignation of seeing their attempts to put Erdoğan in his place backfire, by providing a mandate for such increased powers. The president has already promised a purge of those still connected to the exiled dissident cleric Fethullah Gülen – Erdoğanspeak for anyone who opposes his will.

To the outside world, this spectacle should cause dismay. Turkish ambitions to project power, to assist in the fight against Islamic State, to help forge a settlement in Syria will be much harder to realise if the government is at war with its own military and the army at war with itself. A Turkey that governs through consensus is the more valuable ally. The Turkish economy, too, will be more buoyant if relieved of the weight of political risk.

The lesson of the failed coup is that Turkey needs a leader who can bring different sides of a divided society together – or at the very least, one who is willing to try.