import logging

from django.conf import settings
from django.core.mail import send_mail


logger = logging.getLogger(__name__)


def send_registration_success_email(user):
    """Send the welcome message created-account flows call exactly once."""
    email = (user.email or '').strip()
    if not email:
        logger.warning('Registration email skipped for user %s: no email address.', user.pk)
        return False

    name = user.get_full_name().strip() or email
    message = (
        f'Hello {name},\n\n'
        'Welcome to TheStarFX. Your account has been created successfully, '
        'and you can now sign in and access the platform.\n\n'
        'If you did not create this account, please contact our support team.\n\n'
        'Kind regards,\n'
        'TheStarFX Team'
    )

    try:
        send_mail(
            subject='Registration Successful',
            message=message,
            from_email=settings.DEFAULT_FROM_EMAIL,
            recipient_list=[email],
            fail_silently=False,
        )
    except Exception:
        # The account is already committed at this point; report the mail failure
        # without turning a successful registration into a misleading 500 error.
        logger.exception('Registration email could not be sent to user %s.', user.pk)
        return False
    return True
