Captain James T. Kirk didn't often endure unhappy endings before the first generation of Star Trek movies arrived. He did, however, in “The City on the Edge of Forever” from the series' first season.

Often named as the best story in the entire franchise, the April 6, 1967 episode is also star William Shatner’s personal favorite.

Dr. McCoy is accidentally injected with a dangerous drug that drives him insane. He evades attempts to capture him, instead beaming down to a planet the crew was scanning since it showed signs of emitting time-changing energy. Kirk, Spock and others follow McCoy down and meet the Guardian of Forever, a living machine that has been waiting a million years alone and can provide access to any moment in human history.

Before anyone could stop him, McCoy jumps into the timestream, and does something in the past that wipes out the present as it was known. With apparently no USS Enterprise, no Starfleet and potentially no Earth, Kirk and Spock follow McCoy to correct what he changed.

It turns out he saved the life of kind-hearted mission operator Edith Keeler, who devoted her life to helping New York City's needy during the Great Depression era. When she didn't die in a road accident, Keeler went on to lead a peace movement that delayed the United States from entering World War II, with the effect that Nazi Germany won the war and fascism took over the planet.

The twist is that, by the time Kirk realizes Keeler had to be allowed to perish, they'd fallen in love. Spock tells Kirk: “Save her – do as your heart tells you to do – and millions will die who did not die before,” and he allows the fatal crash to take place.

With their timeline corrected, Kirk, Spock and McCoy return to the present, where the Guardian offers Kirk the opportunity to continue using it as a “gateway.” The captain replies: “Let’s get the hell out of here.”

Watch Kirk Meet the Guardian in ‘Star Trek’

“The City on the Edge of Forever” endured a difficult pre-production process. Writer Harlan Ellison became upset over the revisions made by showrunner Gene Roddenberry and others, and tried to have a pseudonym replace his own in the credits. Since he had the right to do so, Roddenberry employed delaying tactics because he believed that if the industry knew Ellison was unhappy, Star Trek would struggle to attract similarly talented writers.

Along the way, this episode became the most expensive of the show’s first season, costing $245,000 as opposed to the usual $190,000. Production ran two days over the usual shooting time, and several elements of the story didn’t make it to screen because of budget and time issues.

The master stroke, though, was casting Joan Collins as Keeler: He chemistry with Shatner was intense. The big-name actress was originally inclined to refuse the role, knowing nothing about Star Trek, but relented after her children begged her to do it.

Her agent, Tom Corman, argued that she'd “probably be queen of the universe, possess intergalactic powers, wear tight, revealing costumes," Collins subsequently recalled. "A week later I was cast as Edith Keeler, a saintly Earthling, who works as a social worker in a 1930s mission for down-and-out bums in New York's Bowery. Thanks, Tom!”

The script deftly contrasted such a tragic ending with a good deal of humor, mostly courtesy of the banter between Kirk and Spock.

At one point Kirk accuses Spock of being a bit too human, to which Spock replies: “Captain, I hardly believe that insults are within your prerogative as my commanding officer.” Later, after Spock says it will be almost impossible to build the technology they need in the '30s, Kirk responds: “Yes, well, it would pose an extremely complex problem in logic, Mr. Spock. Excuse me, I sometimes expect too much of you.”

Meanwhile, De Forest Kelley's McCoy enjoyed more limelight than usual, hamming it up while delivering another of his “I’m a doctor” lines – in this case, “I’m a doctor, not a psychiatrist.”

Watch Kirk and Keeler Falling in Love in ‘Star Trek’

Roddenberry and particularly D.C. Fontana worked hard to adapt Ellison’s original script into the Star Trek formula, but “The City on the Edge of Forever” still feels like something other than the usual fare. With Collins’ presence drawing a wider audience than usual, it was almost certainly helpful that the story focused on star-crossed lovers rather than starship battles.

“The City on the Edge of Forever” explored a spectrum from friendly comedy to the survival of the race. Like 1986's Star Trek IV: The Voyage Home, however, the episode was ultimately more about human relationships than space adventures.

“So here you have a drama about wanting to change something, not because it’s historical and you’ve got to stop Hitler killing all those people, but because it’s love,” Shatner said during a joint interview with Collins in 2014. “Without this person your life has less or no meaning.”

Shatner's co-star called it “a fantastic idea. Germany was going to control the whole world with its fascism because of me! When I did her backstory in my head I thought, ‘She probably had one big romance when she was 17 or 18 in college, then she went on this crusade.’” Collins added that Keeler’s prescience about the future, including nuclear weapons and galactic voyages of discovery, was what made Kirk fall in love with her: “She was not interested in men; she was interested in saving people – until she met you!”

Collins went on to ask Shatner what might have happened if Keeler had been allowed to live: “Would Kirk have stayed in the 1930s or would he have gone back to his starship?” Shatner replied: “That’s probably why that segment is so interesting, why it’s palpable. … What would you have done? Do you have regrets?”

He called regret a “terrible emotion” that “only makes you sad,” adding that there’s a better alternative: “Trying to say, ‘Okay, well I didn’t do X … but now I can do something else. I can learn and improve from the wrong decision.’ You wish that the pain hadn’t happened, but you realize as you get older that those are the life lessons – and hopefully you make a better decision the next time.”

Watch Kirk Let Keeler Die in ‘Star Trek’

“The City on the Edge of Forever” was a “beautiful story. It was lovely to act. It was well directed and shot – everything,” Shatner told CBS in 2016. “My ideal work as an actor is to be in the moment all the time, to be right there. Sometimes you can’t because you’ve got to hit a mark and then you’ve got to dodge a punch, and it takes you away from being in the moment. But if you can be, if you can exist as the character, those are the finest moments – and, in that show, I was there.”

Shatner occasionally selected other episodes as his favorite, before finally settling on “The City on the Edge of Forever”: “We did 79 [episodes],” Shatner said in 2014, “but this would be my pick of the best.”

Many others have agreed over the years – and not just Star Trek fans. Harlan Ellison’s original script won a Writers Guild of America Award for Best Episodic Drama on Television, while the filmed version earned a Hugo Award for Best Dramatic Presentation. Still, Ellison never forgot the fight over the script. Some members of the Star Trek family believe he submitted the original script to the Guild as an act of spite against Roddenberry.

Ellison went on to dedicate his acceptance speech to criticizing producers who adjust writers’ work – with Roddenberry in the audience. So perhaps some of those involved with this episode didn’t quite live up to its moral about letting go of regrets.

Collins certainly had none – at least when discussing “The City on the Edge of Forever”: “To this day, people still want to talk about that episode – some remember me for that more than anything else I've done,” she later enthused. “I couldn't be more pleased, or more honored, to be part of Star Trek history.”

Asked by Shatner in 2014 if she had any more general regrets, however, Collins answered: “A few husbands!”

28 Classic Films That Were Turned Into (Mostly Failed) TV Shows

Many classic '70s and '80s flicks have spawned TV series - but few have found success.