Linking MacBook Pro and iMac as single system
You could potentially solve this with a product called Barrier and with display sharing software
Note: this is a total bodge and not something I would personally do because you’re likely to spend more time fiddling with it than getting actual work done.
Barrier will let you use your keyboard and mouse on several computers.  Its like a software KVM that works on a client/server model.  All the computers must be on the same network and ideally, you’d use the MacBook keyboard and trackpad to control the iMac.  The external display would be connected to the iMac for simplicity.  To use the MacBook screen, you would need something like Luna Display to extend the display of the iMac to the MBP.
There’s a YouTube video on how Barrier works.  You can also see a demo of Luna Display on Youtube.