If you bought it privately and didn't inspect it first, you'll need to suck it up and fix the issues and move on. If you got it from a dealer, you could try having them fix it. You'll be hard pressed to just "get my money back".
Regarding the issues, E61 wagons are known for sunroof issues, do some research and you should be able to find how to manually close it and then never touch it again as IIRC there is a piece in them that breaks and is a pain to fix. Lots of them leak (I think both of the ones i looked at buying were leaking, and one had mouldy carpet).
Tailgate struts can be replaced, do some research on that too.
I hate to point it out, but you should also look into N62 V8 engine issues too. If it doesn't have valve stem seal issues now, it will at some point.
They're a very nice car when they're in good shape and working well. If you like it, its worth fixing it and enjoying it.