How to Add Rewrite Ruled Argument Into Permalink Properly

I’m affraid this “nasty” solution would be the most effective than trying to write some cool function that will translate your permalinks into pretty permalinks:

$permalink = rtrim( get_permalink( $id ), "https://wordpress.stackexchange.com/" ) . '/foo' . '/2';

The rtrim part (removing trailing slash) is just for to be sure there are not going to be two slashes on the end of the first part of your new pretty permalink.