SAN FRANCISCO — An Oakland man hoped to kill 10,000 Bay Area residents, according to court records that reveal a chilling plan of planting bombs around Oakland and UC Berkeley, blowing up a gay bar in San Francisco, setting arson fires around the Berkeley hills and distributing poisoned cocaine in local night clubs.

Amer Alhaggagi, whose last name is also spelled “Al-Haggagi” in court records, was caught on video discussing the plots with an undercover FBI agent.

Alhaggagi pleaded guilty in July to attempting to give material aid to a terrorist organization, stemming from when he helped set up propaganda social media accounts for people connected to the Islamic State, known as ISIL and ISIS. He also pleaded guilty to three identity-theft counts. He appeared in federal court Monday for a sentencing hearing, which was continued to next month.

“I want to make it to a point where every American here thinks twice or three times before he leaves his home,” Alhaggagi said on the video, which was obtained by KQED. “Like, ‘Is it necessary for me to leave right now?’ That’s how I want it to be.”

But in a letter to the court, Alhaggagi claimed the purported plots were fantasies he concocted out of boredom and said he had no intentions of committing terrorism.

“Everything was a joke to me, … I didn’t think anyone was taking me seriously,” Alhaggagi wrote to the court. “I do not support any terrorist organization, or any organization for that matter. It truly saddens me to acknowledge and own up to the fact that it took me to come to this calaboose to elevate my mind from the vacuous state that it was in. I feel really bad for the harm I’ve caused everyone and wasting the FBIs (sic) valuable time.”

Alhaggagi faces up to 47 years in federal prison. Prosecutors asked for a sentence of 33 years, and defense attorneys argued for a far lower figure: four years in prison followed by federal probation.

This case is only the latest in recent memory that involved allegations of a Bay Area terrorist plot in the making. Last year, a Modesto man was charged with plotting a bombing and mass shooting at Pier 39 in San Francisco. He was sentenced to 15 years. In 2014, a San Jose man was implicated in a terror plot in Oakland.

Terrorist or internet troll?

According to court records, Alhaggagi was born in Lodi and attended Berkeley High School from 2008 to 2012. Afterward he worked briefly at Andronico’s grocery store and took college classes. Several friends and family members — including a Berkeley High teacher — wrote letters of support to the court, calling him friendly and peaceful.

But online, Alhaggagi took on a different persona. He spoke of a desire to kill “gays and Jews” in particular and said he wanted to kill UC Berkeley students because “they think they are better than everyone else.”

When agents searched his home, they reportedly found a note claiming responsibility for the planned attacks and saying they were done in the name of ISIS. He said the attack would have three phases: The first would be poisoning “infidels,” then setting a gasoline fire in the Berkeley hills during the day, then a series of bomb attacks. He wrote that he hoped the attacks would inspire copycats.

“As for the final operation, it is only if I were to be exposed and unable to leave the country; if I were surrounded in an area; a direct confrontation with security; either the police or army, the end will be an explosive belt after the ammunition runs out,” he wrote.

Alhaggagi’s lawyer described his client as an internet troll who had a lifelong fascination with getting a rise out of people by coming up with offensive things to say. He said Alhaggagi took this to an extreme end on the internet, but those who knew him well say he would never actually harm others. The defense pointed out that Alhaggagi never bought bomb-making material, despite saying he would.

“Amer Alhaggagi is not a terrorist. He is neither radicalized nor dangerous,” his attorneys Mary McNamara and August Gugelmann wrote in a sentencing brief. They later added: “He believed none of what he said, was surprised when anyone took him seriously and, in fact, is something of a coward. He is truly remorseful for (and mortified by) his atrocious online behavior and what he has put everyone through.”

Federal authorities responded that any attempts to paint Alhaggagi’s actions as “a lark” are “purely false” and that his “appetite for evil knows no bounds.”

Cat and mouse game with the FBI

During the FBI investigation, Alhaggagi reportedly had internet chats with members of ISIS, including a 17-year-old who later admitted to authorities he was part of United Cyber Caliphate, a hacker branch of ISIL. He also spoke with an FBI informant who introduced him to an undercover FBI agent who claimed to have ties to Al-Qaeda.

Alhaggagi met with the agent multiple times and discussed in detail ways to kill hundreds or thousands of people. During their talks, Alhaggagi claimed his uncle in Yemen was a friend of Anwar al-Awlaki, a Yemeni-American imam described by U.S. officials as a recruiter and planner for Al-Qaeda. Al-Awlaki was killed by a U.S. drone strike in Yemen in 2011.

Prosecutors say Alhaggagi eventually became suspicious that the undercover agent wasn’t who he claimed to be and questioned the agent — who made several missteps — about his knowledge of Iran and other subjects. When the agent expressed sympathy for Iran, Alhaggagi became suspicious, even more so when the agent failed to recognize the leader of Al-Qaeda by name.

At their final meeting in Oakland, Alhaggagi reportedly invented a ruse and ran from the agent, and they never crossed paths again. Federal prosecutors called this proof that Alhaggagi had figured out the agent’s true identity, but Alhaggagi’s attorney said it was evidence that Alhaggagi never intended to commit a terrorist attack.

In his letter, Alhaggagi said that after he met with the FBI agent, he began to believe the man was actually plotting terrorist attacks and became extremely concerned. He said his plan was to blow off the agent indefinitely. After the last meeting, he stayed off the internet for more than a month, he wrote, but then went “back to messing with people on the internet especially the ISIS guys.”

“It was a mixture of imprudence and boredom and as I had stated in the very beginning of this paper, I was in a funk! What can I say but that I should of known better, but regretfully I didn’t,” he wrote.

But federal authorities say they have further proof Alhaggagi fully intended to carry out plots: After Alhaggagi was arrested on identity theft charges in November 2016 — before the terrorism charge was filed — two jail inmates came forward saying he had approached them about planning more attacks. The inmates’ statements were filed under seal.

Alhaggagi will be sentenced on Jan. 8, according to federal prosecutors.