First-principles modeling of molecular switches at surfaces