This is of course not to be taken as a diagnosis but something to look into.
You might have what’s called avoidant personality disorder.
There’s a subreddit if you’re interesting in learning more: https://reddit.com/r/avpd
One common theme which you alluded to in your post is the feeling of loneliness but also enjoying being alone.
Avoidants crave social interactions but also engage in avoidant behaviour. For example, someone invites you to a party and you decline but you actually really want to go (and might fantasise all night about being there and have pretend conversations in your head).
Again, this is not a medical diagnosis but hopefully can help you search some more and help figure out some stuff about yourself.
Good luck!