Microscopic theory of photon production in proton-nucleus and nucleus-nucleus collisions