"and how does this fit in with the whole evolution selects life best suited for survival-why waste valuable energy producing opium when it has no benefit for the plant??
"
That one's easy. Opium is a poisonous alkaloid. Many plants produce poisonous alkaloids. They have evolved to protect the plant from being eaten by insects. Even the common milkweed produces poisonous alkaloids. The interesting thing is that a species of caterpillar has evolved that is not harmed by those alkaloids, so it eats milkweed exclusively.
Isn't nature amazing?
And typically bitter or foul smelling. Mammal herbivores avoid eating such plants.
Except for the one mammal that has learned to manipulate them.
When used for food, humans learned to harvest the plants when their alkaloid content was lowest: cucumber, bell pepper, and tomato; or to cook them to neutralize the bitter taste: mustard & spinach green, cabbage, etc.
Caffeine, nicotine, cocaine, ephedrine, codeine, and morphine require considerably more processing and are not consumed for the food value, but for it's pharmaceutical effects.
The interesting thing is that a species of caterpillar has evolved that is not harmed by those alkaloids, so it eats milkweed exclusively.
The Monarch butterfly.
And then there's Manduca sexta (tobacco hornworn), a moth that can become "addicted" to the leaves of the nightshade family (tobacco, eggplant, potato or tomato).
The larvae can feed and develop normally on just about any plant, and will readily switch to other types of leaves. But if it hatches and begins eating the leaves of a nightshade, it'll starve to death before it'll eat leaves from another plant.
nature is amazing without doubt..as for opium being a poisonous alkaloid to insects, not so sure opium has any effect on insects.