Defense Secretary Chuck Hagel on Wednesday forcefully defended President Obama’s decision to secure the release of Army Sgt. Bowe Bergdahl in exchange for five Taliban detainees at Guantanamo Bay, telling lawmakers that officials acted within the law and “in the best interests of our country.”

“We made the right decision, and we did it for the right reasons,” Hagel said in an opening statement before a House Armed Services Committee hearing on the exchange.

In repeated exchanges with Republicans who charged the administration had broken the law by failing to inform them before the exchange, both Hagel and Pentagon legal counsel Stephen Preston repeated that the circumstances dictated their actions and that they sought and received legal counsel that they acted legally under the president’s constitutional powers to protect American citizens and servicemembers.

Rep. Jeff Miller (R-Fla.) asked why Bergdahl had not yet been transferred back to the United States but remained at a U.S. medical facility in Germany and suggested that the administration was holding him there for other reasons. “Are you trying to tell me he’s being held [in Germany] for medical reasons?” Miller asked.

Hagel, raising his voice and talking over Miller’s efforts to interrupt him, said, “I don’t know how much medical training you’ve had, congressman. I haven’t had much.” Saying that Bergdahl remained in Germany because of the decisions of his doctors, Hagel said, “I hope you’re not implying anything other than that. I don’t like the implication.”

In his remarks, the administration’s first public testimony on the issue of the prisoner swap, Hagel provided additional details on negotiations with the Taliban.

After three days of “intensive talks” with Qatar acting as intermediary, Hagel said, the final decision to move forward on the exchange was made only 96 hours before Bergdahl’s release.

“We did not know the general area of the hand-off until 24 hours before,” Hagel said. “We did not know the precise location until one hour before. And we did not know until the moment that Sgt. Bergdahl was handed over safely . . . that the Taliban would hold up their end of the deal.”

Hagel said that “we were told by the Qataris that a leak, any kind of leak, would end the negotiations for Bergdahl’s release” and that U.S. Special Forces operators would be in danger of attack. The five detainees were not released into the hands of Qatari officials at Guantanamo until confirmation that Bergdahl was in U.S. hands, Hagel said.

In a statement opening the hearing, Committee Chairman Howard P. “Buck” McKeon (R-Calif.) said Obama “has broken the law” by not notifying Congress in advance of the exchange and put U.S. troops at greater risk in the future by negotiating with Bergdahl’s captors.

Hagel disagreed. In deciding not to inform Congress, under a law requiring 30 days notification of all transfers from Guantanamo, he said, the administration received Justice Department assurance that the action was legal.

“The president and I would not have moved forward unless we had complete confidence that we were acting lawfully, in the national interest and in the best traditions of our military,” Hagel said. “Our operation to save Sgt. Bergdahl’s life was fully consistent with U.S. laws in at least five ways.”

First, he said, risk associated with the transfer of the Taliban detainees was “substantially mitigated,” as required by U.S. law, under conditions agreed to by Qatar, including “travel restrictions, monitoring, information sharing and limitation on activities,” as well as other measures Hagel said he would detail in a classified hearing following the open session.

“Second, we fulfilled our commitment to recover all military personnel held captive,” Hagel said. The exchange also followed “the precedent of past wartime prisoner exchanges.”

Hagel drew a distinction between negotiating with hostage-takers and negotiation over a “detained combatant being held by an enemy force . . . fully consistent with our long-standing policy not to offer concessions to hostage takers. The Taliban is our enemy, and we are engaged in an armed conflict with them.”

Finally, Hagel said that the exchange was consistent with briefings Congress was given in late 2011 and early 2012 during early negotiations over Bergdahl’s release.

“Under these exceptional circumstances — a fleeting opportunity to protect the life of an American service member held captive and in danger — the national security team and the president agreed that we needed to act swiftly.”

Recalling his own service as a senator from Nebraska serving on the Intelligence and Foreign Relations committees, Hagel said he understood lawmakers’ unease.

“I know that trust has been broken. I know you have questions. . . . I’ll tell you something else, I have always been straightforward, completely transparent with this committee. I will continue to do that. . . . That’s what I always demanded of any administration when I was a member of the U.S. Senate. I’ve been on your side.”

“The circumstances were imperfect,” Hagel said. “But you have to make a choice, you have to make a decision.”