Distributed photovoltaic (PV) generation is typically connected to power distribution grids, which are not designed to host a large amount of production if it is significantly larger than their nominal electricity demand. Given the prominent role of PV in energy transition pathways, modeling the existing power distribution infrastructure's constraints and limitations is key for its reliable techno-economical analysis and expansion. As countrywide models of the distribution grids are, in general, not available, this paper first tackles the problem of estimating medium voltage (MV) distribution grids starting from publicly available datasets. It then proposes a method to estimate the PV generation hosting capacity of such grids and extend it through energy storage systems. As a final contribution and ultimate objective, this paper proposes a method to derive cost-optimal plans for countrywide deployment of PV generation and energy storage systems considering the MV power distribution infrastructure's technical limitations. The distributed PV generation potential is modeled with high-spatially resolved capacity factors. Results are discussed using Switzerland as a case study.