Optimal Integration of Interconnected Buildings in a Smart Grid: A Bi-level Approach