The young boy back to past and he falls in love

Hello, I’m looking for a movie where a boy who travels back in time to the middle ages is mistaken for a wizard because he knows science, and teaches new technologies such as the use of gears, falls in love with a female knight… At the end of the film he wakes up and discovers that he had dreamed everything and that knight was none other than his friend with whom he is in love, perhaps a university classmate

sounds close to “a kid in king arthur’s court”, but the ending is a baseball game