There are a great many causes of weight gain, and the only way to truly lose weight and keep it off is to find and address ALL of the possible causes in each person, in a personalised and holistic way. One person's weight gain will be due to eating the wrong foods for their body, but another person's weight gain will be due to stress, or inflammation, or artificial sweeteners and other "diet" products (which have NO calories - oh, the irony), poor sleep quality or quantity, toxins in chemicals in foods or environment, the contraceptive pill, medications, hormone conditions, exposure to artificial light, genetics, increasing age, imbalance of gut bacteria, use of antibiotics, and many more causes...
In reality, as I see in practice, weight gain is very much a combination of several or many of these factors, and NOT just about how many calories someone is eating or not expending.
Can you see yet that calories are irrelevant?
Someone can be eating the best diet possible together with regular exercise, but still put on weight because of these other factors! Simply eating the "right" foods and exercising more will not correct all of the possible causes of weight gain in most people! This is why diets don't work and people who follow the diet/exercise theory almost always gain their weight back again, after all their efforts.
Counting calories is a waste of time! It doesn't work to help anyone lose weight. Focus instead on getting some help and advice on a holistic and personalised plan for you! I'd love to help you improve your health!