The mechanics come down to three separate documents doing three separate jobs, and the failure mode you named is the right one to be worried about.
The interest you'd hold is a leasehold. A long term ground lease or a lease of a defined portion of the lot can be recorded, and in most states a recorded lease gives notice to later buyers and lienholders so a subsequent sale takes subject to it. Recording practice and what a recorded lease survives against differ state to state, which is why the answer to "does it survive foreclosure" is jurisdiction specific and depends on lien priority. Their existing first mortgage was recorded before your lease, so it has priority, and a foreclosure of that mortgage can wipe out a junior leasehold unless the lender signs a non-disturbance agreement saying it won't. That agreement is the single document the whole structure rests on, and asking an existing residential lender to sign one is often where these deals die. Their loan documents may also restrict leases beyond a certain term or bar granting other interests in the property, so the note and deed of trust need reading before anything else.
Separately, who owns the improvement during the term is a matter of what the lease says, not of default rules. A ground lease can provide that the building is the tenant's property during the term and reverts at expiration, and it can address insurance proceeds, condemnation and what happens if the homeowner dies. Without that language, the structure is a fixture on their land.
There are simpler versions worth pricing before the complicated one. A shorter master lease with a purchase or extension option, or a straight construction contract where the homeowner finances the build and you take a management agreement, both avoid the priority problem entirely and give up upside.
A real estate attorney in that state has to draft this and confirm what survives foreclosure there. If you plan to fund several of these with money from other people, that raises securities questions and needs counsel too.