How can a circuit be unexpectedly affected by change in light levels?
From a comment I see that your problem was fixed by enabling a pullup resistor in the microcontroller.

That means your original circuit was not "completely functional".  It apparently happened to work in the particular case you tested it at that temperature, at that humidity, with those specific parts, at exactly that supply voltage, and apparently that specific light level.

This is what happens when you violate specs.  In this case you ignored the minimum high and maximum low logic level specs for the input pin.  These are clearly spelled out in the datasheet, along with the maximum pin leakage current in input mode.  Putting all this together, it means the logic state of a floating input is not guaranteed, in addition of course to the usual it being a bad idea for other reasons.

The switch will pull the line solidly one way (apparently low in your case) when closed.  The problem is what happens when the switch is open.  Without something making sure that the pin voltage is above its minimum guaranteed logic high threshold, anything can happen.  This is what the pullup resistor fixed.  It sources more current onto the pin at the minimum logic high level than the pin can leak out, thereby guaranteeing it will be seen as a logic high when nothing else is connected.

The reason light mattered is because various substances can change conductivity slightly depending on light level.  Semiconductor junctions are even more susceptible to light, which is in fact the basis of how photocells work.  Since you were operating the device outside of specs, there was no guarantee of anything, and you got what you got, which in this case happened to be light sensitivity.  The same problem could have occurred at different temperatures, different humidities, and different parts, even from the same batch.

READ THE DATASHEET next time.