The impeachment inquiry into U.S. President Donald Trump’s dealings with Ukraine is primarily a story about a president who appears to have weaponized foreign policy in a bid to bolster his own electoral chances. Yet it is also a story about what happens when America’s foreign policy decision-making system collapses.

For nearly three years, national security wonks have been trying to explain why the seeming minutiae of the policy process matters — how the way an administration organizes, or disorganizes, itself profoundly affects the quality, even the basic integrity, of America’s interactions with the world. With each new revelation, the Ukraine scandal is driving this point home.

The basic outline of the story seems to be as follows. Following the election of Volodymyr Zelenskiy as Ukraine’s president, Trump allegedly resolved to use U.S. diplomatic leverage — including nearly $400 million in military assistance allocated by Congress — to prod the Kiev government to investigate the business dealings of former U.S. Vice President Joe Biden’s son, Hunter.

To oversee this outright politicization of American statecraft, the White House constructed a shadow policy apparatus. The U.S. ambassador to Ukraine was removed from her post ahead of schedule. Two Trump loyalists, former New York City Mayor Rudolph Giuliani and Ambassador to the European Union Gordon Sondland, pressured Ukraine’s government to give Trump what he wanted.

They used texts and WhatsApp messages to communicate outside normal channels. Sondland used his personal relationship with Trump to elbow aside the officials who would normally have been charged with handling the U.S.-Ukraine relationship.

All this left much of the national security establishment in Washington — including the National Security Council officials on the Ukraine desk — alarmed and scrambling to find out what was being done in America’s name. This is not how U.S. policy is supposed to work.

Over several decades, the U.S. has refined a robust national security decision-making system to guide the country’s statecraft. This process, overseen by the National Security Council and its staff, brings together key departments and agencies — the Defense and State departments, the intelligence community and many others — with responsibility for international affairs. It creates multiple layers of assessment and consideration of key issues, with policy problems rising through several interagency bodies — the deputies committee, made up of each agency’s No. 2 official; the higher-level principals committee — before reaching the president.

This structure is meant to ensure that important issues are considered carefully, that plausible options are presented to the president and that decisions, once made, are executed faithfully and competently. Overall, the interagency process is staffed by roughly 300 personnel — many of them career civil servants detailed to the NSC from their home agencies — who make the trains run on time.

Some presidents have used the NSC system far more effectively than others. But in general, the existence of this process has improved the quality of American diplomacy and constituted an important check on harebrained or downright unethical policy ventures.

Unfortunately, the Ukraine saga is not an isolated incident: It is the culmination of a debasement of this foreign policy process carried out since Trump became president. During year one of Trump’s tenure, his second national security adviser, H.R. McMaster, tried to establish mechanisms for orderly consideration of key issues, only for the president to flank those mechanisms repeatedly. After McMaster was forced out, his successor, John Bolton, actively undermined normal decision-making procedures. This allowed him to influence the president without resistance from other Cabinet-level officials.

Bolton made the most of that strategy for a while, until he became a victim of his own craftiness. As his hawkish instincts increasingly clashed with Trump’s own preferences, Bolton found himself isolated. The president adopted Bolton’s basic approach — consulting with key confidants instead of using the formal national security structures — but sidelined Bolton himself.

The most spectacular of many humiliations was when Bolton was dispatched on a hasty trip to Mongolia while cable news personality Tucker Carlson, who had publicly called Bolton a “bureaucratic tapeworm,” accompanied Trump for a photo op summit with North Korean dictator Kim Jong Un. Bolton is now out, leaving his successor, Robert O’Brien, with a weakened, often-irrelevant national security apparatus, which may be on the verge of significant personnel cuts.

If Trump’s venality is at the heart of the Ukraine scandal, this breakdown of normal order and discipline also contributed. It takes a broken system, led by a president who appears incapable of distinguishing his personal interest from the national interest, to make a mess like this.

The relevant historical parallel here is less Watergate than the Iran-Contra scandal of the Ronald Reagan years. That involved covert arms sales to Iran, in contravention of a U.S. arms embargo, and diversion of the proceeds to support the Contra insurgents in Nicaragua, in contravention of a congressional ban. In the Ukraine case, too, a presidential administration constructed a parallel foreign policy that was meant to evade normal oversight mechanisms and featured a mixture of government officials and shadowy middlemen.

There were major differences, of course. Iran-Contra was a dark blemish on the record of an administration that otherwise executed one of the more successful, even transformative, foreign policies in U.S. history. The president and his aides made serious errors in the service of good motives: freeing U.S. hostages held by Iranian proxies and preventing a Soviet-backed regime in Nicaragua from consolidating its power.

Yet the Iran-Contra scandal was possible because Reagan tolerated a very messy national security process. “Sometimes our right hand does not know what our far right hand is doing,” he once joked. When the administration’s wrongdoing was revealed, the resulting political crisis nearly derailed Reagan’s presidency.

This fallout highlights the great irony of the current situation: Trump’s behavior, while self-interested, is also self-defeating. Rather than improving his political fortunes, the Ukraine affair has him on a fast track to suffering the moral and political stain of impeachment, even if actual removal from office remains highly unlikely. A president as undisciplined as Trump is just the sort that most needs a functioning national security system — to protect the country, and also himself, from his own worst instincts.

Bloomberg Opinion columnist Hal Brands is a professor at Johns Hopkins University’s School of Advanced International Studies.