Preheat oven to 350° F.
Bring a large pot of salted water to a boil. Once boiling, add the pasta and cook according to package directions for al dente. Drain the pasta (do not rinse) and return it to the empty pot.
Meanwhile, heat the olive oil in a large skillet over medium heat. Add the onion and cook, stirring often, for 2 minutes.
If the sausage is in casings, remove it from the casings. Add the sausage to the skillet. Crumble and cook the sausage until it browns, about 5 minutes.
Add the garlic and cook, stirring, for 30 seconds.
Add the marinara sauce, Italian seasoning, dried basil, black pepper and red pepper flakes (if using). Stir. Bring the sauce to a simmer. Reduce the heat to a low simmer and cook the sauce for 10-15 minutes.
Remove the skillet from the heat and stir the chopped spinach into the sauce.
Spoon 1 cup of sauce into the bottom of a 13x9-inch baking dish and spread evenly across the bottom of the dish. Gently stir ⅔ of the remaining sauce into the pasta in the pot. Transfer half of the saucy pasta to the baking dish and spread it out into an even layer.
Dollop spoonfuls of the ricotta cheese over the top of the pasta in the baking dish. Sprinkle on ⅓ of the shredded mozzarella cheese and ¼ cup Parmesan cheese.
Spread the remaining pasta in an even layer on top of the cheese. Top with the remaining sauce, mozzarella and Parmesan.
Spray one side of a sheet of foil with cooking spray and use it to cover the baking dish, cooking spray side down (to prevent the cheese from sticking to the foil).
Bake, covered, for 35 minutes. Then uncover and bake for 10-15 minutes more, until the casserole is hot and bubbly and the cheese on top is golden brown. Let rest for 10 minutes before serving.