How do I secure a rent or lease agreement before applying for visa? If possible, I do not want to pay the rent in advance. That seems like a red flag to me.
There are a lot of ways to do this. Idealista has rental listings, green-acres, others. Google should turn up rental sites.
It’s highly unlikely anyone is going to let you sign a lease without at least a significant deposit, any more than anyone else anywhere in the world is likely to do such a thing, especially if someone is not right there in front of them and not moving in right away. Can you blame them? That you don’t get a visa and can’t actually show up is hardly the landlord’s problem.
Also, the rental market in Portugal is very, very tight now, especially in desirable areas. Too many people are interested in the country now. Expect to pay.
AFIP on FB is probably a far better resource for D7 information, they have a lot of files on the topic.