Some cells gained photosynthesis by swallowing cells that already had it
Sometimes one complex cell swallows another cell that already has the machinery for photosynthesis and keeps it. This is called secondary endosymbiosis. Instead of getting photosynthesis directly from bacteria, the host gets it by taking in a cell whose ancestors had already done that.