The European Union and the Turkish government reached accord Friday on how to contain Europe’s largest migrant crisis since World War II, agreeing to a deal that turns Turkey into the region’s refugee camp and leaves untold thousands stranded in a country with a deteriorating record on human rights.

After a day and half of wrangling, European leaders and Turkey hashed out the specifics of a broad agreement announced last week that was the brainchild of the Turks and German Chancellor Angela Merkel. Under the deal, which goes into effect Sunday, virtually all migrants who attempt to enter Europe via the Aegean Sea — including Syrians fleeing war — will be sent back to Turkey.

The goal, leaders said, is to finally shut down massive irregular migration along a route that runs from the battlefields of the Middle East through Turkey to Europe, a conduit for a almost 900,000 arrivals last year.

“To those who would embark on this perilous journey: Do not risk your lives, there is no prospect of success,” Merkel said after the deal was struck. “This is how we want to end this inhumane business model of the traffickers and restore the protection of our external borders.”

Journey alongside refugees through Lesbos, the gateway to a new life

At a news conference in Brussels, European Commission President Jean-Claude Juncker described the deal as “fair” and “in accordance with the law” but conceded that implementing it will be “a herculean task.”

Four thousand E.U. staffers will be involved in the new effort to secure the Aegean Sea, at a cost in the hundreds of millions of dollars, Juncker said, adding, “It is the largest challenge the E.U. has yet faced.”

Smiling broadly, Turkish Prime Minister Ahmet Davutoglu described the deal as “historic” and said it reflected deepening cooperation between his nation and the E.U.

“Turkey and the E.U. have the same destiny and the same challenges,” he said.

But the fissures in the relationship soon became apparent as Davutoglu denounced militant Kurdish groups as terrorist organizations and chided European leaders for allowing pro-Kurdish protests in their cities.

European Council President Donald Tusk shot back with pointed comments defending the right to protest as a core European value.

Under the agreement, Turkey gets cash — 6 billion euros, or $6.6 billion — and other incentives, including jump-started talks on its bid for E.U. membership and a conditional promise of visa-free travel for its citizens to Europe. Such gifts are likely to provide a big boost at home to Turkey’s authoritarian president, Recep Tayyip Erdogan, now in the midst of a crackdown on domestic dissent.

Besides being logistically complex, the deal is filled with conditions that, if left unmet, could prove its undoing.

The Europeans did pledge to accept a relatively small number of Syrians after legal processing in Turkey. But E.U. countries have the right to reject refugees, and it remains unclear which would take them. No other nationalities, including Iraqis and Afghans, would qualify for sanctuary, and Syrians caught trying to enter without authorization would be effectively barred from legal entry.

In negotiating the accord, the Europeans pitched it as the only way to end the “human suffering” of migrants being exploited by smugglers. But human rights groups dismissed those claims, arguing that the deal is a possible violation of international and E.U. law and likely to cause more misery, not less.

More asylum seekers will now be stranded in Turkey, a nation that does not fully honor the Geneva Convention on refugees. European leaders say Turkey will rapidly strengthen protections as part of the deal — a claim that many observers found dubious.

Even as the agreement was being worked out, Erdogan suggested that calls for better human rights in Turkey were hypocritical coming from the leaders of wealthy countries that refuse to take in asylum seekers.

“At a time when Turkey is hosting 3 million migrants, those who are unable to find space for a handful of refugees, who in the middle of Europe keep these innocents in shameful conditions, must first look at themselves,” Erdogan said in a nationally televised speech in Turkey.

Yet rights groups say that in Turkey, where more than 2.7 million Syrians fleeing war already live, many migrants being badly exploited. Amnesty International says Turkey has also arrested asylum seekers attempting to cross the Aegean, bringing them to detention centers where they have been kept for weeks without access to lawyers or family. Some, the group says, were given the choice to stay in Turkey or return to Syria or Iraq.

“Turkey itself is a human-rights-abusing country,” said Wenzel Michalski, Germany director for Human Rights Watch. “We have worrying news of lawyers, activists and journalists being thrown in prison. They have started a war against the Kurdish, and parts of Turkey are now like a war zone. How does this make Turkey an appropriate country to manage refugees?”

Activists sounded particularly disillusioned with Merkel, who was celebrated in human rights circles after vowing last year that there was “no limit” to how many asylum seekers Germany could take in. But after a million migrants, using myriad routes, took her up on that offer — and with anti-migrant sentiments mounting at home — she has grown increasingly desperate to curb the influx.

“Merkel isn’t the moral leader any longer, not with this deal,” said Karl Kopp, a spokesman for the refugee aid group Pro Asyl.

In addition, critics said, the plan could force migrants onto even more dangerous routes to Europe. Arrivals via lawless Libya and a wide stretch of sea to Italy appeared to be spiking, with 700 migrants picked up just on Friday.

Even some European leaders conceded that the legality of the new Turkey deal is unclear.

Under the plan, migrants interdicted in Turkish waters will be forcibly sent back to Turkey. Rejecting asylum seekers without a hearing, however, violates E.U. and international law. For those migrants who make it as far as Greek waters, or even Greece’s islands, the E.U. will try to technically comply with international law by offering flash hearings — supposedly within hours where possible.

The plan’s counterargument is that once Turkey implements required changes, it will be a “safe” country for refugees.

There was no sign of an immediate solution for the more than 40,000 migrants currently in Greece and barred from moving north.

Europe is, however, pledging to roll out a “one to one” deal on Syrians with Turkey. For every Syrian returned, another would be brought legally by air from Turkey into Europe. Initially, at least, Europe would offer legal slots to at most 72,000. Since the plan is not mandatory, European nations would need to volunteer to take Syrians in. Some countries, including Hungary and Slovakia, have rejected that suggestion outright.

To reach a deal, Europe and Turkey had to tackle sensitive issues, including lingering animosity between the Turks and the Cypriots and Greeks. In line with Turkish demands, the Europeans also agreed to broadened talks with Ankara on Turkey’s bid to join the E.U. But many believe that Europe’s concessions are more political show than reality.

Marc Pierini, a former E.U. ambassador to Ankara, noted that Turkey still has to clear major E.U.-imposed hurdles to earn visa liberalization for its citizens, and he argued that the Erdogan government does not truly want E.U. membership, because it would have to dial back its autocratic behavior.

Still, he said, the concessions are important symbolic victories for the Turkish leader.

“It’s largely a show,” Pierini said. “But the fact that the E.U. is playing that game tells you about the political panic that the refugee crisis has created among leaders in Europe.”

Witte reported from London. Stephanie Kirchner in Berlin contributed to this report.

Read more:

Europe’s harsh new message for migrants: ‘Do not come’

Turkey to E.U.: If you want to send us your migrants, send money, too

David Cameron blasted for calling people in refugee camp ‘a bunch of migrants’

Today's coverage from Post correspondents around the world