Preheat the oven to 375°F (190°C) and bring a large pot of salted water to a boil and cook the pasta until just al dente according to package directions; drain and set aside.
In a large skillet over medium heat, warm the olive oil. Add the chopped onion and sauté until softened. Stir in the minced garlic and cook until fragrant.
Add the crushed tomatoes, oregano, red pepper flakes (if using), salt, and pepper.
Add pasta and simmer the sauce for 10–12 minutes until slightly thickened and stir in the halved olives, then remove the sauce from heat.
In a mixing bowl, combine ricotta, Parmesan, egg, and chopped basil or parsley until smooth. Season lightly with salt and pepper.
Add the drained pasta to the sauce and toss until evenly coated. Spread half of the sauced pasta into the prepared baking dish. Dollop the ricotta mixture evenly over the pasta layer.
Top with the remaining sauced pasta and sprinkle shredded mozzarella over the top.
Bake in the preheated oven for 20–25 minutes, until the cheese is melted, bubbly, and lightly golden.
Remove from the oven and let the dish rest for a few minutes before garnishing with fresh basil.