A scandal involving an extramarital affair with a key staffer and an alleged cover-up by law enforcement led Alabama Gov. Robert Bentley to resign Monday after briefly being booked into jail.

The scandal has been a slow-moving train wreck for Bentley, a Republican who surprised the state’s political establishment by winning his party’s nomination in 2010 and then easily won election and reelection in a deeply conservative state.

But beginning during Bentley’s second term, an affair with a key staffer, Rebekah Mason, and the collapse of his marriage led to a steady drip-drip-drip of embarrassing revelations culminating in his likely resignation. And Bentley’s refusal to take one for the team and simply step down created a mounting series of journalistic and legal inquiries that brought more details to light.

We now know, for example, that Bentley mistakenly sent a text to his wife, Dianne Bentley, reading, “I love you Rebekah,” along with an emoji of a red rose. And we know that due to his lack of technical savvy, his text exchanges, made with a state-provided iPhone, also appeared on a state-provided iPad he’d given to his wife as a gift.

In a more closely contested partisan environment, it’s possible that Bentley would have been pushed out much faster to avoid giving the gift of scandal to the Democratic opposition. It’s also possible that he would have been able to rally GOP support behind him, arguing that the whole thing is a fundamentally personal matter that is being ginned up into a scandal by his opponents.

But with Alabama under rock-solid GOP control, the scandal has instead moved forward on a years-long slow burn with rumors of an affair and then evidence of the affair — and of attendant financial improprieties and subsequent abuses of power — coming out over a period of years.

Monday, with the Alabama House of Representatives finally poised to vote on articles of impeachment, Bentley bowed to the inevitable, resigning as part of a plea deal that also included pleading guilty to two misdemeanors, a suspended sentence, and community service. Lieutenant Governor Kay Ivey will assume Bentley’s office.

Massey has sentenced the gov and accepted the terms. It's done. Your gov has been convicted. #alpolitics — Josh Moon (@Josh_Moon) April 10, 2017

It all started with a good old-fashioned sex scandal

Back in fall 2015, Dianne Bentley, the governor’s wife, filed for divorce after 50 years of marriage. That was a tip-off to Alabama reporters that there might be fire behind the smoke of rumors of an affair between Robert Bentley and Rebekah Mason, who had worked for Bentley since his first gubernatorial campaign.

Inside the state government, though, at least some people had been aware of a scandal for some time. Back in August 2014, Stan Stabler of the Alabama Law Enforcement Agency saw a text message from Mason on Bentley's cellphone. Stabler notified his then-boss, Ray Lewis, of the sexual nature of the text message, and three days later, Lewis went to Paul Collier, then the head of the state law enforcement agency, with the information.

Lewis also had an audio recording. The tape, whose contents have since become public, was originally made by Bentley’s wife, and makes the existence of an affair perfectly clear: “When I stand behind you, and I put my arms around you,” Bentley says on the tape, “and I put my hands on your breasts, and I put my hands (unintelligible) and just pull you real close. I love that, too.”

Collier then told Bentley what he knew, recounting later, “I told Governor Bentley there was no need to try and explain it for anything other than it was. It was very obvious that it was sexual in nature.”

According to Collier, he warned Bentley that using state resources to facilitate an affair could turn an embarrassing family matter into a legal problem, so he should make sure to keep his business and personal lives separate. “I told Governor Bentley that I loved him like a father and that there was nothing I wouldn’t do for him, except lie to a grand jury.”

In March 2016, as Bentley’s legal and political problems were mounting, he fired Collier, citing financial improprieties in his department, in an ultimately failed effort to beat back the swirling tide of scandal.

Bentley’s legal problems stem from Mason’s work

What makes the Bentley matter more than a sex scandal is the nature of Mason’s work as a key aide to the governor, which made it essentially inevitable that state resources would end up being used to facilitate both the affair and the subsequent cover-up.

Mason joined Bentley’s first gubernatorial campaign back in 2010 when Bentley was a member of the state House of Representatives. She’d competed in the 1990 Miss Alabama pageant and started working in local broadcast television news after graduating college in 1993. She and her husband also ran a local marketing and advertising firm, from which she jumped to Bentley’s campaign when he was an underdog in the GOP primary. After his victory, she moved to his gubernatorial staff and took up a position as one of his key aides.

Bentley cruised to reelection in 2014 — a great year for Republicans nationally — and by his second term, Mason was reportedly referred to as the “de facto governor” of the state, blamed by many Republicans in the state legislature for pushing Bentley toward more moderate positions on taxes and what they felt was a general lack of fealty to caucus priorities.

Given Mason’s roles in state government, Bentley’s campaigns, and his love life, it was almost inevitable that both public and campaign funds would wind up being intermingled with the governor’s personal situation. And indeed, the Alabama Ethics Commission says that its inquiry into the matter “found probable cause to believe that Governor Robert Bentley committed violations of both the Alabama Ethics Act and the Fair Campaign Practices Act.”

Bentley compounded problems with his abuse of power

But the really outrageous part of this saga isn’t the sex scandal — it’s the abuses of power Bentley allegedly committed in order to cover up his infidelity.

Last week, a special investigator hired by the state legislature to consider impeachment charges against Bentley released his 131-page report on the matter. It concluded that Bentley “directed law enforcement to advance his personal interests and, in a process characterized by increasing obsession and paranoia, subjected career law enforcement officers to tasks intended to protect his reputation.”

Bentley’s wife had longstanding suspicions of an affair between Bentley and Mason, and collaborated with her chief of staff, Heather Hannah, to secretly record Bentley and Mason on the phone. Her goal was to puncture the web of denial around Bentley personally and force him to acknowledge the truth to his family. But as the impeachment report states, “Governor Bentley became obsessed with the existence of the tapes and a desire to prevent them from becoming public.”

This led to efforts to intimidate Hannah (she says he told her, “You will never work in the State of Alabama again if you tell anyone about this,” and, on a later occasion, that she ought to “watch herself”) into silence.

Bentley also used Wendell Ray Lewis, chief of the state’s Dignitary Protection Unit, to try to help him cover up the affair, asking Lewis to chastise women staffers in the governor’s office for gossiping about it, directing him to break up with Mason on his behalf, ordering him to travel to Tuscaloosa to convince Bentley’s son to turn over the tapes, and then marginalizing Lewis after he attempted to intervene to block the use of state resources to further the affair and the cover-up.

Bentley also ordered Collier, the state’s chief law enforcement officer, “to research criminal law and to be prepared to arrest Heather Hannah” as well as “to travel to Greenville to question Director of Scheduling Linda Adams about whether she knew about the recordings.”

The scandal has dragged on forever

The basic contours of the scandal have been known for some time. Bentley had an inappropriate affair with a staff member, lied about it, used state law enforcement personnel to cover up the affair, and even fired the state’s chief law enforcement officer for his reluctance to go along with it. The political fundamentals have also been clear — Bentley clashed with Alabama Republicans on policy matters, offended and embarrassed a culturally conservative state, and had no real defenders in state politics.

Impeachment proceedings nonetheless moved very slowly, because though the Alabama Constitution does make provision for impeachment, it doesn’t spell out the details of the process. No previous governor has ever been impeached.

Bentley didn’t cooperate with the legislature’s investigation, and has consistently maintained that, whatever you think of all this, he did nothing that was actually illegal. The state’s attorney general, meanwhile, tried to slow down the process last fall shortly before Donald Trump’s election victory. Jeff Sessions’s elevation to the Cabinet opened up a Senate vacancy that Bentley swiftly filled with the attorney general in question.

But long before Bentley resigned on Monday, his situation was clearly impossible. His party’s leaders in both houses of legislature have called on him to step down, and the impeachment process is essentially a political one rather than a legal one. Nobody regards Bentley as an indispensable man in Alabama politics, and he had no realistic chance of beating removal proceedings in the legislature.

In the end, he pled guilty in exchange for not being prosecuted for any felonies — a deal that included, as one provision, an agreement to never again seek public office.