(CNN)Alex Gansa and the rest of the writers on "Homeland" were in the middle of writing episode 5 of Season 6 when Donald Trump was elected president. It was a shock, to say the least.
'Homeland's' timely 'fake news' storyline hits real nerves among cast
"Obviously the election surprised everybody and we had to adjust in the story room," Gansa, the show's executive producer, told CNN Monday night at an Emmy campaign event. "We did a lot of work trying to figure in this idea that Donald Trump was now the president, and it became quickly apparent then that a lot of stuff that was happening in the real world was [more uncertain] than what we were dramatizing."
At that point, the team had already decided to set the season in the period between a presidential election and the inauguration. They had also made some choices.
For one, their on-screen president was a woman. But, they insisted, President-elect Elizbaeth Keane (Elizabeth Marvel) was not a Hillary Clinton clone.
"Homeland's" president, was a hybrid, "a collage," as Marvel put it to CNN. She was a bit like Shirley Chisholm, with a little George W. Bush, a little Franklin D. Roosevelt, and "a lot of Joe Biden" mixed together, she said.
They'd also decided that the new president would have a contentious relationship with her intelligence community and that "fake news" would play a factor in the season.
"The two main characters that rose to the surface, for me, more than any of the rest of us are 'fake news' and the truth," said Mandy Patinkin, who plays Saul on the show. "That [those stories] became the focal point is pretty serious and pretty important and pretty great."
The season that has unfolded -- the season 6 finale airs on Sunday -- has proven to be closer to reality than anyone could have predicted -- even Gansa.
"Now that the inauguration has happened, things have gotten even more contentious between Trump and the intelligence community, which is what our whole season was about crazily enough," Gansa said. "So some of it was fortuitous and some of it was terrifying, but it is what it is."
In the penultimate episode, Keane's fight with fake news came to a boil as she took on right-wing radio commentator Brett O'Keefe (Jake Weber), whose supporters are drawn to his brash, anti-Keane sentiments.
The character was modeled after Infowars founder Alex Jones.
Keane had been hesitant to give O'Keefe the satisfaction of acknowledged legitimacy, but Saul prompted her to fight back, recalling the "disinformation campaigns" that derailed leadership in Nicaragua, Chile and the Congo.
"You're missing it, all of you. It's happening right now in front of your faces," Saul told Keane and her advisers in the episode. "It does not end well for the elected regime."
So Keane went on O'Keefe's show to defend her late son's name. (O'Keefe had edited and circulated a video that alleged the young soldier died running from danger, instead of as a hero.)
She was armed, too, with a plan to reveal that O'Keefe's audience was mostly made up of "bots" designed to spread his message.
In an earlier episode, the bots were said to be part of a large-scale domestic propaganda machine.
In real life, Senate lawmakers have said they have plan to interview cybersecurity experts about paid Internet trolls working out of a facility in Russia who may have utilized "fake news" throughout the 2016 election.
"We all assume that what we're doing on TV tends toward the fantastical, so it's been intense that it has been so [much like a] docudrama, quite frankly," Marvel told CNN. "It is remarkable how these writers read the tea leaves and see what's coming."
Executive producer Lesli Linka Glatter was, at times, concerned their story trajectory would be thought of as too outlandish.
"I was worried we'd gone too far, honestly," she said. "'Yes, I know this is all real, but it's not in the public zeitgeist.' And by the time it was all being aired, it was."
Claire Danes, who has won two Emmy awards for her role as Carrie Mathison on the series, said she's proud of how the show and its writing team handled "a pretty tricky assignment."
"I think the writers did a pretty incredible job of managing all of that," she said. "And when Trump was elected and our country's fate was determined, I think our writers were really able to metabolize that and make sense of what was really meaningful and relevant from that."
The "Homeland" season finale airs Sunday.