The attack on Istanbul's main airport has underlined President Recep Tayyip Erdogan's increased weakness, a vulnerability that's a product of the actions of Turkey's allies and opponents alike. But it's partly Erdogan's own doing.
From the beginning of the Syrian uprising, Erdogan has used the conflict as an opportunity to crack down on the Kurdish Workers Party (PKK), a militant group that has been battling the Turkish state for decades and is listed by NATO, the U.S. and the EU as a terrorist organization.
The government stoked the fires of Kurdish grievances, and the PKK returned the favor in-kind -- ratcheting up its terror attacks on the Turkish state, mainly against security institutions like the police, which have increased in number and frequency over the past five years.
But the attacks also benefited Erdogan, who used the attacks to present himself as the only person able to secure Turkey from terrorism.
The tactic worked for a while and enabled Erdogan to use the fear of instability as a rallying tool to secure a sweeping victory for his party in the last parliamentary election.
But the focus on the PKK also meant diverting security resources that could have been deployed to gather intelligence on and prevent attacks by another cause of instability -- ISIS, whose fighters are believed by some Turkish officials to have been behind this week's airport massacre.
At the same time, Turkey saw ISIS as a convenient tool to help overthrow the Syrian regime of Bashar al-Assad, particularly as some elements in the Turkish administration have been sympathetic towards the group. This facilitated the movement of ISIS fighters in and out of Syria through Turkey's porous borders.
This pragmatic stance towards ISIS initially offered Erdogan a way to assert himself vis-à-vis Assad, but it also paved the way for ISIS to strengthen its presence and activities in Turkey. Turkey was held hostage by fears that a crackdown on ISIS would result in serious retaliation on Turkish soil.
This laissez-faire stance on ISIS also eventually put Erdogan under dual pressure -- from the U.S. on one side, and Russia on the other.
The White House pushed Turkey to collaborate with the international anti-ISIS coalition -- something which Turkey was reluctant to do, partly because the government regarded the coalition as casting aside the matter of regime change in Syria. But Turkey's participation in the coalition meant that ISIS now saw Turkey as an opponent, and the terror group began unleashing attacks in the country.
Russia, on the other hand, saw the Obama administration's lukewarm approach towards regime change in Syria an opportunity to affirm its own political standing towards the West.
With Turkey seen as an economic and political competitor, Moscow set its sights on Ankara and engaged in a series of military provocations out of its base in Syria that eventually led in November to the downing of a Russian fighter jet that had trespassed into Turkish air space.
Rather predictably, Erdogan handled Moscow's anger over the incident with defiance. But the U.S. and Russia were both quick to support rising Kurdish groups in northern Syria on the basis that those groups were fighting ISIS. As those groups have direct connections with the PKK, Erdogan found himself stuck between the U.S. on one side and Russia on the other.
Having already severed diplomatic relations with Israel in 2010 in the wake of the Gaza flotilla crisis, Erdogan was becoming increasingly isolated in the international community. Even the new cooperation with Saudi Arabia over Syria proved to be a bad bet as the Kingdom itself complained about being let down by its American ally on Syria -- as well as on Iran, following the nuclear deal.
Without a strong regional backbone and with pressure from both the West and the East -- as well as with a growing terrorist problem at home from both the PKK and ISIS -- Erdogan was pushed to compromise, restoring diplomatic relations with Israel and patching things up with Russian President Vladimir Putin over the downed fighter jet.
But instead of appearing as a shrewd politician, those compromises made Erdogan appear weak. This has played right into the hands of ISIS, which had been looking for a spectacular way to mark the second anniversary of the establishment of its so-called "caliphate."
At a time when ISIS is facing increasing pressure in Syria and Iraq, Erdogan's vulnerability provided the terrorist organization with a golden opportunity to compensate for its losses. Whether Erdogan will be able to overcome it remains to be seen.
Lina Khatib is the Head of the Middle East and North Africa Programme at Chatham House. Previously she was the director of the Carnegie Middle East Center in Beirut and the co-founding head of the Program on Arab Reform and Democracy at Stanford University's Center on Democracy, Development, and the Rule of Law. The opinions expressed here are her own.