Abu Khattala, photographed shortly after his apprehension by U.S. Special Operations forces near a villa south of Benghazi, Libya, on the evening of June 15, 2014. (U.S. Attorney’s Office for the District of Columbia)

Under a nearly full moon, a small boat pulled away from the Libyan coastline.

A bearded man sat on board, masked, handcuffed and gagged until out of shouting range of land. After 20 minutes, armed guards lowered him into a shallow pit in the deck of a second vessel to guard against his falling overboard on the final choppy, 100-minute sprint to their destination: a U.S. warship waiting in the Mediterranean Sea.

There, Ahmed Abu Khattala began a 13-day trip crossing the Atlantic Ocean that would end in a U.S. courtroom.

The suspected ringleader of the Benghazi terrorist attacks was taken to a specially constructed brig aboard the USS New York on June 16, 2014, at 4:19 a.m. Libya time, according to the log for the ship’s detention center.

Interrogations began four minutes later, at 4:23 a.m.

The daily detention log showing “first time in cell.” (U.S. Attorney’s Office for the District)

The events surrounding the capture of Abu Khattala, accused as the mastermind of the lethal attacks in Benghazi that killed a U.S. ambassador and three other Americans, are emerging with rare detail in testimony and records in an ongoing federal court case in Washington.

Abu Khattala’s lawyers want his statements to FBI investigators thrown out, arguing that his capture and detention in a windowless room below deck were coercive and render meaningless the many waivers he signed giving up his right to an attorney and his right to not incriminate himself.

He has pleaded not guilty to charges including murder.

Prosecutors contend that Abu Khattala never explicitly asked for a lawyer and implicated himself as a conspirator when he cooperated.

The case could mark the first time a federal judge rules on the constitutionality of interrogation practices developed in recent years for terrorism suspects captured overseas.

The judge’s scrutiny will go to the heart of a question that has not been settled since the rise of the U.S. detention facility at Guantanamo Bay, Cuba: Can the government successfully flip a ­legal switch during interrogations and move from military tactics to the safeguards needed for a speedy and fair trial in civilian court?

The case also arises as the Trump administration pledges to step up actions against foreign terrorists, including Islamic State fighters, raising the prospect that a ruling on Abu Khattala’s questioning could affect the handling of future detainee issues.

U.S. District Judge Christopher R. “Casey” Cooper is due to rule on Abu Khattala’s statements before the Libyan’s trial, scheduled for September.

Brian Egan, a senior White House and State Department ­legal adviser during the Obama administration, called the Abu Khattala matter “a real test case” in which “the government consciously designed the capture and interrogation around being able to withstand legal challenges.”

Abu Khattala, who is being held in the Alexandria, Va., city jail, has pleaded not guilty to 18 charges in the Sept. 11 and 12, 2012, attacks in Benghazi.

The final plans for Abu Khattala’s capture had been set for nearly a year, according to a timeline described in court by an FBI agent who was told in late 2013 to get ready to join what became an eight-man, military-led capture team that grabbed Abu Khattala from his southern Benghazi villa.

The agent, who testified only under the last name “Johnson,” said the team expected violent resistance from Abu Khattala, an auto mechanic nearly 6 feet tall and 230 pounds who was known to carry a handgun and possibly a grenade.

File photo of the USS New York in the Hudson River. (U.S. Attorney's Office for the District)

They seized him at about midnight June 16 in Libya in a struggle that left him with a two-inch cut to his head and evidence of blows around his eyes and a wrist.

About three hours later, after rides in small boats that were “special, somewhat expensive and delicate,” a crane hoisted Abu Khattala onto the warship, still shackled, masked and with headphones that cut sounds.

The mask came off in the detention center, a row of four identical 8-by-7-foot cells plus a latrine linked by a corridor. A plywood barrier and black, noise-blocking tarp encased the detention center to hide tipoffs that the holding cells were on a ship.

Army guards in camouflage uniforms stripped of name tapes and identifying patches told Abu Khattala that he could address them with only two words — “bathroom” or “water,” said Army Staff Sgt. Dylan Peterson, the sergeant of the guard.

Abu Khattala was read and shown a copy of his Geneva Conventions rights in English and Arabic and was seen by an Army doctor who was part of the mission, government witnesses said. They said he was told to report any abuse and directed to change into an orange shirt and pants and sneakers.

He was given a Koran and a blanket and could sit or sleep on a bare floor. And he was told that a rough, hand-drawn compass on the wall pointed to Mecca for his prayers.

He was masked for any movement outside the cell, even to the latrine or an adjacent room, so he could not see guards’ faces.

And in his cell, the lights were on round-the-clock, the room was a steady 72 degrees, and he was roused or moved at least every two hours, FBI and Army witnesses testified. The detention rooms were monitored by closed-circuit television, but video was not recorded, the FBI said.

Abu Khattala was held in an 8-by-7-foot ventilated cell between interviews, lighted around the clock at a controlled temperature of about 72 degrees, and subject to checks every two hours. For the first 3½ days, the cell contained a Koran, a blanket and a box to keep the objects off the floor. (U.S. Attorney’s Office for the District)

Upon reaching the USS New York, Abu Khattala was given an orange detention suit in this cell, checked by a military doctor, and told to provide DNA and biometric samples. Abu Khattala was apprehended by U.S. Special Operations forces near a villa south of Benghazi, Libya, during the evening of June 15, 2014. (U.S. Attorney’s Office for the District)

His questioning was done in two phases, the government said in court.

For 26 hours over three days, a High-Value Detainee Interrogation Group of military, intelligence and law enforcement officials questioned him for intelligence-gathering purposes.

How that work was done was classified and discussed in a closed courtroom.

As the USS New York exited the Mediterranean near the Strait of Gibraltar, the intelligence-gathering unit left the ship by plane. About the same time, a second team of FBI investigators flew in.

The switch was meant to preserve the ability of the incoming “clean” team to collect evidence for civilian court without a taint from the intelligence unit.

The second FBI team did not meet with the first team or discuss its work, and a shipwide ban on Skype and video-conferencing took effect, FBI counterterrorism official C. Bryan Paarmann testified. Members of the second team were directed to limit email, and they and their New York field office were cut off from email circulation of FBI intelligence reports, he said.

The changeover was made obvious to Abu Khattala, according to the FBI’s lead Benghazi agent, Michael M. Clarke.

With the arrival of a “clean team” to question him and gather the evidence for a criminal trial, Abu Khattala’s living quarters were provided with a mattress, a prayer rug, a sweatshirt, a Koran and writing materials, prosecutors said. (U.S. Attorney’s Office for the District)

For two days, Abu Khattala faced no questioners, Clarke said in court, and his living conditions improved: He got a mattress, pillow, prayer rug and writing materials. He went from two meals a day to three, was allowed to shower and got a Champion tracksuit as a change of clothes.

His interview sessions went on for another 28 hours but in shorter installments, and he received small items: a sweatshirt on June 21 when he said he was cold, a watch from the FBI interpreter on June 22 when he said he did not know the time of day for prayer.

Clarke testified that Abu Khattala was told of the criminal investigation into the Benghazi attacks and that he was being taken to the United States. He was told that the new FBI team was starting afresh and knew no specifics of his earlier interrogation, Clarke said. And Abu Khattala signed forms waiving his constitutional rights on each day of questioning, six in all, testimony showed.

The interview room used by the first team of intelligence investigators. Video cameras sent a live feed to shipboard monitors but did not record. (Interview room/U.S. Attorney’s Office for the District)

The two-step process was not done to undermine Abu Khattala’s constitutional rights, Assistant U.S. Attorney John Crabb argued in court Tuesday, but instead “was employed for legal, intelligence purposes [after] one of the most infamous terrorist attacks in recent history.”

Defense lawyers contend that U.S. authorities were deliberate when they planned the Abu Khattala operation as a “slow-boat” procedure to allow nearly two weeks of continuous, unrecorded interrogation.

Although Abu Khattala’s captors told him he had a right to a lawyer, when he asked if one was present, he was told no, Clarke said. “He asked, ‘Is there a lawyer here?’ In my mind, I thought that meant on the ship, and I replied no, there was not,” Clarke said.

For all the cost and intrigue of the operation, “the government spent no time or effort to make sure that Mr. Khattala could meaningfully exercise his right to counsel, even as it was dangled before him,” defense attorney Eric L. Lewis said.

Government witnesses acknowledged under cross-examination that a lawyer could have been flown aboard with medics or investigators or provided by video conference. But they said that Abu Khattala never specifically asked for one and that he had signed the waivers.

Abu Khattala had spent 10 years as a political prisoner under Libyan dictator Moammar Gaddafi’s regime and could have feared a return to harsher or indefinite detention if he failed to cooperate, a defense expert testified. He knew only that he was being interrogated, then held for two days in isolation, then given better treatment once he cooperated and signed statements, his lawyers said.

Prosecutors challenged the characterization of Abu Khattala as an unsophisticated victim, noting that despite a ninth-grade education, he led the Benghazi branch of Ansar al-Sharia, an armed militia that seeks to establish sharia law in Libya. The United States designated the group as a terrorist organization for its role in the attacks.

He was taken to the USS New York because U.S. officials thought it too risky to fly him out of Libya using a desert airstrip, government witnesses said.

As the ship headed west, he was not transferred to land — for a quicker flight to the United States — because of diplomatic hurdles, government witnesses said. European Union countries that oppose capital punishment might not have helped because Abu Khattala faced a possible death sentence once in the United States.

FBI and Justice Department witnesses said other countries in Europe and the Middle East were not approached — except one unnamed country, whose head of state refused — because participation in the U.S. capture could stir internal political opposition or unrest.

On the 12th of his 13 days on the USS New York, before they reached U.S. soil, FBI agent Clarke told Abu Khattala that he had been indicted on conspiracy charges.

Clarke said Abu Khattala had acknowledged knowing about 70 of 120 individuals whose photos he was shown, as investigators pieced together communications, associates and coordination of the attack.

Abu Khattala asked, “What is conspiracy?” and “if that was a Western law,” Clarke said. “We replied it was a U.S. law, a Western law.”

He said Abu Khattala responded, “That’s unjust.”

On the 13th day, a final entry went into the log for the ship’s detention center: “detainee leaves facility” at 3:30 a.m. June 28.