Imagine for a moment you saw yourself as a "model for the world" like the Turkish prime minister, Recep Tayyip Erdoğan, who recently not only retained the world titles for locking up journalists and ordering Google to take down web pages, but also declared yet again that he would not rest until Turkey was a "top 10 democracy".

So what would you do if half of your inner circle of ministers and their sons were implicated in the biggest corruption scandal in Turkish history, accused among other things of taking bribes of tens of millions to ignore to billions of dollars of dodgy dealings?

Would you – as Erdoğan did – fire the police who uncovered the corruption; threaten to jail judges and curb their powers; bring in new prosecutors who are relaxed about people keeping millions in cash in shoes boxes, and order that henceforth police must tell ministers if they are thinking of investigating them, so they can tidy their shoe boxes away?

And would you also claim, like him, that "dark forces" in the same judiciary and police that you recently used to lock up your enemies, were plotting to assassinate you?

In a final flourish, Erdoğan allowed his interior minister not only to pick off the detectives investigating the minister and his son, but also to get rid of 70 police chiefs and 580 other officers in six days, while an equal number of Erdoğan supporters were rewarded with their jobs. The new police chiefs' first act was to refuse to investigate fresh corruption cases, one of which allegedly involves Erdoğan's son, Bilal.

Yet until the last few days, Erdoğan seemed as immune as ever to the normal rules of political gravity. This was a leader who not only remained after the deaths of young people during the Gezi protests this summer, but also persuaded a large section of the Turkish public that a crisis he himself had created was actually an international conspiracy against him by the EU, the US, Lufthansa – yes Lufthansa – and of course, Israel.

This time, however, Erdoğan appears to have been undermined by a fatal moment of sanity. After seven days of sacking and shredding, he finally asked four ministers to step down. One refused to go – and said that Erdogan himself should also resign.

In an ideal world the scandal would have been exposed by a fearless cadre of impartial prosecutors. The truth is more complicated. Almost certainly nothing would have come to light if Erdoğan had not crossed his most important former ally, the exiled spiritual leader Fethullah Gülen. The head of a worldwide movement dedicated to interfaith dialogue and reconciling Islam with science, Gülen's followers run a network of schools and media outlets including Turkey's biggest selling newspaper, Zaman.

Gülenists are widely believed to have infiltrated intelligence and the special courts, which were crucial to breaking the malign power of Turkey's coup-happy military by exposing alleged plots against Erdoğan and his moderately Islamist AK party, who were once seen as Turkey's great hope for reform. Awkward questions, however, began to surface when journalists who pried into the movement were rounded up with the ultra-secularist generals. As he was carted off to prison, one investigative reporter, Ahmet Sik, famously cried: "Touch them and you burn."

It is now Erdoğan who is feeling the heat after he threatened to close Gülen's schools. He was convinced the Gülenists were plotting to rid of him and afraid his authoritarianism would destroy the AK party if he assumed Putinesque presidential powers after a referendum later this year.

The first wave of corruption inquiries stopped just short of Erdoğan's own family, whose spectacular enrichment was one of the sparks of the Gezi protests. However, the usually circumspect Gülen delivered the lowest blow personally, saying he once warned a powerful politician of the dangers of his relationship with "a promiscuous woman". Ever the macho, Erdoğan threatened to "break the hands of the traitors" and declared a "new war of independence" against foreign interference (Gülen is exiled in the US). While some secularists revel in the battle, far more in Turkey are terrified at the damage that might be done by Erdoğan's fury.

Turkey has made huge strides in the past decade. Erdoğan and Gülen will be thanked by history for neutering the military, despite the questionable methods used. All that and more is now at risk as these two ill and wounded men go all out to destroy each other — the latest salvo being Erdoğan's use of the military yesterday to allege that it was Gülen, and Gülen alone, who was behind a plot against them. Oh the irony.

There was once much good in Erdoğan, a hardman footballer turned self-styled national saviour – and his fall is a great political tragedy. He promised Turks and Kurds peace, freedom and prosperity, and for a while seemed capable of delivering this. But he will leave a legacy of fear, censorship and corruption.

Now as he winds himself in the rhetoric of martyrdom and conspiracy, Erdoğan has one last chance to redeem himself in the manner of his going. Turkish history, however, is not littered with many edifying precedents.

Semi-secret organisations such as Gülen's Hizmet are not ideal champions of transparent democracy, particularly in a country cursed since Ottoman times by the unseen hand of masonic fraternities and a notorious "deep state". Like the military, they too must be tackled if real democracy is ever to thrive. But for now, with no opposition worthy of the name, and a civil society not yet strong enough to count, they are all Turkey has got.