This is not quite correct.
Idiot is an a very ancient term (deriving from Ancient Greek basically meaning a private citizen but also being used to imply someone who was a bit dim) that long-predates modern medicine and indeed any attempt to systematize mental illness or deficiency. From the 1300s onwards it was used to mean both people with mental disabilities and to insult people.
Imbecile is a bit more of obscure in origin, but is used first seemingly as an insult, albeit on implying a general weakness of character and body. It starts being used regularly as a general insult long before modern medicine appears.
Both were terms that medicine decided to then use with a more precise/technical meaning in the 1800s/1900s. So the actual chain of events is insult/descriptor then medicine appears and adopts the term to describe a more technical condition, but society keeps using it as an insult, and maybe even does it more after the medical definition.
Moron, however, you are correct about - the first known usage is medical, and it was then used as an insult.