You are automatically considered a fiscal resident if you spend more than 183 days / year in Spain. You are then expected to pay taxes here on your world-wide income. If you also want to apply for a residence permit, this site is very helpful:
https://www.justlanded.com/english/Spain/Spain-Guide/Visas-Permits/Residency-and-NIE
You will need a medical certificate that contains a very specific statement, and in most cases, proof of medical insurance.