On Friday, Donald Trump said that Fox News's Megyn Kelly had it out for him during the first Republican presidential debate. And he had a theory as to why.

Update: Coverage of the second Republican debate.

"She gets out and she starts asking me all sorts of ridiculous questions," Trump told CNN. "You could see there was blood coming out of her eyes, blood coming out of her ... wherever."

So how did we get to the point where the leading Republican candidate for president is accusing one of Fox News's signature hosts of going on a PMS-fueled rant against him? The answer is surprisingly complex.

It's not just about what happened at Thursday's debate. It's also about the way Fox News had, until Thursday, been inflating the Trump bubble, and the broader tension between Fox News's role as a ratings-obsessed cable network, an actual journalistic outlet, and one of the most important institutional actors in the Republican Party.

But let's start with what happened at the debate, and why it left Trump feeling so betrayed.

There's been an internal war at Fox News over Donald Trump

Until Thursday, Fox News had been one of Trump's most important allies. The liberal media watchdog group Media Matters notes that between May 1 and July 31, 2015, Donald Trump was given, by far, the most airtime of any GOP presidential contender, with 31 appearances on the network; Jeb Bush, by contrast, only had seven.

And more than simple airtime, Fox News's hosts defended Trump when the rest of the media was piling onto his more noxious comments.

When Trump said Mexico was sending rapists and criminals into America, Fox News contributor Monica Crowley said Trump "is saying things that need to be said."

When Trump blasted Sen. John McCain's war record, Fox News's Steve Doocy said, "If you listen to his comments in total ... he's not critical of John McCain the war hero, he's critical of his Senate record." Harris Faulkner said, "McCain is not admitting that he kind of started this whole thing."

Erik Bolling, a member of Fox News's The Five, said, "I like what Donald Trump is saying, I like what he's doing." On Fox and Friends, Trump was compared to St. Augustine and Mr. Smith from Mr. Smith Goes to Washington. And on his radio show, Sean Hannity, one of Fox's key primetime personalities, gave Trump the ultimate compliment. He compared him to Ronald Reagan:

One of the greatest moments of Ronald Reagan's presidency, he was at Reykjavik. And Gorbachev was pushing him to give up Strategic Defense, what liberals called, derisively, 'Star Wars.' And he said, 'nyet' [no], and he walked away from the table. And eventually it led to peace. That's like Trump's art of the deal.

But there's been at least one powerful critic of Trump at Fox News: Rupert Murdoch. On July 22, New York magazine's Gabriel Sherman, author of a biography of Roger Ailes, reported that there was a schism at Fox News over Donald Trump.

According to sources, Murdoch has tried — and failed — to rein in Fox News Chairman and CEO Roger Ailes, who, insiders say, is pushing Fox to defend Trump’s most outlandish comments. This week, Ailes told his senior executives during a meeting that Murdoch recently called him and asked if Fox could "back off the Trump coverage," a source told me. Ailes is said to have boasted to his executives that he told Murdoch he was covering Trump "the way he wanted to."

Murdoch loathed Trump so much that he took to Twitter to make his feelings known. "When is Donald Trump going to stop embarrassing his friends, let alone the whole country?" he asked.

Fox News went after Trump at the GOP debate

Donald Trump's post-debate tantrum has been an embarrassment. But it's not, on some level, a surprise.

After months of having Fox News as his main ally, Trump stepped onto the Fox News debate stage and suddenly found the network seemingly committed to his destruction.

The first question was designed to embarrass Trump in front of a national audience of Republicans. Bret Baier began the debate by asking, "Is there anyone on stage, and can I see hands, who is unwilling tonight to pledge your support to the eventual nominee of the Republican party and pledge to not run an independent campaign against that person?"

Trump had already made clear that he wouldn't take that pledge. But Fox News began the debate by making sure every Republican in the country knew Trump wouldn't take that pledge. It was a question designed to embarrass him, and Baier kept turning the screws.

"Experts say an independent run would almost certainly hand the race over to Democrats and likely another Clinton," Baier told Trump. "You can't say tonight that you can make that pledge?"

And, for Trump, it went downhill from there.

Megyn Kelly's first question for Trump exposed his rampant misogyny in front of a national audience. "You've called women you don't like 'fat pigs, dogs, slobs, and disgusting animals,'" she began.

Trump, of course, interrupted. "Only Rosie O'Donnell," he said with a smile.

Kelly wasn't having it. "For the record, it was well beyond Rosie O'Donnell," she replied. "You once told a contestant on Celebrity Apprentice it would be a pretty picture to see her on her knees. Does that sound to you like the temperament of a man we should elect as president, and how will you answer the charge from Hillary Clinton, who is likely to be the Democratic nominee, that you are part of the war on women?"

Trump's answer was a master class in how to excuse sexism and wield the politics of white male resentment. "I don't frankly have time for total political correctness. And to be honest with you, this country doesn't have time either," Trump replied.

"Honestly, Megyn," he continued, "if you don't like it, I'm sorry. I've been very nice to you, although I could probably maybe not be, based on the way you have treated me. But I wouldn't do that."

It turned out he would do that. But we'll get to that in a second.

The next question to Trump came from Chris Wallace. And here, again, the question was designed to embarrass the candidate.

"Mr. Trump, it has not escaped anybody's notice that you say that the Mexican government, the Mexican government is sending criminals – rapists, drug dealers, across the border. ... You have repeatedly said that you have evidence that the Mexican government is doing this, but you have refused or declined to share [that evidence]. Why not use this first Republican presidential debate to share your proof with the American people?"

But the demolition effort didn't end with the questions. As soon as the debate ended, Fox News cut to a focus group being conducted by pollster Frank Luntz. The entire segment was about how the focus group came in liking Trump and left loathing him. It was a festival of Trump hatred. You can watch it here:

Look at the debate from Trump's perspective. His onetime friends at Fox News crafted the questions to embarrass him and then, once he was off the air, cut to a focus group — and who knows if that was a real focus group or actors who were coached on what to say — who told the whole country that Trump had lost the debate.

And there's nothing Donald Trump hates like being called a loser.

Trump's Twitter tantrum

Remember when Trump said, to Kelly, "I've been very nice to you, although I could probably maybe not be, based on the way you have treated me. But I wouldn't do that"?

Turned out he would do that.

The next day on Twitter, Trump had a full-on anti-Kelly tantrum.

I really enjoyed the debate tonight even though the @FoxNews trio, especially @megynkelly, was not very good or professional! — Donald J. Trump (@realDonaldTrump) August 7, 2015

Wow, @megynkelly really bombed tonight. People are going wild on twitter! Funny to watch. — Donald J. Trump (@realDonaldTrump) August 7, 2015

Trump also attacked some other Fox News personalities after the debate — notably Frank Luntz — but his assault has been vicious and personal toward Kelly in a way that it hasn't been towards her co-moderators Baier and Wallace, though their questions for Trump were similarly designed to expose his greatest weaknesses. It's almost as if Trump particularly hates being questioned by powerful women.

Why Fox News turned on Donald Trump

Fox News is a strange beast. It is a conservative advocacy organization run by a longtime Republican operative. It is a profit-hungry cable network run by a talented media executive. And it is a news operation that employs some talented journalists who want to be taken seriously by their peers.

These missions conflict with each other. Fox News wants the Republican Party to win elections, but it also wants American politics to be a ridiculous circus that fires up conservative voters. It employs hacks like Steve Doocy and Sean Hannity but also hosts people like Bret Baier, Megyn Kelly, Shepard Smith, and Chris Wallace who, while they might be conservative, pride themselves on actually being journalists.

And what makes all this harder is that Fox News is tremendously powerful. It is arguably more powerful in shaping the opinions of GOP voters than the official Republican Party apparatus. It's no accident that the first Republican debate was held on Fox News. Of course it was. The Republican Party needs Fox News more than Fox News needs the Republican Party — something the GOP learned when Fox devoted endless airtime to pumping the rise of the Tea Party.

As heterodox conservative commentator David Frum said, "Republicans originally thought that Fox worked for us, and now we’re discovering we work for Fox."

This is the dilemma that Trump now faces. He originally thought Fox worked for him. He was on all the time, he was helping them get higher ratings, and they seemed to love him. But then a few things happened.

First, Fox's promotion of Trump worked too well. He went from making the Republican primary more interesting to follow — and thus better for ratings — to dominating the Republican primary and potentially harming the ideological movement that Fox supports. Murdoch's public opposition to Trump was a signal that the network wasn't likely to tolerate Trump actually becoming the Republican nominee.

Second, Trump had spent much of his time in the hackier corners of the network — outposts of conservative inanity like Fox and Friends. But the debate was led by Fox's more serious personalities. Baier, Kelly, and Wallace want to be known as some of the toughest questioners in news business — and Trump was a chance to prove their journalistic bona fides to the world. So where other Fox personalities wanted to treat Trump well in the hopes he would come back on their shows, Baier, Kelly, and Wallace wanted to embarrass him.

Finally, Fox News's incentives had switched. Early in the campaign, the way to get bigger ratings was to build Trump up. But now the whole country was tuning in, and what most people wanted to see was Trump torn down — or at least the fight that would result if Fox News tried to tear Trump down. And that's what they got. It was extraordinary television, and it led Fox to the highest ratings for any cable news program ever broadcast. Fox figured out how to profit off Trump coming and going, and, better yet, the network got to decide when Trump was coming and when Trump was going.

In a war with Fox, Trump will likely lose

Now Trump and Fox News are at war. And as Nate Silver writes, this is a war Trump probably can't win.

Until now, Trump has mostly been fighting with institutions that Republicans mistrust — like the media, and the Republican establishment in Washington, DC. But 80 percent of Republicans trust Fox News. And Fox News is the most reliable source of cable airtime for Republican candidates trying to reach Republican voters.

Perhaps as importantly, there are few parts of the contemporary conservative movement that aren't woven into Fox News. So Fox has allies that go well beyond its walls. An example is Erick Erickson of RedState.com, who disinvited Trump from a RedState event after his comments about Kelly. Erickson, who has his own history of sexist commentary, is an influential conservative in his own right, but he is also a Fox News contributor.

So far, Trump had found he can divide and conquer by separating a certain segment of the conservative base from the Republican Party. But it's much harder to cleave conservatives from Fox News, because, both in terms of money and exposure, it's much more important for leading conservatives to be in the good graces side of Fox News than in the good graces of Donald Trump. The angrier Trump makes Fox News, the fewer friends he will find he has.

Trump's feeling that he's been betrayed by Fox News is understandable, even if his reaction to it is gross and childish. But his declaration of war against a major Fox News personality is unwise, and may mark the beginning of the end of his campaign.

On Monday, Trump tweeted that Fox News CEO Roger Ailes had called him and all was now well:

Roger Ailes just called. He is a great guy & assures me that "Trump" will be treated fairly on @FoxNews. His word is always good! — Donald J. Trump (@realDonaldTrump) August 10, 2015

But in some ways, the interesting question isn't what Ailes said to Trump, but what Trump said to Ailes. After all, for a network that calls itself "fair and balanced" when it is anything but, a promise to treat Trump "fairly" might mean less than Trump hopes. And even if Ailes isn't playing with loaded dice, "fair" treatment looks very different than the lovefest Trump was enjoying prior to the debates.

Watch: How Donald Trump is trolling the Republican Party