this was a similiar situation that happened to me, and i think i played it bad, what should i have done?

middle position raises 3x
i three bet 9x with pocket 4
( i just do this because pre flop i like to take control of the action if im going to play,)
he calls
all other guys fold
flop
jj6

guy checks

here i figured i was ahead, and i didn't really see any value in cbetting here, as he will only call me with a better hand, and hes not folding any stronger hands. However, i dont want to appear weak either and have him steal the turn. So, maybe if i bet really small, he will spazz with ace high or something and i can call do to weak board...

i bet just 4.5x

(this is like 1/10th of the pot size)

guy raises 40 x (half his stack)

then i was actually kind of scared, and i thought i should fold, but i had a plan, and i wanted to follow through, besides, this looks like a bluff spazz right?

i go all in for 40x more and he calls turning aces

i feel like a complete freaking idiot and lose the pot

WAS I REALLY SLOWPLAYING AN UNDERPAIR?

this play seems really bad to me, what should i have done to avoid catastrophe like this?

See, i thought i had the best hand, and so i figured the only way to get value was inducing a bluff, because i figured i could possibly beat a bluff, but unfortunately i just look like an idiot donk face