Six weeks ago, Mexican President Enrique Peña Nieto stood alongside President Obama in the White House and pledged his “absolute will” to cooperate with whichever candidate won the U.S. presidential election.

Even that banal political statement landed with alarm in Mexico. That’s because the night before, the man who formally accepted the Republican nomination for U.S. president was Donald Trump, who built his campaign around calling Mexican immigrants criminals and vowing to wall off the border and roll back trade ties.

Peña Nieto’s comment during the July 22 visit to Washington, however, hinted at the already brewing notion among some factions of the Mexican government that a meeting with Trump might be worth organizing. In the weeks that followed, Mexican cabinet members and advisers to Peña Nieto debated fiercely about whether to invite Trump to Mexico, either before or after the election, according to people here familiar with the discussions.

The result was Trump’s surreal appearance Wednesday, calmly talking about his plans for the border wall as he stood alongside Peña Nieto, who hardly spoke a critical word about the American candidate. The event marked a dramatic departure from Mexico’s historic caution about getting involved in U.S. presidential politics and has been roundly criticized in this country, where Trump is widely loathed. The meeting has divided the Mexican government and is being portrayed as a sign of ineptitude from Peña Nieto, who already had dismal approval ratings.

“This is probably the single worst public relations disaster of his entire administration,” said Jorge Castañeda, who was Mexico’s foreign minister from 2000 to 2003. “The substance of [Peña Nieto’s] entire message should have been that the wall and the deportations and revisiting NAFTA are all unacceptable positions to Mexico, and all would constitute serious threats to the U.S. relationship with Mexico.”

Republican presidential nominee Donald Trump is slated to meet with Mexican President Enrique Peña Nieto as he tries to clarify his past comments about Mexico and immigration. (Jenny Starrs/The Washington Post)

Mexico has long sought to carefully calibrate its relations with its more powerful neighbor and chief trading partner — seeking good ties but insisting on respect for its sovereignty and its citizens’ contributions. But Trump’s bid for the presidency has utterly flummoxed the Mexican government. Its response has veered from one extreme to the other, with officials first dismissing Trump’s candidacy, then abruptly replacing the low-key Mexican ambassador in Washington this past spring and crafting an aggressive public relations strategy to counter Trump’s claims and show Mexico’s value to Americans. In March, Peña Nieto compared Trump to Hitler.

While it is common for American presidents to visit Mexico soon after being elected, a high-profile meeting between the Mexican president and an American candidate is quite unusual. In 2008, Sen. John McCain (R-Ariz.) visited Mexico while a presidential candidate, along with other senators, and met privately with then-President Felipe Calderón. But that didn’t have the state-visit flavor of Trump’s appearance.

Andres Rozental, who served as Mexican ambassador to Britain and was a career diplomat for more than 35 years, expressed astonishment that the Mexican government gave Trump a formal greeting at the presidential hangar, flew him by helicopter to the presidential palace and allowed him to appear with Peña Nieto before the world’s media.

The planning “was entirely done in secret and outside of the Foreign Ministry’s knowledge,” he said. “It was certainly done in an extremely amateurish and totally unprofessional way.”

Critics said the Mexican president not only took a huge political gamble but appeared to botch the execution of his strategy.

“Peña Nieto meddled in the electoral process of the United States, and as I see it, what was the use?” asked Lorenzo Meyer, a history professor at the College of Mexico. “Mexico is a weak country, and we have to take great care with symbols — it’s almost the only thing we have — and he gave to Trump international exposure.”

Economic concerns

Over the summer, Finance Minister Luis Videgaray, who had been Peña Nieto’s campaign manager in the 2012 election, was a leading advocate for the Trump visit, according to Mexicans familiar with the deliberations, who spoke on the condition of anonymity to be candid. Videgaray, an MIT-educated economist, had been a state-level leader of the Institutional Revolutionary Party, and his political career had advanced along with Peña Nieto’s. The president selected Videgaray to be his behind-the-scenes liaison to the Trump campaign.

Videgaray and other aides saw a Trump meeting as a political risk that was worth taking, in case Trump won the election, the sources said. But some senior members of the Mexican government strongly argued against such an invitation — among them, Foreign Minister Claudia Ruiz Massieu.

The Mexico City daily Reforma reported Friday that Videgaray was especially concerned after ratings agencies Moody’s and Standard and Poor’s had lowered their outlooks on Mexico’s sovereign debt to negative in recent months because of the state of the country’s public finances. Trump has threatened to slap a 35 percent tariff on many imports from Mexico, potentially making things worse. Videgaray wanted to bring Trump to calm financial-market fears over the potential impact of his victory on the country’s economy, the newspaper reported.

“This was the type of political decision that was not popular but was needed,” said one person in Mexico who was familiar with the decision-making, describing the advocates’ position. “This was going to cost us in the polls, but it was important to meet face to face with the most dangerous candidate on the turf.”

Videgaray’s spokeswoman, Claudia Algorri Guzman, said that the “idea and the decision” to invite Trump were Peña Nieto’s. Any other account is “false,” she said.

Peña Nieto wrote in an editorial that ran on the front page of Mexico’s El Universal newspaper Thursday that it is his responsibility to meet with both U.S. presidential candidates, but especially Trump, “because there are things that he should hear in person from the President of Mexico, beginning with how Mexicans feel.”

“First, I was very clear — in public and in private — in emphasizing that in Mexico we feel offended and hurt by his pronouncements about Mexicans,” he wrote.

Peña Nieto’s three-page invitation, dated Aug. 25, was delivered to Trump’s campaign headquarters in New York by courier on the next day, a Friday. Hillary Clinton received an invitation the same day.

“Dear Mr. Trump,” began the note to the Republican candidate. “On November 8th, the American people will choose the next President of the United States of America. I am sure that the electoral process will be one of vibrant debate, contrast of ideas and intense citizen participation, honoring the great democratic tradition of America.”

The letter referenced Peña Nieto’s recent meeting with “my good friend President Barack Obama,” noted the huge volume of trade between the two countries and called for strengthening their partnership.

“Therefore, it would be a great honor to meet with you and have a direct conversation about the common future of our nations,” the letter concluded. “For this purpose, I have instructed the Secretary of Foreign Relations to contact your office.”

Left out of the loop

Some Mexican officials who opposed the invitation didn’t realize that a visit would happen so fast, and on Trump’s terms. It occurred on the same day he gave an immigration speech in Phoenix.

“Things got out of control,” said the Mexican familiar with the decision-making. “This was mishandled, to say the least.”

Key parts of the Mexican government were not fully informed about the invitation and Trump’s quick acceptance. The U.S. Embassy was alerted to the visit by the Secret Service, which was arranging security for the trip, but by Tuesday afternoon the American diplomats still hadn’t received final confirmation of the visit.

On Tuesday, Ruiz Massieu, the foreign minister, was in Milwaukee, unaware that Trump would be landing in Mexico City the next day. In her speech in the United States, she emphasized the importance of trade and the contributions of undocumented workers to Wisconsin’s economy, and she appeared to take a jab at Trump.

“The facts speak against the stereotypes,” she said. “History against intolerance. Cooperation against xenophobia.”

The next afternoon, as Trump stood alongside Peña Nieto, Ruiz Massieu sat with other cabinet members in the front row, a funereal look on her face.

Karen DeYoung in Washington and Gabriela Martinez in Mexico City contributed to this report.