In a 302-page opinion, a federal judge in Montgomery condemned the dire conditions faced by prisoners with mental illnesses.

Ten days after he testified on the state of mental-health care in Alabama’s prisons last year, Jamie Wallace killed himself. He was 24 years old.

Wallace took the stand in December 2016 in Braggs v. Dunn, a class-action lawsuit challenging the Alabama Department of Corrections’ mental-health-care system. Over the course of a seven-week trial, he and other inmates described ADOC’s troubling policies and procedures for treating their severe mental illnesses.

While testifying, Wallace began to grow agitated and distressed while recounting his experiences. Eventually, federal judge Myron Thompson halted the proceeding and moved it to the library of his judicial chambers. There, in the quieter surroundings, the judge and the lawyers were able to “coax him into completing his testimony as if he were a fearful child,” Thompson wrote in his ruling Tuesday.

After the man finished, the judge asked both parties to send him a report on Wallace’s mental condition “and the steps that were being taken to address that condition.” Wallace took his own life before it could be completed.

His death “darkly draped all the subsequent testimony like a pall,” Thompson wrote. In his 302-page decisionin the class-action lawsuit, released earlier this week, the judge called the state’s mental-health-care system for prisoners “grossly inadequate” and concluded it violated the Eighth Amendment’s prohibition against cruel and unusual punishment.

The failures cited by Thompson in his decision are comprehensive and grim. ADOC often doesn’t identify and treat prisoners with serious mental-health needs inside the 14 prisons under its control, he wrote. For those it does identify, the department fails to provide enough qualified mental-health workers to treat them with therapy, and instead often “[imposes] disciplinary sanctions on prisoners for symptoms of their mental illness … without regard for the impact of sanctions on their mental health,” Thompson wrote.

“This ruling means that prisoners with mental illness may finally get the treatment they have been denied for so long,” Maria Morris, a senior attorney with the Southern Poverty Law Center, which represented some inmates in the case, said in a statement. “The suffering some of these men and women have endured is excruciating and inhumane. We are pleased Judge Thompson has demanded that the state of Alabama meet its constitutional obligation to provide adequate care.”

Wallace suffered from bipolar disorder and schizophrenia when he took the stand to testify about the system’s shortcomings. He told the court he sometimes heard the voice of his mother telling him to cut his wrists. He had fatally shot her when he was 16 years old, less than two weeks after his release from a mental hospital on a new medication. Wallace showed the judge and the lawyers present his scars.

Prison officials did not take Wallace’s mental-health needs seriously before his death, Thompson described. In a note in his medical records five days before his suicide, a staff member wrote that he was “using crisis cell/threats to get what he wants,” referring to a specialized cell designed to make suicide attempts more difficult. After some of Wallace’s own attempts, officials cited him for disciplinary infractions—which was department policy, according to Thompson—and sometimes placed him in solitary confinement.

Those practices drew sharp criticism from the judge. “Mental-health and correctional professionals have recognized that long-term isolation resulting from segregation, or solitary confinement, has crippling consequences for mental health,” he wrote. Among those quoted by Thompson was Justice Anthony Kennedy, who wrote a major concurring opinion two years ago criticizing the “terrible price” imposed by lengthy periods of isolation.

Wallace was far from the only one in Alabama’s prisons to face these circumstances. About 3,400 prisoners under ADOC’s supervision receive some kind of mental-health treatment, ranging from medication to psychotherapy. That amounts to 20 percent of the state’s prison population. The stigma surrounding mental illness means the true number of prisoners wrestling with symptoms may be even higher.

Nor are the problems restricted to Alabama. The mid-20th century shift from inpatient psychiatric hospitals to outpatient mental-health care allowed many people with mental illnesses to live normal, productive lives. As I reported in 2015, those who fell through the cracks often ended up in the American criminal-justice system, which now effectively serves as the nation’s mental-health system of last resort. American jails alone house an estimated 400,000 people struggling with serious mental illnesses, a population larger than Cleveland, Honolulu, or New Orleans.

Prisons are far from ideal environments for medical care, mental or otherwise. But states are required to provide adequate levels of health care under the Constitution. The Supreme Court has held since the 1970s that depriving prisoners of medical treatment violates the Eighth Amendment. (Ironically, this makes prisoners the only group of Americans with a constitutional right to health care.) Prison systems often face lawsuits claiming substandard medical care for physical illnesses. In Illinois, for example, the state is facing a class-action lawsuitfrom prisoners who claim the treatment they receive doesn’t meet the Constitution’s minimal standards.

Testifying in their defense during the trial, ADOC officials attributed the failures to forces often beyond their control. Jefferson Dunn, the commissioner of the state department of corrections, told the court about the “two-headed monster” of overcrowding and understaffing that he wrestled with while overseeing the system. (The Illinois prisoners’ lawyers in that lawsuit found similar woes inside facilities there.) Thompson acknowledged those problems in his findings, but chastised corrections officials for not doing enough to address shortcomings.

“ADOC officials admitted on the stand that they have done little to nothing to fix problems on the ground, despite their knowledge that those problems may be putting lives at risk,” he wrote. “The skyrocketing suicide rate within ADOC in the last two years is a testament to the concrete harm that inadequate mental-health care has already inflicted on mentally ill prisoners.”

The next step for both sides in the lawsuit will be deciding how to begin remedying the problem. Thompson said he would gather the parties together to discuss solutions in the near future. “The court emphasizes that given the severity and urgency of the need for mental-health care explained in this opinion, the proposed relief must be both immediate and long term,” he wrote in his decision. Whatever form the relief takes, it will still have come too late for Jamie Wallace and others like him.