ISTANBUL — It was 4 a.m. in December when Tunca Öğreten heard loud kicks against the door of his apartment.

Twenty anti-terrorist police officers armed with rifles knocked it down and ransacked the home Öğreten shared with his fiancee, before arresting him.

He was kept in a police cell, barely 10 feet square, with four other men for 24 days. With no toilet or shower, he was forced to wash himself by saving up water bottles and to urinate into an empty bottle.

"I begged them, 'Take me to prison,'" Öğreten said. "Prison would be better than this torture."

A judge eventually ordered him to jail, where he languished for 10 more months.

Tunca Öğreten, a Turkish journalist who spent one year in jail without trial, at home in Istanbul. Danielle Villasana

Öğreten, a 37-year-old journalist, is accused of crimes against the state for reporting on hacked emails that accused a company run by Turkey's energy minister of being involved in trading oil from the Islamic State. Almost two years after the raid, he still awaits trial.

Öğretenis far from alone.

Turkey, a key NATO ally, has detained tens of thousands of teachers, lawyers, students, judges and other officials amid a crackdown on dissent by the government of President Recep Tayyip Erdogan after the failed coup of 2016.

The country's 384 prisons and detention facilities are already overcrowded, holding 224,974 inmates as of March 20, according to the Ministry of Justice — almost 7 percent over their official capacity.

Turkey now has the third-highest per capita prison population in Europe, behind only Russia and the tiny post-Soviet dictatorship of Belarus. (The U.S. has the highest rate in the world.)

To fix the problem, the ministry announced in December that it will build 228 more prisons over the next five years.

"Prison wings designed for 20 people are being used to keep up to 45," opposition CHP party spokesman and former lawmaker Baris Yarkadaş told NBC News. "Some of them must sleep on the floor, others develop respiratory sickness. The solution of Erdogan's government is just to keep building more prisons."

Justice Minister Abdulhamit Gul did not immediately respond to a request for comment.

A NATO ally 'isn't acting like one'

Images of civilians climbing on tanks and soldiers being violently beaten with belts dominated news coverage of the aborted coup on July 15, 2016, in which 251 people died.

Pro-Erdogan supporters wave Turkish national flags during a rally in Istanbul on July 18, 2016, a few days after the failed coup attempt. Aris Messinis

But Erdogan's triumphant revenge on dissenters has proved no less brutal.

He immediately blamed the coup on influential cleric Fethullah Gulen, 77, a former Erdogan ally who lives in self-imposed exile in Pennsylvania; Gulen denies involvement and the White House has resisted demands for his extradition.

Turkey describes Gulen followers as members of the Fetullah Terrorist Organization (FETO) and accuses them of a long-running campaign to overthrow the state through the infiltration of state institutions, particularly the military, police and judiciary.

At least 169,000 state workers, soldiers or others have been suspended or dismissed from government jobs in the crackdown, according to the Ministry of Justice. Activist websites such as TurkeyPurge.com, which is blocked inside the country, put the numbers even higher. More than two years after the coup, the roundups continue; 12 accused coup members were detained this month according to the official Anadolu news agency.

"These days you only have to have a bank account or study at a university with Gulen connections and the courts consider that you are a terrorist," Adnan Seker, a lawyer who has been arrested for alleged FETO links, said. He denies any involvement in the coup. "I do not even find sympathy for those who adopt violence."

Turkish cleric Fethullah Gulen is pictured in his home in Saylorsburg, Penn., on July 29, 2016. Charles Mostoller

Erdogan has also used a two-year state of emergency, which ended only last month, to detain anyone suspected of being linked to outlawed groups such as the Kurdish separatist PKK, which is recognized by the U.S. and Turkey as a terrorist organization.

Among those is Andrew Brunson, a North Carolina evangelical pastor who was arrested in 2016 on charges of espionage and "committing crimes on behalf of terror groups without being a member." He has been released from prison but remains under house arrest and is expected to face trial next month.

Brunson, 50, has lived in Turkey for 23 years, running the small Resurrection Church in the western city of Izmir. The Erdogan-linked Daily Sabah newspaper reported last month that a prominent member of Brunson's congregation had shared PKK links on social media.

Secretary of State Mike Pompeo has appealed to Turkish Foreign Minister Mevlut Cavusoglu in person for the pastor's release from house arrest, calling him "an innocent man."

At my first hearing the judge didn't even ask any questions. It is the risk we take by reporting in Turkey.

The White House slapped sanctions on Turkey's justice and interior ministers over the case, which has badly strained relations between Washington and Ankara, culminating in President Donald Trump's sudden decision to raise trade tariffs on Turkish steel.

Also imprisoned is NASA scientist Serkan Golge, a U.S.-Turkish citizen convicted of links to FETO that the State Department says are "without credible evidence."

The crackdown has even reached inside the U.S. mission in Ankara, where three workers are accused of links to the PKK — including Hamza Ulucay, a Turkish national who worked there for more than three decades before his arrest this year.

In January, Erdogan's government created a commission to review decisions made under the state of emergency, but its members are appointed by the same authorities responsible for approving dismissals and the enforced closing of Gulen-linked schools.

"In the meantime, those affected have no right to work in public service, their bank accounts are frozen, and passports confiscated," according to Human Rights Watch, which said more than 102,000 people had appealed to the commission, though it has yet to begin issuing any decisions.

TOPSHOT - Turkish President Recep Tayyip Erdogan greats supporters on August 7, 2016 in Istanbul during a rally against the failed military coup on July 15. Ozan Kose

Status is irrelevant in Erdogan's purge.

New York Knicks center Enes Kanter, a Turkish national who has long been an outspoken critic of Erdogan, was charged in December with insulting him in a series of tweets. Prosecutors want to try Kanter in absentia and have him sentenced to more than four years in prison, if he is convicted.

Kanter wrote in Time on Tuesday that he could not go home because of his views. "This month, my dad will face trial in Turkey," Kanter wrote. "He is a university professor, not a terrorist. Because I play in the NBA, I am lucky enough to have a public platform, so I've used every opportunity to make sure everyone knows about Erdogan's cruelty and disdain for human rights."

Turkey is a crucial U.S. partner in the region — it borders Iraq and Syria, and hosts a U.S. base at Incirlik from which strikes against ISIS have been launched.

Sen. Jeanne Shaheen, D-N.H., a member of the Senate Foreign Relations and Armed Services Committees and who visited Brunson in jail, suggested last month that Washington should seek an alternative base in the region. "Turkey is an important NATO ally but isn't acting like one," she said.

Game of chess

High above courtroom 29 on the sixth floor of Istanbul's giant central courthouse is a brass engraving of Mustafa Kemal Atatürk, the secularist founder of modern Turkey.

Directly facing him on Thursday morning was Tunca Öğreten, attending only the third hearing since his pre-dawn arrest.

Although released from prison on bail last October, Öğreten has yet to be cleared of the allegations against him or be committed for trial. Turkish authorities still have the laptop computers and iPhones seized from his apartment.

Istanbul\' Çağlayan Justice Palace — Europe\'s largest courthouse Murad Sezer

"They even took my iPod for God's sake," he recalls. "It's just my music. What do they want with it?"

Öğreten believes his case is an act of revenge by the government after he reported the contents of hacked emails from Erdogan's son-in-law, Berat Albayrak.

He was not allowed to see a lawyer until five days after his arrest, and the precise charges against him have changed at least twice; he was briefly accused of membership in a proscribed Marxist terrorist organization, DHKP.

Albayrak was energy minister in 2016 when hacked emails, circulated to a number of journalists, revealed his company was allegedly linked to the trade of oil from ISIS-held territories in northern Iraq. He is now the finance minister tasked with managing Turkey's inflation-crippled economy. Albayrak has denied the accusations, although Öğreten's report was never officially disputed.

"Of course they are doing this to punish me," Öğreten said. "At my first hearing the judge didn't even ask any questions. It is the risk we take by reporting in Turkey."

He passed his time in jail by playing chess with other inmates, and managed to avoid beatings from officers — even notorious naked searches.

Each day, prisoners are required to strip to their underwear to prove they are not concealing contraband items. In an act of defiance, Öğreten simply lowered his underwear. "After I did that a couple of times they just stopped asking," he laughed.

Perhaps most cruelly of all, he was prevented from seeing his fiancee, Minez, 31; Turkish law only guarantees prison visits for spouses. Eventually the couple got married in the prison chapel. "Finally, she could visit me," he said. "I am so proud of her. She has been so strong through everything. She is also a journalist, so she kind of understands, but it has been so difficult for her."

Since his release, he has been able to return to work as a freelance reporter, including for an online Turkish news site, Dikem, but is banned from traveling.

His lawyers on Thursday asked a judge to lift the travel ban and return Öğreten's personal items; after a brief recess, the judge refused.

"They still have my music," Öğreten sighed.

Tunca Öğreten, a Turkish journalist who spent a year in jail without trial, at home in Istanbul. Danielle Villasana

Four decades after Alan Parker's stomach-churning "Midnight Express" hit American movie theaters, conditions in Turkey's prisons have improved — but rights groups say beatings and abuse remain commonplace.

The Stockholm Center for Freedom (SCF) reported in one of its studies, "Suspicious Deaths and Suicides in Turkey," that there has been an increase in deaths inside Turkey's jails and detention centers. Among the recent cases is that of Sabri Çolak, a retired professor who was jailed reportedly because he once appeared in a television documentary about Gulen.

The State Department country report for Turkey cites a catalog of prison abuse cases "included alleged torture of detainees in official custody; allegations of forced disappearance; arbitrary arrest and detention under the state of emergency of tens of thousands, including members of parliament."

Although the Ministry of Justice did not respond directly to NBC News, it has described the country report as "one-sided" and "subjective" and said abuse allegations are always investigated. It was "pushing the limits of irony" that the U.S., which had violated rights at Guantanamo, "dares to evaluate Turkey with respect to human rights and freedoms," the ministry said.

According to figures published in an August report by the New York-based Journalists and Writers Foundation, some 44 percent of inmates in Turkey are still awaiting trial or appeal.

"Even though the state of emergency has ended," Öğreten said, "we are still living it every day."