The court was told that in the three days before the stabbings, Safi had attended an appointment at his GP, had a mental health assessment at Hillingdon Hospital and had sought help at Hayes police station.

After the stabbings, doctors concluded Safi was suffering from psychotic depression which had led to the violent offences.

Dr Elliott Carthy, who assessed Safi at Broadmoor, said he had “paranoid and persecutory beliefs”, and voices in his head contributed to his actions that day.

He said a hospital order with restrictions was the best way to protect the public “going forward now and in anticipation of his release into the community”.

He said Safi was likely to spend at least five years at Broadmoor before being considered for transfer to a medium security facility where he would stay for several more years, and if he is released into the community he would be under strict conditions and subject to lifelong monitoring.

Safi, who admitted manslaughter, was found guilty by a jury of attempting to murder Farrukh and pleaded guilty to causing actual bodily harm to the teenage boy, as well as possession of the knife.

Prosecutors dropped a charge of murder after the guilty plea to manslaughter was entered, and after receiving expert reports on Safi’s mental health.

The judge told the court she would have sentenced Safi to life in prison with a minimum term of eight years if he had not been mentally unwell.