A federal judge in Arizona has ruled that Border Patrol facilities in the Tucson sector deprive migrants of “basic human needs,” saying the conditions at the temporary detention facilities are “substantially worse” than those in jails or prisons and violate the Constitution.

Overcrowding was at times so severe, U.S. District Judge David C. Bury found, that migrants were forced to sleep on the floor in bathrooms and in toilet stalls.

The Wednesday ruling permanently enjoins U.S. Customs and Border Protection from detaining migrants in holding cells at its Tucson sector stations for longer than 48 hours — “unless CBP can provide conditions of confinement that meet detainees’ basic human needs.”

That means migrants staying any longer than two days must be provided a variety of things they have not been getting: sleep free from constant disruption, in a real bed with a blanket; nutritious food; access to a shower; and a medical assessment by a medical professional.

The ruling also says CBP must immediately stop making migrants sleep in “toilet areas” because of overcrowding.

“Regardless of whether a detainee is sleeping on a mat or directly on the concrete floor, being forced to sleep in a toilet area due to overcrowding offends the notions of common decency,” Bury, a George W. Bush appointee in the District of Arizona, wrote in the 40-page order. “It is unsanitary and degrading for all detainees who either have to sleep in the toilet area or try to use the toilet when others are sleeping there.”

A CBP spokesman said the agency is reviewing the ruling and deferring to the Justice Department, which did not immediately respond to a request for comment.

Bury’s ruling, which follows a seven-day bench trial in January, makes permanent a 2016 temporary injunction that compelled CBP to provide mats and silver Mylar blankets to all detainees; many were previously sleeping on the bare concrete floor.

The class-action lawsuit was filed in 2015 by the National Immigration Law Center, the American Civil Liberties Union, the American Immigration Council, Morrison & Foerster, and the Lawyers’ Committee for Civil Rights of the San Francisco Bay Area to combat filthy Obama-era conditions. But the case extends through the Trump administration, with most evidence at trial spanning 2017 to 2019, an attorney for the migrants told The Washington Post.

At one point during the trial, attorneys for the migrants showed surveillance video footage of a migrant trying to climb over people sleeping in a “toilet area” to reach a bathroom stall — only to find people sleeping in the stalls as well.

“It’s just unfathomable that in our nation we have a Constitution that says all human rights should be protected, and that we were seeing these images,” Alvaro M. Huerta, an attorney for the National Immigration Law Center, said in an interview. “We’re not treating people with the dignity that every human being deserves. It was important that the judge was able to see with his own eyes what’s happening day in and day out."

Bury found that migrants slept on the floors, sometimes on overlapping mats, with the lights on all night, shivering beneath a thin aluminum-foil-like blanket and often huddling together to keep warm. Some migrants slept sitting up. Some could be seen on surveillance video trying to crawl over others just to get a cup of water in the holding cell. Few had access to showers, with most being given moist wipes as a substitute.

Bury said wipes are “ineffective to meet the full personal hygiene needs of detainees, who have been traversing the desert for days, and the smell in holding cells reflects human unwashed bodies until detainees can shower.” He included the dictionary definition so there would be no mistake as to what kind of shower he was ordering migrants be given: “a bath in which water is showered (as in to wet with a spray, fine stream, or drops) on the body.”

CBP said in legal filings that it is building more shower facilities at its stations and is negotiating a new food contract that will provide a better variety of meals and snacks.

Bury said that there was no evidence the government was being intentionally punitive to migrants, and many of its practices, such as keeping the lights on all night, were for security reasons. But because the conditions were “more restrictive” than that of jails and prisons where convicted or suspected criminals are held, in effect, the conditions still functioned as punitive, in violation of the migrants’ constitutional rights to due process, Bury said.

The Fifth and 14th Amendments bar government from punishing people before being convicted of a crime. Immigrants are civilly held after crossing the border.

The government has long argued in this case and in similar cases that CBP facilities were not designed for long-term detention, serving as temporary holding facilities until migrants can be transferred to other agencies. Processing them should take only a matter of hours, never longer than 48 hours, according to the ruling. That’s why most Border Patrol stations are not outfitted with showers, which CBP has said would slow down processing times.

But according to the ruling, in 2019, 34 percent of the 63,490 migrants detained in facilities in the Tucson sector spent more than 48 hours in custody, with 19 percent held longer than three days.

Last summer, during a surge of migrants that led to mass overcrowding, CBP acknowledged that it was failing to turn over child migrants to shelters equipped to care for them in the three days required by law, attributing the failure to a huge backlog of migrants awaiting transfer.

Bury, however, said that periodic surges of migrants along the border is a “chronic condition” and cannot be used as an excuse “to justify unconstitutional conditions of confinement."

Bury is the latest federal judge to assail the filthy, freezing and at times humiliating conditions within CBP detention facilities that greet migrants arriving in the United States, many who are legally seeking asylum.

Outrage over the conditions at facilities along the border boiled over last summer, especially given children and pregnant women were held in them for as long as three weeks, attorneys reported.

In June, U.S. District Judge Dolly M. Gee ordered the federal government to work with an independent monitor to immediately improve the unsanitary conditions affecting hundreds of children.

Around the same time, in a separate case, a panel of judges on the U.S. Court of Appeals for the 9th Circuit excoriated a Justice Department lawyer for arguing that the government wasn’t legally required to provide child migrants toothbrushes, soap, showers, towels or adequate sleeping conditions.

The Justice Department was appealing a 2017 order by Gee requiring the government to provide those items to children. Gee, a Los Angeles-based judge who has long overseen litigation involving child migrants, has at times expressed exasperation with the government’s seeming resistance to improving conditions at CBP facilities along the border.

During the trial in the Arizona case, Bury also appeared dissatisfied with efforts by the federal government to improve overcrowding conditions on its own since his 2016 injunction forced them to provide mats, the Associated Press reported.

“Nobody has done anything. Is that why a court has to jump in?” Bury asked during the last day of trial on Jan. 22, according to the AP. “It just seems like the lack of a response to these numbers just calls for a court order.”