import hashlib
from dotenv import load_dotenv
load_dotenv()
from app import app, db
from models import User

def reset_all_passwords():
    with app.app_context():
        users = User.query.all()
        count = 0
        for user in users:
            # Set the password to their 'usuario' (username)
            new_password = user.usuario
            user.password_hash = hashlib.sha256(new_password.encode('utf-8')).hexdigest()
            count += 1
        
        db.session.commit()
        print(f"Successfully reset passwords for {count} users.")

if __name__ == "__main__":
    print("Starting password reset migration...")
    reset_all_passwords()
    print("Done!")
