WASHINGTON – As much of the country was gripped Wednesday by horrific images from the mass shooting at a Florida high school, two dozen senior Trump administration officials worked frantically into the night to thwart what they considered a different national security threat.

The looming danger on the minds of the officials was a piece of legislation scheduled for a vote the next day in the Senate. It was designed to spare hundreds of thousands of young immigrants known as “dreamers” from deportation – but to the men and women huddled in a makeshift war room in a Department of Homeland Security facility, the measure would blow open U.S. borders to lawless intruders.

“We’re going to bury it,” one senior administration official told a reporter about 10:30 that evening.

The assault was relentless – a flurry of attacks on the bill from DHS officials and the Justice Department, and a veto threat from the White House – and hours later, the measure died on the Senate floor.

The Trump administration’s extraordinary 11th-hour strategy to sabotage the bill showed how, after weeks of intense bipartisan negotiations on Capitol Hill, it was the White House that emerged as a key obstacle preventing a deal to help the dreamers.

The episode reflected President Donald Trump’s inability – or lack of desire – to cut a deal with his adversaries even when doing so could have yielded a signature domestic policy achievement and delivered the U.S.-Mexico border wall he repeatedly promised during the campaign.

Along the way, Trump demonstrated the sort of unpredictable behavior that has come to define his topsy-turvy tenure, frequently sending mixed signals that kept leaders in both parties guessing.

Trump told lawmakers last month he would sign any immigration bill that made it to his desk. At one point in the fall, to the chagrin of some in the GOP, Senate Minority Leader Charles Schumer, D-N.Y., and House Minority Leader Nancy Pelosi, D-Calif., thought they had a deal, giving Trump billions of dollars for the wall in exchange for a “dreamer” fix. Immigration advocates recalled that Trump, last year, had told the dreamers they could “rest easy.”

In the end, Trump remained loyal to restrictionist advisers and allies, who have pressed the president to be true to his hard-line rhetoric on the issue. And Democrats and some GOP centrists are asking whether Trump ever really wanted to reach a deal in the fall when he terminated the Obama-era Deferred Action for Childhood Arrivals program, placing in limbo the lives of nearly 700,000 young undocumented immigrants who were brought to the United States as children.

“People will begin to question the president’s credibility over his statements that he feels empathy for these young people,” said Enrique Gonzalez, a Miami-based immigration attorney who previously served as a policy adviser to Sen. Marco Rubio, R-Fla.

Up against legislation that was gaining steam in the Senate, the administration executed its attack with military precision.

Shortly before 1 a.m. Thursday, DHS blasted out a blistering three-page statement warning that the bipartisan bill would create “a Sanctuary Nation where ignoring the rule of law is encouraged.” Hours later, Attorney General Jeff Sessions declared the bill would “invite a mad rush of illegality across our borders.” White House press secretary Sarah Huckabee Sanders issued a veto threat.

The plan did not stand a chance.

The bill’s demise, along with the failure of three alternative immigration measures, has left the Senate talks in tatters and convinced many on Capitol Hill that nothing will be done in an election year.

A Democratic Senate aide involved in the negotiations said Trump “allowed himself to be pulled 20 times” by his senior advisers after he had tiptoed toward a deal with Democrats. A number of Senate Republicans were on the fence, and “as soon as the president came out against it, we knew they would not go for it,” said the aide, who was not authorized to speak on the record. “To his credit, he leveraged his DACA position to get Democrats to vote for his wall – and yet he still turned it down. He’s not going to get another shot this clean to get a wall. He tossed that away for good.”

Some on Capitol Hill pointed to Trump’s rejection of a different bipartisan proposal last month from Sens. Lindsey Graham, R-S.C., and Richard Durbin, D-Ill., and to the president’s use of a vulgar term to describe African countries, Haiti and El Salvador during a bipartisan immigration meeting – an outburst that made it politically impossible for Democrats to accede to Trump’s demands to terminate a diversity visa lottery program.

In the end, a president who promised to build a border wall paid for by Mexico balked over an immigration deal that would have given him a $25 billion down payment from U.S. taxpayers. A president who last year advised dreamers to “rest easy” and told lawmakers last month at the White House that he wanted a “bill of love” torpedoed an effort backed by 16 senators on Valentine’s Day.

White House aides and Trump allies in Congress fiercely rejected the idea that the president was not operating on the level. In their view, the president moved as close to the middle as possible in the Senate talks – given that any bill with a reasonable chance at becoming law would have to pass the more conservative House.

Trump’s immigration framework, sent to the Hill late last month, included a path to citizenship for 1.8 million dreamers – more than twice as many as enrolled in DACA.

For Republicans, Trump’s willingness to offer a path to citizenship to a much larger group of dreamers meant any Democratic concessions had to go well beyond a border wall.

The president’s demands for large cuts to legal family immigration programs and the elimination of the diversity visa lottery were intended to balance out the legalization of the dreamers, the aides said.

Thanks to a push from Trump’s hard-line advisers and key lawmakers – and conservative media outlets that amplified threats from “chain migration” and the visa lottery – curbs to legal immigration became a central part of the GOP demands.

“If you would have said at the beginning of President Trump’s administration that one year in he is willing to grant a pathway to citizenship for 1.8 million currently illegal immigrants, your jaw would have hit the floor, right? I mean, that is not a concession that is commensurate with a wall,” said Josh Holmes, a Republican consultant who is close to Senate Majority Leader Mitch McConnell, R-Ky. “The opportunity here was to do something further than the bare minimum.”

The bipartisan plan from the Senate’s self-styled “Common Sense Coalition” did not touch the diversity visa program and made relatively minor changes to family immigration rules. But as the “war room” of administration lawyers and policy experts examined the 64-page text on Wednesday, it was a handwritten note on the final page that set off the loudest alarm bells.

That section dealt with setting in law DHS’s priorities for enforcement. Under the proposal, the agency would focus its powers on immigrants with felonies or multiple misdemeanors, who were national security threats and who had arrived in the country after a certain date.

Scribbled in the margins was a date: June 30, 2018.

The administration team was dumbstruck: In addition to making it harder for DHS to deport all of those already here illegally, lawmakers were opening the door to a surge of new unauthorized immigrants by setting an effective “amnesty” date four months in the future.

“No one who has worked on immigration issues in the administration or on the Hill was aware of any legislation that had ever been proposed and scheduled to receive a vote on the floor of the Senate that created an amnesty program effectively for those who arrive in the future,” said a DHS official who helped lead the review. “That would clearly and unequivocally encourage a massive wave of illegal immigration and visa overstays.”

Democrats later explained that the date was an estimate of when key provisions in the bill would begin to take effect after an implementation period. The Trump administration had other objections, but officials said it was that provision that persuaded them to ramp up the coordinated effort to sink the bill.

A Republican Senate aide, whose office worked closely with the White House, agreed that the bill was flawed but expressed surprise at the ferocity of DHS’s opposition.

“If anything, I would have preferred the DHS statement to be a little ratcheted back,” said the aide, who was not authorized to speak on the record. “But I appreciated the clarity of the statement.”

Republican moderates were apoplectic at the administration’s assault on the bill. Graham accused DHS of “acting more like a political organization intent on poisoning the well.” He laid blame at the feet of White House senior policy adviser Stephen Miller and a former Republican congressional aide who now works at DHS – both considered immigration hard-liners.

The same senior official who spoke to The Washington Post on Wednesday night disputed Graham’s account, emphasizing that it was the DHS team, which included a number of career officials who are not political appointees, that led the way in opposing the bipartisan proposal. Given the condensed timetable for the Senate’s floor votes, administration officials said they had no choice but to respond quickly and forcefully.

“This was a pretty dangerous situation,” the official said. “Saying that we had 24 hours to prevent one of the greatest enforcement catastrophes in modern times but somehow we should have calibrated that statement more is ridiculous Monday-morning quarterbacking.”

Trump threw his support behind an alternative bill from Sen. Charles Grassley, R-Iowa, that more closely hewed to his immigration framework. That bill got the least support – 39 votes in favor and 60 against – of any of the plans the Senate considered.

By Friday morning, the political blame game was underway. “Cannot believe how BADLY DACA recipients have been treated by the Democrats . . . totally abandoned!” Trump wrote on Twitter. “Republicans are still working hard.”

Allies of the White House said the president feels emboldened, given that Senate Democrats had caved in three days into a partial government shutdown last month, giving up their initial demand to tie an immigration deal to a must-pass spending bill. Also, Trump’s success in obtaining the concession for the $25 billion for his border wall has now become a starting point in any future talks, they said.

“He’s been very consistent and firm in supporting a four-pillar approach, and I expect him to remain that way,” said Rep. Mark Meadows, R-N.C., head of the conservative House Freedom Caucus, which is pushing Speaker Paul Ryan, R-Wis., to support a more restrictive immigration bill.

But Trump’s rivals said Republicans will be tagged as the anti-immigration party in a nation in which the fastest-growing voting blocs are Latinos and Asian Americans.

“He ended the program,” the Democratic Senate aide said of Trump. “He’s the one who repeatedly said no to bipartisan efforts to fix it.”

The Washington Post’s Ed O’Keefe contributed to this story.