import hashlib
from django.conf import settings
from .models import Voter

def generate_voter_hash(email: str) -> str:
    salt = settings.SECRET_KEY
    return hashlib.sha256(f"{email}{salt}".encode()).hexdigest()

def voter_get_or_create_profile(user, ip=None) -> Voter:
    v_hash = generate_voter_hash(user.email)
    voter, created = Voter.objects.get_or_create(
        user=user,
        defaults={'voter_hash': v_hash, 'ip_address': ip}
    )
    return voter