Image copyright Reuters Image caption US Navy Seal Edward Gallagher, shown with his wife, was found not guilty of most of the charges against him

The US navy secretary has been forced out after clashing with the White House over the case of a US Navy Seal who was prosecuted for war crimes. The dispute has turned into a high-stakes and confusing drama.

Here is a guide to how we got here.

What just happened?

Mr Spencer, the US navy's highest-level civilian leader, was forced to resign on Sunday.

But the three key players in the drama, the president, the US defense secretary and Mr Spencer himself have different versions of events.

President Trump said Mr Spencer was sacked in part because of the way he handled the case of Chief Petty Officer Edward Gallagher, a man who was accused of killing a young, wounded Islamic State militant who was taken prisoner.

Gallagher was acquitted of the boy's killing but was found guilty of posing with his dead body in photos. As a result, Gallagher was demoted. Mr Trump reversed the decision, however, restoring his position in the elite Navy Seal unit.

Mr Trump also said that poor management was behind the secretary's resignation: "I was not pleased with the way that Navy Seal Eddie Gallagher's trial was handled by the Navy."

Speaking to reporters on Monday, the president defended his decision to get involved, saying: "I have to protect our war fighters."

Image copyright Reuters Image caption Richard Spencer was forced out as US navy secretary

Meanwhile the US defense secretary, Mark Esper, said he had been "deeply troubled" by Mr Spencer's handling of the case because he had broken his chain of command and sent out conflicting messages about the Navy Seal's fate.

Mr Spencer said publicly that Gallagher should face a disciplinary hearing, but Mr Esper alleges that he was in fact negotiating in secret with White House officials to ensure the Navy Seal would be able to stay in his unit.

And what did the man himself say? Mr Spencer saw the situation differently - he wrote in a resignation letter that he left his position because he felt that he and the president "no longer share the same understanding" about "good order and discipline".

He told CBS News on Monday that the president's actions would send a dangerous message to the troops - "that you can get away with things".

The full truth about why Mr Spencer left the White House may not be known for some time - if ever. But there is no mystery about the underlying reason - the Gallagher case had become highly politicised, with the president rallying behind him.

Mr Spencer was caught in the middle and soon out of a job.

What's the back story?

Mr Trump stirred up controversy earlier this month by publicly defending Chief Petty Officer Gallagher and two other service members who were implicated in war crimes.

Some saw the president's intervention in these cases as an abuse of power, while others applauded his actions because they believed that the military men had been wrongly accused. Regardless of how one saw the individual cases, the story of the president and the navy has reverberated for those in the military and beyond.

The president said that Chief Petty Officer Gallagher should not be demoted. Yet Navy officials nevertheless planned for a disciplinary proceeding that would remove him from the elite unit and take away his Trident pin, a gold-coloured insignia that shows he is a member of the unit.

The hearing and Chief Petty Officer Gallagher's membership in the elite unit was at the heart of the dispute among the president and other high-powered men in Washington. On Monday, the US defense secretary, Mark Esper, told reporters that the president had issued an ordered that ensured that Chief Petty Officer Gallagher would be able to keep his Trident pin.

Can the president intervene like this?

Absolutely. As commander-in-chief, Mr Trump has the legal authority to weigh in on all military matters.

Many welcomed it. David Gurfein, a retired lieutenant colonel who served in the US Marine Corps and is now chief executive officer of United American Patriots, an advocacy organisation that assists military personnel who are charged with war crimes, says he has been pleased with the president's role in the story.

He and many members of the military believe Gallagher and others have been wrongly accused of war crimes and that an atmosphere of "political correctness" has led to overzealous prosecutions of military personnel for their actions on the battlefield.

Image copyright Reuters Image caption Trump stirred up controversy earlier this month by publicly defending Chief Petty Officer Gallagher

"The president's actions are now setting the tone," says Mr Gurfein.

Others see the president's actions in a different way.

A retired military judge, Gary Solis, who served as an officer in Vietnam, acknowledges the president has the right to intervene but that doesn't mean he should, because this will make it harder for military lawyers, investigators and others to pursue war-crimes cases.

"Men and women who witness war crimes are going to be less likely to report them now - it's going to undermine the position of commanders who refer these cases to the court."

Why did the president get involved?

Conservatives and Fox News commentators took up the case after Gallagher was arrested last year, saying he was being maligned by the liberal media and persecuted by the military justice system.

Pete Hegseth, an advocate for veterans and a regular on Fox News, pressed the president to help and Gallagher's family also acted as his defenders.

Image copyright Getty Images Image caption Peter Hegseth

Andrea Gallagher made her view clear to me as I covered the trial in San Diego - her husband was innocent. During one of his Fox News appearances, Gallagher's brother Sean denounced what he described as "false accusations" that were made against his brother.

One of Gallagher's lawyers also had ties to the president as a lawyer for the Trump Organization.

What kind of impact will this have on the Navy Seals?

The commander of the Navy Seals, Rear Admiral Collin Green, ordered a review of the unit earlier this year in order to examine possible wrongdoing within its ranks as well as to improve morale among the members and boost the unit's reputation.

Some say these efforts at reform will be hampered by the president's intervention in the Gallagher case.

Mr Solis is unequivocal in his belief.

"The actions of the president have undermined military justice."