Germany was warned in advance that a Syrian who blew himself up last month might carry out a "spectacular" suicide attempt if it tried to deport him, BBC Newsnight has learned.
Mohammed Daleel was due to be deported when he detonated a bomb in the Bavarian town of Ansbach on 24 July.
A 25-page psychological assessment written more than a year before mentioned his "extreme spirit".
It was sent to the Federal Office for Migration and Refugees.
It warned that attempts to deport Mr Daleel could result in a "spectacular" suicide attempt.
Before the bombing, Mohammed Daleel left a message pledging allegiance to Abu Bakr al-Baghdadi, the leader of the so-called Islamic State.
The attacker tried to get into an open-air concert attended by 2,500 people, but was stopped by a security guard. Instead he detonated a rucksack filled with explosives in a local beer garden, killing himself and wounding 15 others.
Ten days before the attack, Mr Daleel had received notice from the German authorities that he was to be deported to Bulgaria.
Under the Dublin regulation, refugees are required to claim asylum in their first port of entry into the EU. Mr Daleel crossed into Bulgaria from Turkey in 2013. He later claimed he had been abused in detention in Bulgaria.
The psychological assessment of Mohammed Daleel, seen by BBC Newsnight, was written by Axel von Maltitz, a trauma therapist in the town of Lindau. Mr von Maltitz treated the 27-year-old Syrian for psychological problems until January 2016.
He believes the deportation notice triggered Mr Daleel's attack.
"That is always what he promised. That was always his reaction as answer to the information that he was to be deported.
"I warned them. That is what I wrote. 'Be careful if he has to be deported to Bulgaria'."
Axel von Maltitz first met Mohammed Daleel in January 2015, after the Syrian had cut his wrists following an earlier deportation order.
"He had bandages around his arms because that was still quite fresh. He was highly traumatised.
"Traumatisation has very typical symptoms. He had Post Traumatic Stress Disorder severely. Very strong."
Mr Daleel claimed he was an opposition activist in Syria and that he had been imprisoned and tortured on three occasions between 2008 and 2011.
He told Mr von Maltitz he was beaten, subjected to electric shocks, and forced to witness rapes and brutal executions. He also said his wife and infant son were killed in a rocket attack on their home in Aleppo.
The 2015 deportation order was quietly dropped, and Mohammed Daleel continued living in a hostel in Ansbach while receiving treatment, first at a psychiatric ward in a local hospital, and subsequently again with Axel von Maltitz in Lindau.
Mr von Maltitz remembers: "He once said: 'I cannot close my eyes. Because the bad pictures start running the moment that my eyes are closed'."
German investigators have not ruled out the possibility that Mohammed Daleel may have had contact with jihadist groups years before he detonated his bomb. They say he was communicating with a telephone number in Saudi Arabia in the minutes before the attack.
But Manfred Hauser, deputy director of intelligence for the state of Bavaria, told Newsnight it was more likely that Mr Daleel was a "lone wolf" who was vulnerable and had recently self-radicalised.
"The internet plays a big role here," he said.
"There is a variety of jihadist propaganda easily accessible on the internet. This process happens very fast.
"It seems quite likely that people who are susceptible to mental problems are particularly receptive to Salafist or jihadist ideology."
Balance of motivations
Several witnesses told Newsnight that, in the months before the attack, Mr Daleel spent more and more time in his room in his hostel in Ansbach, surfing the internet on his laptop.
Axel von Maltitz, and others who knew him, said he did not seem especially concerned with religion and was rarely seen in local mosques.
Mohammed Daleel's therapy was due to resume on 1 August.
It has not been possible to verify many of the details of his story as told to his therapist: his role in the opposition, the torture, the death of his wife and child.
Nor is it clear what role, if any, the threat of deportation played in his decision to carry out the bombing.
In Germany's first IS-inspired suicide bombing, the balance of motivations, between jihadist ideology and mental health, will likely remain unknown.