A Theoretical Framework for Microscopic Surface and Interface Dipoles, Work Functions, and Valence Band Alignments in 2D and 3D Halide Perovskite Heterostructures