It's not that much of a mystery. Certain plants evolved mechanisms against predators that would eat them. The predators, in turn, developed mechanisms to cope with those defenses.
Since a plant cannot get up and run, it can either make itself difficult to eat, using thorns, or other mechanically useful alterations, or it can evolve to taste bad, or contain toxins and anti nutrients that harm the predator.
The keyword there is harm. These classes of plants evolved to selfishly propagate their own species and avoid being eaten. Humans can eat them, but at a price. That price is shorter statute, tooth decay, shorter lives, autoimmune disease, atherosclerosis, and so forth.
As a simple example: good luck trying to prove that living in close quarters with other humans would have caused dental carries. There's only one thing that does that, which is a specific genus of bacteria, and that bacteria doesn't thrive when you eat meat.
Other plants developed in a different pathway, they evolved to attract insects to germinate via pollen and flowers, and produced fruit to encourage mammals to eat them, and carry their seeds further away.
Animals can run, hide, bite, claw, burrow, to escape predation, so they're less likely to need poison, though some, have infact developed venom. We don't tend to predate those that can kill us with venom, we tend to predate those that are less capable of defense.
I would also suppose that if you were to remove all modern fakery in foods, you'll find that we are evolved to find those foods that are healthy for us to be the same that are tasty. If you go back to a time before pollution, eating fresh fish, mollusks, and shellfish is perfectly safe, eating lots of cooked wild boar is perfectly safe, etc.
You weren't likely to eat oxidized fish oil because to us it smells terrible, and it does so as a signal to prevent us from eating possibly harmful things.
On the other hand, grains were very difficult to harvest and left you prone to roving bands of marauders since you were more likely to stay on your land than starve, thus leading to slave labor, taxes, slavery, and governments. These brought with them higher density populations, sure, , but people could usually run off into the woods and start new lives fairly easily so long as water and fauna were plentiful - but the investment of work into land is what mostly kept them there. It was easier to put up with the taxes of roving bandits who eventually declared themselves ordained by this or that god as king, than give up their grain xenomorphine addiction, and thus delegated themselves to serfdom.
We can even see the effects of grains on our pets. Both cats and dogs are obligate carnivores, when fed their naturally evolved diet of meat, they thrive and live long, healthy lives. When fed dry food which usually has gluten as one of the first ingredients, if not the very first, and then has rice, or corn as the next, live short, painful lives with plenty of diseases that are analogous to what we see in ourselves, when we eat the SAD.