First-principles theory for Schottky barrier physics