from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
from django.urls import reverse

def send_welcome_email(user, request=None):
    """
    Send a welcome email to a newly registered user.
    
    Args:
        user: The User object of the new user
        request: The request object, used to build absolute URLs
    """
    subject = 'Welcome to Bloggers Event Management System'
    
    # Create context for the email template
    context = {
        'user': user,
        'login_url': request.build_absolute_uri(reverse('events:login')) if request else settings.SITE_URL + '/login/'
    }
    
    # Render the HTML content
    html_message = render_to_string('email/welcome.html', context)
    
    # Create plain text version for email clients that don't support HTML
    plain_message = strip_tags(html_message)
    
    # Send the email
    send_mail(
        subject=subject,
        message=plain_message,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=[user.email],
        html_message=html_message,
        fail_silently=False,
    )

def send_proposal_confirmation_email(proposal):
    """
    Send a confirmation email when a user submits a proposal.
    
    Args:
        proposal: The Proposal object that was submitted
    """
    subject = f'Proposal Submission Confirmation - {proposal.cfp.event.title}'
    
    # Create context for the email template
    context = {
        'proposal': proposal,
        'cfp': proposal.cfp,
    }
    
    # Render the HTML content
    html_message = render_to_string('email/cfp_submission_confirmation.html', context)
    
    # Create plain text version for email clients that don't support HTML
    plain_message = strip_tags(html_message)
    
    # Send the email
    send_mail(
        subject=subject,
        message=plain_message,
        from_email=settings.DEFAULT_FROM_EMAIL,
        recipient_list=[proposal.submitter.email],
        html_message=html_message,
        fail_silently=False,
    )
