Pedro Gerson is a professor of law and economics at the Ibero-American University and the Mexico Autonomous Institute of Technology (ITAM), both in Mexico City.

Over the past few months — both as a candidate and in his new capacity as U.S. president — Donald Trump has issued a series of provocations that have brought U.S.-Mexico relations to their lowest point since the Mexican-American War of 1846. His brash Twitter diplomacy moved Mexican President Enrique Peña Nieto to take the unprecedented step of canceling a trip to the United States. Stepped-up raids by U.S. immigration authorities have stirred deportation fears in the Mexican immigrant community, and expressions of anti-Mexican sentiment have become so common that even a top Mexican diplomat recently endured public harassment. Last but not least, Trump has continued to vilify the North American Free Trade Agreement (NAFTA), raising doubts about its durability.

It would seem that there could be no bleaker scenario for the Mexican president. And yet Trump may actually turn out to be a blessing in disguise for Peña Nieto. In fact, Trump may offer Peña Nieto’s only chance of salvaging his party’s chances in next year’s general election — and potentially even his own legacy.

At 12 percent, Peña Nieto has the lowest approval rating of any Mexican president in history. Since 2013 the administration has committed one blunder after another. Most notably, the president has borne the brunt of a major corruption scandal involving his wife and a lavish home, managed to mishandle the largest human rights tragedy in recent Mexican history and, perhaps most unbelievably, legitimized candidate Trump by inviting him to Mexico on what looked like a state visit. Peña Nieto’s responses to all of these cases and crises has seemed at best incompetent, in some cases downright negligent.

Nevertheless, Trump offers Peña Nieto a welcome distraction and, more importantly, an opportunity. On the first point, the existence of a new villain, if nothing else, has given Peña Nieto a break from the negative headlines of the past few weeks. An unscientific survey of the four most-read newspapers in Mexico showed that, since his inauguration, Trump has been mentioned almost three times more often than Peña Nieto.

Now Peña Nieto is well positioned to strike back, and his options are many. Mexicans all along the political spectrum are offering the president suggestions on how to do this. A leftist collective called on the government to start accepting refugees in defiance of Trump’s Muslim ban. Jorge Castañeda, a former minister of foreign affairs, has said that Peña Nieto should accept deported migrants from the United States only if they can prove that they are Mexican citizens. Given that many immigrants lack any identification, this measure would severely hamper deportation efforts. Others have recommended that Peña Nieto should collaborate with officials in the United States who are openly pro-immigration, appearing in public with figures such as California Gov. Jerry Brown. Finally, some have even argued that Peña Nieto should refuse discussions on NAFTA in order to avoid a potentially devastating period of economic uncertainty during the renegotiation. If this were to happen, Trump would have an answer for NAFTA’s demise to the largely Republican constituency that benefits from it.

Policies like these would represent a total reversal of the Mexican administration’s current timid approach. Rather than responding to Trump’s bravado with strength, Peña Nieto dumped his minister of foreign affairs in favor of someone with ties to Trump’s inner circle. He also vacillated on the cancellation of his trip to the United States even after Trump issued an executive order mandating the border wall construction. Finally, Peña Nieto continues to insist on having an “open dialogue” with Washington.

Politically, confronting Trump may be Peña Nieto’s last shot at securing a win for his party in the 2018 presidential election. Currently, the president’s Institutional Revolutionary Party (PRI) would seem to have no chance against Andres Manuel López Obrador, the left’s candidate. Nevertheless, the PRI has a strong machinery that can guarantee 25 to 30 percent of the vote. If Peña Nieto stands up to Trump, he may be able to tap into nationalist sentiment to win the hearts of that 5 to 10 percent of the electorate that his party will need to win the election.

Also, it is likely that such actions turn out to be better policy options than the continuance of Peña Nieto’s overtures. Trump has demonstrated that he will not budge regardless of the number of olive branches that are thrown at him. A firmer stance may in fact be the only way to protect Mexicans abroad and to give Peña Nieto room to negotiate with Trump.

If Peña Nieto were to pursue a more confrontational route, he would be returning to some of his party’s foundational ideas. The PRI was founded in the 1920s in the wake of the Mexican Revolution, and gained traction over the next decades by enacting nationalistic policies such as the nationalization of oil and the glorification of the “Mexican race.” The boogeyman of the story was, of course, the United States, a country that had unjustly taken a large chunk of Mexican territory. Eventually the party and the country shifted their tunes, opening Mexico to the United States and the world. Even so, suspicion of the gringos has never really ended.

Peña Nieto could thus leverage the same nativist sentiments that Trump and the Brexiters have exploited. The difference is that he must temper his nationalism with a commitment to openness to all those who seek collaboration with Mexico. And promises won’t suffice — he will have to follow up his rhetoric with policy measures that stand in contrast to Trump’s: commitment to free trade, openness to immigration at home and defense of Mexican citizens in the United States. Given Peña Nieto’s current unpopularity, however, even this strategy may not be enough to make voters change their minds about him and his party.

Adopting such a confrontational stance undoubtedly runs counter to the Mexican president’s own nature, which favors caution and compromise. Yet the realization that he has nothing to lose may persuade him, in the end, to stand up to Trump. Peña Nieto and Mexico can only benefit.