TO NATO officials, it must have seemed like a bad joke. Earlier this autumn, Turkey’s state-run news agency published an infographic on the S-400 missile-defence system, which President Recep Tayyip Erdogan’s government is buying from Russia. After praising the system’s prowess, including its ability to intercept enemy fighter jets and incoming missiles at a range of up to 400km, the graphic cited examples of planes the S-400 could knock down. Every single one was an American aircraft.

Turkey’s allies have learned to ignore such needling, examples of which often pop up in the pro-government press. But they are following the missile deal itself with increasing unease. NATO officials say Mr Erdogan’s government is free to shop for military hardware wherever it pleases, but take a dim view of its decision to do so in Russia. Analysts point out that the S-400 would not be interoperable with NATO’s air-defence system. The chairman of NATO’s military committee, Petr Pavel, recently warned of unspecified “consequences” if Turkey were to go ahead with the purchase. The presence of Russian missiles on Turkish soil, he said on October 25th, would create “challenges for allied [aircraft] potentially deployed onto the territory of that country”. Nonetheless, Turkey’s defence minister announced on November 11th that the sale had been agreed.

It risks running foul of American sanctions against Russia. In October the State Department said it reserved the right to penalise governments that buy military equipment from Moscow. (The Russian arms companies blacklisted on its website now include the makers of the S-400.) Although countries can apply for exemptions, Turkey’s sorry human-rights record, as well as unprecedented tensions with the US, do not help its cause. Outraged by the arrests of two local consular staffers by Mr Erdogan’s police, America suspended visa services across Turkey in October. The ban has since been relaxed. But the bad blood remains.

Relations with NATO are also increasingly fraught. In November Turkey withdrew its troops from a NATO exercise in Norway after an “enemy chart” prepared for the occasion by a contractor was found to include Mr Erdogan’s name and a picture of Turkey’s founding father, Kemal Ataturk. Turkey’s government accepted an apology from NATO’s highest official, but insisted on a thorough investigation. The ultranationalist and Islamist media seized on the occasion and called on Turkey to withdraw from the alliance.

American officials have long suggested that the best way to defend Turkish airspace would be to buy the US-made Patriot system, the kind already sold to Germany, the Netherlands, Greece and Saudi Arabia, among others. Their counterparts in Turkey agree that the Patriot system is the better option, but balk at the price tag and fear that the Americans will transfer less technology to Turkey as part of a missile deal than the Russians might. Also, Mr Erdogan’s reputation in Washington is so bad that any deal could be torpedoed by Congress. “If they don’t give us visas, they probably won’t sell us Patriots,” quips one Turkish official.

For now, Mr Erdogan’s ministers say, the S-400 is the best system Turkey can afford. Under the deal, Turkey says it will receive four missile batteries, for around $2bn. But because the S-400 cannot be plugged into NATO’s radar network, and Turkey would have only four batteries, the system would be able to defend only a fraction of Turkey’s airspace. (Turkish officials acknowledge that the S-400 is a stop-gap measure, and say that their country remains in the market for a NATO-compatible system.) Some analysts suspect that Mr Erdogan’s enthusiasm for the deal has less to do with national defence and more with his fear of a repeat of last summer’s failed coup, when F-16 fighter jets manned by rogue pilots struck his palace compound in Ankara. “If Turkey buys the S-400, putting one of the batteries in Ankara makes sense,” says Aaron Stein of the Atlantic Council, a think-tank. “Its primary mission could be [to protect] the palace.”

There is another snag looming. Mr Erdogan’s government is still insisting that Russia must allow it to produce some of the S-400 batteries at home. Yet Mr Putin is not in the habit of handing over sensitive defence technology to anyone. He no doubt hopes that a missile deal would deepen Turkey’s rift with the West, but he must also be wary of sharing Russian secrets with a NATO member. So despite all the posturing, the deal could yet unravel.