Preheat your oven to 350°F (175°C). Bring a large pot of salted water to a boil. Cook the spaghetti according to the package instructions until al dente. Drain and set aside.
While the spaghetti is cooking, heat a large skillet over medium heat. Add the Italian sausage, breaking it up with a spoon as it cooks. Cook until browned, about 5-7 minutes. Drain any excess fat.
Add the chopped onion to the skillet with the meat and cook for about 5 minutes, or until softened. Stir in the minced garlic and cook for another 1-2 minutes until fragrant.
Pour the marinara sauce into the skillet with the meat mixture. Add the dried oregano, dried basil, red pepper flakes (if using), and salt and pepper to taste. Simmer the sauce for 5-10 minutes, allowing the flavors to meld.
In a medium bowl, combine the ricotta cheese, 1 cup of shredded mozzarella, grated Parmesan, and the egg. Mix until well combined.
In a 9x13-inch baking dish, spread a thin layer of the meat sauce on the bottom. Add half of the cooked spaghetti, followed by half of the remaining meat sauce.
Spread the ricotta cheese mixture evenly over the sauce. Top with the remaining spaghetti and then the rest of the meat sauce. Sprinkle the remaining 1 cup of shredded mozzarella cheese over the top of the casserole.
Cover the baking dish with aluminum foil and bake in the preheated oven for 20 minutes. Remove the foil and bake for an additional 10-15 minutes, or until the cheese is melted and bubbly.
Let the baked spaghetti cool for a few minutes before serving. Garnish with chopped fresh parsley and enjoy!