There just so happens to be a code requiring headlights. When the lot is open to public transportation movement (such as a parking lot) then it is part of the roadway, which is part of the highway. What if there had been someone walking across the lot and you hit them? The stop was valid though even if the initial ticket gets tossed. It reasonable that an officer would stop to investigate when two vehicles are seen in an otherwise deserted parking lot without headlights. Since he was right behind you it's also reasonable to assume that he say you operating the vehicle on the highway (roadway) and since you were intoxicated that is a problem. The officer also has the right (if not the responsability) to conduct an inventory of your vehicle for CYA reasons so the search which turned up your beer is good too.
§ 4302. Periods for requiring lighted lamps.
(a) General rule.--The operator of a vehicle upon a highway shall display the lighted head lamps and other lamps and illuminating devices required under this chapter for differentclasses of vehicles, subject to exceptions with respect to parked vehicles, at the following times:
(1) Between sunset and sunrise.
(2) Any time when the operator cannot discern a person or vehicle upon the highway from a distance of 1,000 feet due to insufficient light or unfavorable atmospheric conditions, including rain, snow, sleet, hail, fog, smoke or smog.

