FOR nine years Turkey's prime minister Recep Tayyip Erdogan has moved smoothly from one victory to another, winning three elections in a row with a bigger share of the vote each time. He has seen off coup plots by once-omnipotent generals and attempts by their cronies in the judiciary to ban his mildly Islamist Justice and Development (AK) party. So far the economy has survived the financial crisis largely unscathed. And although membership talks with the European Union are stuck, relations with America are (in the words of the foreign minister, Ahmet Davutoglu, who recently spent five hours with Hillary Clinton) in “a golden age”.

The new philosophy in a bureaucracy once steeped in corruption and sloth is that the state exists to serve the citizen and not the other way round. The old maxim that “the Turk's only friend is a Turk” has been replaced by growing confidence in Turkey's regional clout. Mr Erdogan's rivals are riven by internal feuds. A recent opinion poll suggests that if a new election were held today AK would get 54% of the vote, four points more than in 2011.

But the picture is less rosy when you consider a nasty power struggle between Mr Erdogan and Turkey's most influential Islamist movement, led by an imam living in Pennsylvania named Fethullah Gulen. Commanding a sprawling global empire of media outlets, businesses and schools, the so-called Gulenists, who mix piety with hard-nosed pragmatism, are said to have infiltrated the judiciary and the police force. Their next target was apparently Turkey's MIT spy agency. This story gained credence this month when an allegedly pro-Gulenist prosecutor summoned the MIT chief, Hakan Fidan, an Erdogan protégé, for questioning in a case against the separatist Kurdistan Workers' Party (PKK), amid claims that some of his men may have joined its ranks.

A furious Mr Erdogan responded by getting the AK-dominated parliament to pass a law that makes judicial interrogation of MIT officials subject to prime ministerial consent. The offending prosecutor was taken off the case, and a group of suspected pro-Gulenist officials in the Istanbul police force were reassigned.

Although Mr Erdogan appears to have won the first round, the breach may yet have a big effect on his political fortunes, because the Gulenists may withdraw their support. The affair is complicated by Mr Erdogan's health. He has recently had two operations, amid persistent rumours that he is being treated for colon cancer. He and his doctors deny this. But would-be successors within AK are said to be switching to the Gulenists. It is surely in both sides' interest to make peace.

The Gulenists and AK had been making common cause against the army. Gulen-affiliated newspapers brimmed with leaked documents exposing army mischief that have been used as evidence in the Ergenekon trial against alleged coup-plotters. But with hundreds of officers behind bars and the threat of a coup dispelled, the alliance has frayed. Some argue that this reflects policy differences. More probably it is about power, with the Gulenists (in Mr Erdogan's view) wanting too much. Even before the MIT row, the prime minister was said to be concerned about the arrest of journalists, which damages Turkey's image. There are now at least 70 in jail, mostly on thinly supported terrorism charges. Some of them, notably Ahmet Sik and Nedim Sener, had been highly critical of the Gulenists.

Yet Mr Erdogan's own democratic credentials are not so shiny. Hundreds of students are on trial or in prison for such “crimes” as protesting against dam projects. Anti-government journalists have been sacked by media bosses fearful of jeopardising other business interests. The most recent was Nuray Mert, a columnist for Milliyet, an establishment daily, and a fierce critic of Mr Erdogan's decision to opt for a military solution to the Kurdish problem. This took a tragic turn in December when Turkish warplanes mistakenly bombed a group of Kurdish smugglers along the Iraqi border, killing 34 Kurds, mostly teenagers. As Kemal Kilicdaroglu, leader of the main opposition Republican People's Party, also points out, Mr Erdogan has been quick to pass legislation to protect his spy chief but has done nothing to amend vaguely worded anti-terror laws used to jail thousands of dissidents, including nine MPs.

Equally worrying is the lack of progress on the new constitution that Mr Erdogan vowed would crown his third term. A parliamentary committee meant to be drawing up a draft has done such groundbreaking things as inviting the Greek Orthodox patriarch, Bartholemew I, to air his views. But there are still concerns about Mr Erdogan's ambition to create a strong presidency, presumably designed for himself. And it is hard to see the whole process being completed without consent from the largest Kurdish party. Mr Erdogan is unwilling to talk to it unless it publicly disavows the PKK. Never mind that his own men, led by Mr Fidan, were secretly negotiating with the PKK until last summer, when it escalated its campaign of violence.

The descent of Syria, Turkey's southern neighbour, into civil war is another concern. After years of cultivating the Syrian president, Bashar Assad, the Turks are betting on the collapse of his regime. They have offered sanctuary to commanders of the rebel Free Syrian Army and other members of Syria's opposition. Mr Davutoglu is said to have lobbied Mrs Clinton to intervene but, especially in an election year, the Americans will be cautious. The longer Mr Assad hangs on, the greater the risk of his turning against Turkey. Resuming his father's support for the PKK or stoking unrest among Turkey's small Alawite population may both be options.

And then there are worries over the economy. By the standards of its Greek neighbour, Turkey looks scintillating: a budget deficit under 2% of GDP, a public debt of only 40% and GDP growth in 2011 of almost 8%. Yet a current-account deficit of over 10% of GDP points to overheating, and the economy is now slowing sharply. Mr Erdogan's next fights may prove to be his toughest yet.