An attempted military coup in NATO member Turkey was foiled over the weekend.

At the time of writing, at least 265 people have been killed and another 1,400 wounded. Thousands of judges have been dismissed or arrested. Tanks shelled the parliament in Ankara and at one point President Recep Tayyip Erdogan was forced to address the nation using FaceTime.

After finally landing at Istanbul’s Atatürk Airport, a serious, if not slightly shaken, Erdogan declared on national TV: “This uprising is a gift from God to us because this will be a reason to cleanse our army.”

He isn’t wasting any time. Since those words were uttered thousands of military personnel have been arrested, including several senior generals.

This weekend’s coup was the fourth since 1960 (the fifth if you count the so-called “post-modern coup” in 1997). In each previous case the military had been successful and democracy was returned to the people.

The Turkish military considers itself to be the guardian of Mustafa Kemal Atatürk’s (the founder of the Republic of Turkey) legacy of a Turkey based on secularism and Western orientation. Consequently, military coups have been a peculiar feature of the Turkish Republic when the country strays from these founding principles.

It is still not clear what motivated the coup plotters, but some reasonable assumptions can be made.

It’s only logical that many in the military are increasingly worried that Erdogan has embraced a more conservative brand of Islam at the expense of state secularism. Recent crackdowns on press freedom by the government have not gone unnoticed either.

It is in America’s interests for Turkey to remain stable and become a productive leader in the region and beyond.

Erdogan has also been accused of flirting with Sunni Islamists for too long in Syria. The increase in Islamic State terror attacks across Turkey, and the seemingly reluctance of Erdogan to respond in a meaningful way, could have played a role in driving the coup.

The recent rapprochement with Russia could have tipped the military over the edge. It has been reported that the Turkish fighter pilot who shot down the Russian jet after illegally entering Turkey’s airspace last year was a participant in the coup.

In many ways this coup was doomed to fail from the beginning. It was a nameless coup with no public leader or mandate. Instead of protecting the citizens and the country’s democratic institutions, the coup plotters attacked civilians and shelled the parliament building. No major military base came under the full control of the plotters and no senior leader of the government was captured. There was no reason for the average Turk to get behind such shambles, and unsurprisingly very few did.

Paradoxically, the coup demonstrated how resilient democracy is in Turkey, at least for now. Like him or not, Erdogan is a democratically elected leader. All the major political parties, including the opposition, signed a joint letter condemning the attempted coup. Many Turks who protested in Istanbul’s Gezi Park in 2013 against Erdogan’s rule were there this weekend protesting against the coup plotters trying to overthrow Erdogan.

Under Erdogan, Turkey has been a contentious partner for the U.S., but it remains an important ally and NATO member. It is in America’s interests for Turkey to remain stable and become a productive leader in the region and beyond.

The fact that an attempted coup took place shows that Turkey is anything but stable. Instead of being a leader in the region, Erdogan will focus a lot of resources and energy at home bringing the plotters to justice. This invites Russia and ISIS, for example, to take advantage of the situation. This would be bad for Turkey and the U.S.

The coup might not have lasted long, but the fallout will be felt for years. Erdogan will use the coup to consolidate even more power. The political landscape in Turkey will be fundamentally changed. But if Erdogan responds to the coup by completely abandoning Turkey’s democratic principles, he will be planting the seed for the next coup.

The choices Erdogan makes now will impact Turkey for a generation.