# services.py
from django.core.mail import send_mail, get_connection
from django.conf import settings
from django.template.loader import render_to_string

def send_bulk_email(subject, content, prospects):
    """
    Send bulk emails to prospects
    Returns tuple of (success_count, error_count)
    """
    success_count = 0
    error_count = 0

    # Get email connection
    connection = get_connection()

    # You might want to wrap this in try/except if you want to handle connection errors
    connection.open()

    for prospect in prospects:
        try:
            # You could add basic personalization here
            personalized_content = content.replace('{name}', prospect.name)

            send_mail(
                subject=subject,
                message=personalized_content,  # Plain text version
                html_message=personalized_content,  # HTML version
                from_email=settings.DEFAULT_FROM_EMAIL,
                recipient_list=[prospect.email],
                connection=connection,
                fail_silently=False,
            )
            success_count += 1

        except Exception as e:
            error_count += 1
            # You might want to log the error here
            print(f"Failed to send email to {prospect.email}: {str(e)}")

    connection.close()

    return success_count, error_count
