Unable to concentrate, headaches. Anyone knows what the problem is?

First of all, this is my first time using this forum, so, sorry if I'm doing something wrong. OK so, I'm currently 19. I was always a very smart kid. I was very good at math, solving logical problems, playing chess, and that kind of stuff. That was up until around 17 yes old. I started having a very hard time concentrating. I would sit in class, and things would just fly through my head and not stay in there. And recently I started having really weird headaches. Feels more like dull kind of pain, like my head is about to explode. Every time I try to concentrate on something, like a puzzle or reading, the pain gets more intense. Makes it really hard to do well in my programming classes. I feel like my brain needs some kind of reset or something. I legit feel like I was better at solving puzzles in middle school. And yeah I've been going through some personal issues, debating my religion kind o thing, which was really frustrating. Any idea on what this is? Thanks.