Bad news:
You can't use Target Display Mode. Target Display Mode went away when the first 27" Retina 5K iMac came out. Even if you did have an old iMac that supported TDM, Apple added requirements on "the other Mac" that would exclude the M2 Pro Mac mini. See the very restrictive requirements in:
Use your iMac as a display with target display mode - Apple Support
Good news:
If that is a 2019 or 2020 iMac, running Monterey or later, you could set it up as an AirPlay Receiver – and tell the Mini that you want to use it as an AirPlay screen.
System requirements for Continuity features on Apple devices - Apple Support
Set up your Mac to be an AirPlay Receiver - Apple Support
You would need at least one real hardware monitor for the M2 Pro Mac mini, in order to set things up to use the 2019 or 2020 iMac as an AirPlay screen. I don't know what resolutions AirPlay supports (does it support 5K?!?). You might have to ask Apple about that, if that matters to you.