BERLIN - German Chancellor Angela Merkel’s ruling coalition agreed Thursday on sweeping measures to spur the integration of migrants and refugees in a “historic” first for a country that long resisted embracing immigration.

The deal, hammered out in seven hours of late-night talks between Merkel’s conservative Christian Union bloc and the Social Democrats (SPD), also included plans for new anti-terror legislation.

The integration bill would take a carrot-and-stick approach, providing subsidised courses to help newcomers find their way in German life but, in some cases, denying residence permits to those who fail to take up the offer.

Germany took in more than one million asylum seekers in 2015 and Merkel has faced virulent criticism from sceptics, particularly from within her conservative camp, who argue that Europe’s top economy is ill-equipped to cope with the influx.

She told reporters the measures, “in a first for Germany” would foster the settlement of those who stay in the country in the long run.

“We know, after hundreds of thousands of refugees arrived in our country, that we are facing a two-pronged challenge: on the one hand controlling the flow of refugees... and of course on the other hand not just registering them but integrating them,” Merkel said. Vice Chancellor Sigmar Gabriel of the SPD called the agreement a “historic step” towards acknowledging the “modernisation and opening of our society” and said he hoped it would lead to a broader immigration bill long resisted by conservatives.

“For the first time in the history of the republic, Germany will have its own integration law,” he said, criticising the neglect the issue suffered in the country over decades.

“The integration of people from very different cultures does not happen on its own, as we were forced to learn based on our experience.”

Beginning in the 1960s, Germany invited Turks and other “guest workers” to fuel its economic miracle but it failed to provide millions of immigrants and their descendants a place in their new society or a path to citizenship.

In recent years, Berlin has looked to immigration as a viable solution to the country’s demographic time-bomb, with a dramatically shrinking native population expected to pose serious problems for Europe’s top economy.

- ‘We can do it’ -

The government’s show of unity Thursday was intended to end months of infighting and breathe life into Merkel’s “We can do it” mantra during the refugee crisis, which she has repeatedly said can also represent an opportunity for Germany.

Meanwhile the closure of the so-called Balkan route taken by many migrants has led to a sharp decline in new arrivals in recent weeks, alleviating some of the political pressure on Merkel.

Under the new pact seen by AFP, federal funds would be used to create 100,000 jobs for asylum seekers receiving benefits.

Those facing imminent deportation would be excluded but asylum seekers taking part in job training would be shielded from expulsion for the length of the programme.

Refugees who abandon state-assigned housing would face unspecified consequences, but waiting periods for acceptance courses teaching German language and customs are to be slashed to six weeks from three months currently.

“An offer for everyone but also requirements for everyone who arrives here - the goal is to integrate as many people as possible into the labour market,” Merkel said.

“Only those refugees who work toward their own integration will receive a permanent residence permit.”

The right-left coalition also agreed to give more funding, personnel and powers to security forces to combat terrorism.

A significant new provision would allow federal police to deploy undercover officers for attack prevention as well as criminal prosecution.

It would also allow the secret services to step up cooperation with their counterparts from partner states in Europe and NATO as well as Israel.

The draft law is to be completed on May 24 and then presented to parliament, where the so-called grand coalition has a large majority.