Horses bite and kick each other, or threaten to, all the time. It is how they sort out their social hierarchies. However, they need to be taught that it is just never okay to bite or kick a human, or even threaten to. The amount of pressure or force you use to get this message across will of course depend on what behavior the horse is offering. IME, it is more effective to make a horse move back fast from you as a disciplinary tactic, rather than slap them. In horsey dominance games, the horse that backs off loses that round. So slapping a nippy horse, but letting him stand still, isn’t that effective. He thinks he’s won that round: he bit you, but you let him stand his ground. And slapping on the face is a bad idea because you don’t want to make them headshy. On the other hand, depending on the aggression of the horse, you might need to use the end of the lead-rope or a lunge whip to get them to move off.
I don’t think the horse as described here is acting out in pain. If he was in pain, for instance with gut issues, the nippy behavior would continue through saddling, mounting, riding. This seems to me like dominance behavior: he is treating you like another horse that is getting in his space.
I think he is also playing with you. I don’t mean that this is friendly behavior, but rather that he is testing out his aggressive behavior to see what kind of reaction he’ll get. Young male horses are, IME, most likely to do this. And if they are on pasture with an older boss mare, she will just turn around and drill him if he gets too obnoxious.
As far as taking any steps to train this behaviour out of this horse, since this is a lesson horse in a lesson program, you as the student should be guided by what your trainer has to say on the subject, and everyone who works with the horse should be on the same page.
The other thing that occurs to me is common sense. You say that the horse is fine with you in the stall, fine with you working with him, but is threatening to nip when you come to the front of the stall, stick your hands in, and try to pat him. So don’t do that: don’t try to pat him.