Wow, this is very similar to what my experience with mental health issues has been like. I did well before med school, and looking back, it was because I had a strong support system with me in high school and college. Med school turned out to be a far more isolating experience, most people (including myself) were so busy trying to stay afloat, it was difficult to go out and do fun things with classmates on a regular basis. Also, lots more students were married, had families and just were in different places in their lives, which made it a bit harder to develop relationships. I also went from a small close knit undergrad to a larger research university med school, and I realize now, that large school environment doesn't suit me, it made the isolation that much more difficult.
I ended up going to a smaller satellite campus for my clinical years, and it made a world of difference. Once there, I opened up about my psych issues, and I too was very surprised at how accepting and kind my mentors were. They helped me find care, start treatment, and the smaller environment made me feel much more at home. Now I feel much better.
However I have the same questions...how will I handle residency? My health deteriorated the last few years, and I wonder if it might hurt my health even more down the road.
Yet, there's a few things that are different now and they give me some confidence moving forward. I didn't know I had a mental illness until I got to med school, I always had some issues mentally but I never considered myself to have a problem cause I still did well in high school and college. But as Goro said, med school is a furnace and it brought everything out ot the surface. Unlike before starting med school, for residency, I'm aware that I need to keep my mental health in check. I'm even looking at specialties which might have more regular hours, to allow for some balance in my life, and residency programs that are smaller and close knit. These are things I didn't realize before starting med school, but I'm trying to learn from the setbacks. The things is, the future is uncertain, but I do know that after all the ups and downs, I still want to be here, and I'm thankful to be capable and have the chance to try.
Plus, we need physicians of all backgrounds. Your depression likely gives you an understanding of mental illness that not all doctors may have, and you might empathize with some of your patients really well because of it.