I’m assuming that the 30k to 40k number is assuming most of the internals are scraped. That number seams a bit high. With that said, overhauls include more than just the inside if the engine. They also include overhaul of the fuel injection, other accessories, and often new mags. You could always opt for an IRAN. I had a tear down inspection done at Triad in NC for 16.5k. That basically is a top and bottom overhaul without the accessories. It included repairing the crankcase, regrinding the cam for rust and replacing the cranckshaft with a servicable unit. I like you had a mid time engine and didn’t want to fork out an extra 15-20k to claim 0 SMOH. I would suggest that route before replacing with an unknown engine of the same times. Also, if there trully is a lot of metal, make sure to have anything with metal in it properly flushed. This includes oil cooler, lines, and prop.
If you do opt for a used engine, check for a prop strike. Most of the engines available will be from salvaged aircraft and i would say majority of those will have prop strikes.