The cancellation of the planned House vote on the American Health Care Act Friday is a devastating defeat for President Trump, marking the first major legislative setback of his administration.

Yet in some ways, it might also come as a relief to him — because the Republican health bill, crafted mainly by Speaker Paul Ryan, was hideously unpopular, violated many of Trump’s campaign promises, and would have caused a whole lot of pain to a whole lot of voters if it were ever enacted.

As House Democratic leader Nancy Pelosi recently observed, the bill’s failure makes Trump look like a loser. But the bill’s passage could have caused even greater problems for him in the long run.

The interesting question, then, is not why Trump lost but how this allegedly masterful dealmaker ended up in a no-win position in the first place. The answer is surprisingly complex:

Republican leaders designed their legislative strategy around speed and gaming Senate rules to avoid a filibuster, rather than around crafting legislation that could win broad support. Trump decided to let Hill leaders set the legislative agenda rather than demanding they start with other issues or even crafting his own health plan. Ryan took the lead in writing the bill, and his team wrote something that essentially everyone hated — it somehow managed to repel the left, the right, and centrist wonks. Trump decided to stake his prestige on getting the bill through the House, rather than simply disassociating himself from it.

Overall, neither Trump nor Republicans made any attempt to pursue a serious policymaking process here. Through poor policy design, an unrealistic timetable, and their failure to follow changing political winds, they ended up yoked to a horrendously unpopular bill.

“We will need time to reflect on how we got to this moment and how we could do it better,” Ryan said in a press conference Friday. Let’s help him out by taking a walk down memory lane and clarifying how we got here.

The GOP strategy was first determined by the Senate’s budget reconciliation rules

The tale begins shortly after Donald Trump’s shocking victory on November 8, as Republicans in Washington took stock of the new political reality. Unexpectedly, the GOP now had the presidency and majorities in both the House of Representatives and the Senate. And they wanted to do big things with their newfound power.

But they had one big problem: With only 52 senators, they could not overcome Senate filibusters with GOP votes alone — indeed, they weren’t even close to the 60 votes that would take.

Their best chance of passing sweeping new legislation, Republican leaders on the Hill concluded, was to use the Senate’s special budget reconciliation process. That allows certain bills to bypass the filibuster and advance through the Senate with a simple majority.

So far, so good. But then Republicans got a little too clever.

Republicans wanted to use budget reconciliation twice — and they decided to put health care first

Rather than setting one top priority for Trump’s first year, Republican leaders decided they’d have two: Obamacare repeal and comprehensive tax reform. To ensure they wouldn’t have to win over any Democrats, they decided they wanted to use budget reconciliation for both.

By Senate rules, you can’t move two separate budget reconciliation bills affecting spending and revenues at the same time. One would have to go first. Republicans decided that would be Obamacare repeal, for a few reasons.

First, they thought it could be passed quickly. They had already passed it in December 2015, after all. It was vetoed by President Obama, but they thought they’d have sufficient support to do it again.

Second, they thought it would be legislatively easier to write an Obamacare repeal bill, because they intended to put off the hard work of creating an actual replacement until later. This was the “repeal and delay” strategy — pass a quick repeal, set it to go into effect in a few years, and write the replacement in the meantime.

Third, Republicans thought repealing Obamacare first would actually make tax reform itself much easier. That’s because, per Senate rules, anything passed through budget reconciliation isn’t allowed to increase deficits more than 10 years from now. By breaking their preferred tax cuts between the two bills, one of which would include large spending cuts to offset lost tax revenue, Republicans would give themselves the ability to cut taxes even further than they would have with a single bill.

But this strategy meant Republicans had to move on health care fast

To actually put this strategy into action, though, Republicans would have to jump through some procedural hoops in the Senate.

Basically, to use budget reconciliation for a bill, you first have to pass a yearly budget resolution with “reconciliation instructions.” Once you do that, those reconciliation instructions can only be used for one bill that affects both spending and revenue (as Obamacare repeal does).

But Republicans in Congress conceived of a creative but complicated way they could use budget reconciliation twice in 2017. They had never bothered to pass a budget resolution for fiscal year 2017 (something that was supposed to be done in the 2016 calendar year).

So technically, it would be possible for them to ... stay with me here ... 1) pass a pro forma FY 2017 budget resolution purely to set up health care reconciliation, 2) pass Obamacare repeal through reconciliation, 3) pass a full FY 2018 budget resolution to set up tax reform reconciliation, and 4) pass tax reform through reconciliation.

The catch was that in this scenario, Obamacare repeal would have to move very quickly indeed. Republicans wanted to pass the new budget setting up tax reform for reconciliation (step 3) by this spring — and once they did so, the health care reconciliation instructions would expire.

Reports that congressional GOP leaders were consolidating around this strategy began to trickle out in the press in mid-November. And on December 12, Mitch McConnell laid it out in a press conference. “We anticipate doing two budget resolutions,” he said. “The first will be the Obamacare repeal resolution. And then we will do one later in the spring which will largely be dedicated to tax reform.”

Trump himself neglected to set the legislative agenda

You may have noticed one name that hasn’t come up here, and that’s now-President Trump.

That’s because Trump seems to have made no real effort to set the legislative agenda for his first year during the transition, which is rather unusual for a new president. Instead, he and his team seemed content to follow Ryan and McConnell’s lead, trusting their judgment about what had the best chance of getting done.

Trump “approved the agenda putting health care first late last year, almost in passing, in meetings with Mr. Ryan, Vice President Mike Pence and Reince Priebus, the White House chief of staff,” the New York Times’s Glenn Thrush and Maggie Haberman report. (Trump’s selection of Ryan ally Priebus as his chief of staff may have contributed to his deferential approach here.)

The import of this decision becomes clearer when you think about what else Trump could have done at this point:

He could have demanded that tax reform happen before health reform.

He could have decreed that a big infrastructure bill — a topic that has an incredible 90 percent approval rating from the public — would be his top priority.

He could have actually followed through on his promises of writing his own health plan.

He could have at least demanded that Ryan’s health bill be consistent with his campaign’s own promises on health care.

Yet Trump seems to have been told, essentially, that he couldn’t do any of this. He’s publicly said multiple times that he thinks he had to do health care first, and that starting with tax reform wasn’t an option — even though that’s not really accurate. Here’s what he said in February:

I can’t do it until we do health care, because we have to know what the health care is going to cost and — statutorily — that’s the way it is. So for those people who say, “oh, gee, I wish we could do the tax first,” it just doesn’t work that way. I would like to do the tax first.

The result was that Trump was gradually backed into a position where he was stuck with whatever Ryan managed to cook up on health care as his first major legislative priority.

The strategy began to go off the rails when Republicans rejected repeal and delay

When the 115th United States Congress was sworn in this January, step one of the Republican strategy unfolded as planned. That is, both houses of Congress swiftly passed a budget resolution that set up Obamacare repeal to pass through budget reconciliation.

But then GOP leaders encountered a problem. Their members, in both the House and the Senate, turned out to really hate the “repeal and delay” strategy, because it meant getting rid of Obamacare and its benefits without the “replacement” the party had long promised they’d offer being ready.

This came as a surprise to some, since practically every Republican in Congress had voted to pass Obamacare repeal without any replacement many times before. But it became clear that many of those votes were merely protest votes, used for messaging and taken with the comfort that President Obama’s veto would prevent them from actually going into effect.

Now that the GOP had unified control of Washington, the dynamic was different. The phrase “we’re shooting with live bullets now” became popular around the Hill. It suddenly seemed unacceptable for the governing party not to have a replacement plan.

And there was another big problem — if the “replace” bill was put off until later, it couldn’t advance through budget reconciliation. That means it would have to win over eight Democratic senators to beat a filibuster. Many Republicans feared this would never happen, and worried that after they started the countdown clock toward repeal, they’d be unable to pass a replacement and would get stuck with the blame.

So Republican leaders adjusted. The new strategy, they decided, was to do both repeal and as much of “replace” as possible in the budget reconciliation bill. (Publicly, they maintained they could pass other measures through regular order later as part of a “three prongs” strategy, but nobody really believed them.)

This new approach had its own problems, though. For one, the Senate’s intricate rules about what kinds of provisions could pass through the budget reconciliation process meant Republicans couldn’t include some parts of a replacement they liked (such as allowing insurers to sell health plans across state lines).

Even more urgently, the intricate legislative calendar — with two budget resolutions and two reconciliation bills — meant the health bill had to happen fast, or it would hold up everything else.

This timeline made sense for repeal and delay, when many of the tough details of how to overhaul the health care system could be put off until later.

But now that they had to write a replacement, Republicans were stuck trying to completely overhaul the American health care system absurdly quickly. All the ordinary steps to winning broad support for a transformative piece of legislation — holding hearings, wooing over stakeholder groups, selling the bill in public, passing amendments — had to be short-circuited to stick to this timeline.

Rather than adjust their calendar or overall strategy, however, Republicans decided to forge onward.

Trump’s presidency started off controversially — and Obamacare became more popular

As all this was happening, politics shifted underneath Republicans’ feet.

Trump’s inauguration as president was greeted a day later by what may have been the largest nationwide protests in American history. Trump failed to signal that he’d unify the country during the transition, and he started office remarkably unpopular for a new president.

With controversial policies — like the travel ban aimed at people from several majority-Muslim countries — and his overall erratic behavior, his popularity soon declined even further. Still, his approval among Republican voters remained quite high, so there was still some hope that he could bring GOP legislators along with him on a legislative agenda.

But then a funny thing happened: Obamacare, which had long polled rather poorly, surged in popularity now that it was on the chopping block:

For the past few years, voters have consistently viewed the Affordable Care Act more unfavorably than favorably, except for a few brief blips. But with Trump taking office, things flipped. Obamacare still isn’t overwhelmingly popular, but now it’s clear that most Americans aren’t desperate for its repeal.

Enthusiasm, too, was suddenly stronger among the bill’s defenders. Republican members of Congress were dogged by town hall protests and phone calls in favor of Obamacare, in a mirror image of what Democrats supporting the law faced in 2009 and 2010. The bill’s opponents, meanwhile, suddenly didn’t seem so engaged.

The cold feet among many in the congressional GOP got even colder. A health policy discussion at a closed-door Republican congressional retreat in late January — which was secretly recorded and leaked to the Washington Post’s Mike DeBonis — revealed that the party was divided, uncertain, and deeply concerned about how and even whether to move forward. Congress members worried about pulling “the rug out from under” people covered by Obamacare, or “walking into a gigantic political trap.”

Then Paul Ryan wrote a terrible bill nobody liked

By late February, it was clear that repealing Obamacare would be a tougher lift than many initially expected. GOP leaders calculated that the best approach was not to slow down but to move even more quickly.

“Republican leaders are betting that the only way for Congress to repeal the Affordable Care Act is to set a bill in motion and gamble that fellow GOP lawmakers won’t dare to block it,” the Wall Street Journal’s Louise Radnofsky, Kristina Peterson, and Stephanie Armour reported on February 27.

With speed the goal, Speaker Ryan’s office decided to short-circuit the House’s traditional process of crafting legislation in committee, instead writing the bill themselves behind closed doors.

What they cooked up turned out to be disastrous.

The American Health Care Act, released on March 6, was loved by hardly anyone and loathed by many.

The bill’s essential features were: 1) for the individual insurance marketplaces, it replaced Obamacare’s income-adjusted subsidies with a flat and overall less generous tax credit; 2) it overhauled and dramatically cut Medicaid; and 3) it slashed some taxes that almost entirely hit the wealthy.

According to Congressional Budget Office estimates, the bill would have caused 24 million more Americans to lose coverage by 2026, compared with the status quo. It would have choked off future Medicaid funding for states, which would likely have led them to drop people the rolls or give them worse coverage.

The AHCA would also have had particularly devastating effects on older, poorer Americans who had to buy insurance on the individual marketplaces — CBO projected a 64-year-old making $26,500 would see her premiums rise by 750 percent. As a result, key stakeholder groups such as the American Hospital Association and the AARP came out in opposition to the bill.

Meanwhile, conservatives were furious that the bill kept too much of Obamacare, by leaving many regulations on the insurance markets in place and creating that new tax credit. “It creates a new entitlement program,” said Rep. Louie Gohmert (R-TX). Soon, conservative anti-spending groups like Heritage Action, the Club for Growth, and Freedom Partners came out in opposition. Even right-leaning wonks who were harsh critics of Obamacare criticized the AHCA as poorly designed and unlikely to achieve its aims.

All this opposition had consequences. A poll from Quinnipiac University released this week found, astonishingly, that a mere 17 percent of the public approved of the Republican bill, while 56 percent disapproved. “In a hyper-partisan political climate, it's actually an accomplishment to write legislation this unpopular,” political scientist Ryan Enos tweeted.

Trump staked his prestige on Ryan’s bill passing, but lacked the knowledge or ability to craft a workable compromise

The AHCA was clearly on the ropes. It also clearly violated several of Donald Trump’s campaign promises — it cut Medicaid by more than $800 billion despite Trump’s pledge not to do so, and it utterly fell short of his promise to provide “insurance for everybody” even if they couldn’t afford it.

Yet politics and the desire to score a big legislative “win” seem to have driven Trump to get behind what he hoped would be a parade. After all, he had repeatedly promised on the campaign trail to repeal and replace Obamacare, and this bill would do that.

So rather than disassociate himself from a controversial and troubled legislative effort, he endorsed it. In public statements and tweets, the president declared his support for the AHCA and called on the House to pass it. In doing so, he ensured that the bill’s defeat would be viewed as a failure for him.

By mid-March, it became clear that the AHCA was facing opposition from two groups of Republicans in Congress. There was the Freedom Caucus — the holdout conservatives who thought the bill preserved too much of Obamacare. And then there was a de facto “Coverage Caucus” — made up of Republicans worried the law would cause too many of their constituents to lose their insurance or benefits. (Indeed, it turned out that despite their constant criticism of Obamacare, many Republicans didn’t actually want it to go away after all.)

Since the two groups had essentially opposite demands, winning over enough from each side to pass the bill would clearly be a difficult task. Only a truly masterful dealmaker could have gotten it done.

And that wasn’t Donald Trump.

Though the president called and met with House member after House member, he failed to craft a suitable deal. Indeed, even in his public statements Trump seemed conspicuously unable to make a substantive case for why Ryan’s legislation was even good. In a revealing moment, Tucker Carlson asked the president about how “the counties who voted for you would do far less well under this bill.” Trump responded by saying, “Oh, I know,” and blithely claimed any problems would be fixed later.

Members of Congress really would have preferred hearing about why Ryan’s bill was actually good and worth supporting. But instead of making an affirmative case for it, Trump kept returning to simple talking points about Obamacare being a “disaster,” and how if Republicans didn’t pass the bill they’d do badly in the next election. He seemed to have little familiarity or comfort with health policy, and it’s not clear he could even describe what the GOP bill did.

Even in private, Trump failed to impress. One Freedom Caucus source told the New Yorker’s Ryan Lizza that Trump was “in over his head” and “seems to neither get the politics nor the policy of this.” After Trump spoke to the House Republican Conference on health care Tuesday, Rep. Walter Jones (R-NC) came out of the meeting saying Trump gave “no details” on policy and only said the GOP would have “political problems” if it failed, per CNN’s Manu Raju. That wasn’t persuasive to Jones, who remained a no.

So on Thursday, the president tried his final trick — he cut off negotiations and demanded the House vote tomorrow. It was an attempt to transform the vote into a test of personal loyalty to Trump. He hoped that holdout Republicans were bluffing.

But it turned out to be Trump who was the bluffer. When Ryan traveled to the White House to make clear that they were well short of the votes they needed, the president soon agreed not just to cancel the vote but to abandon the effort entirely. It was time to cut his losses and move on to other things. “Obamacare,” Ryan said Friday afternoon, “is the law of the land.”