
import datetime

from django.conf import settings
from django.core.mail.message import EmailMessage
from django.template.loader import render_to_string 

from .models import EmailHistory, PasswordHistory

def send_activation_email(new_user, current_site):
    """ Send activation email to the user """

    subject = 'UseVoice. | Account Confirmation'
    message = render_to_string(
        'accounts/email_activation.html', 
        {'user': new_user, 'domain': current_site.domain, 'token': new_user.token('email_verification')}
    )

    mail = EmailMessage(
        subject,
        message,
        to=[new_user.email],
        from_email=settings.EMAIL_HOST_USER,
    )

    mail.content_subtype = 'html'
    mail.send()

def send_password_reset_email(user, current_site):
    """ Send password reset email to the user """
    subject = 'UseVoice. | Password Reset'
    message = render_to_string(
        'accounts/email_password_reset.html', 
        {'user': user, 'domain': current_site.domain, 'token': user.token('password_reset')}
    )
    mail = EmailMessage(
        subject,
        message,
        to=[user.email],
        from_email=settings.EMAIL_HOST_USER
    )

    mail.content_subtype = 'html'
    mail.send()


def check_email_provider_allowed(new_email):
    allowed_providers = ['gmail', 'yahoo', 'outlook', 'hotmail', 'icloud']
    email_provider = new_email.split('@')[-1].split(".")[0]
    return email_provider in allowed_providers

def check_email_domain_allowed(new_email):
    allowed_domains = ['.edu', '.gov', '.com', '.ma', '.fr']
    domain = new_email.split('@')[-1]
    return any(domain.endswith(d) for d in allowed_domains)

def email_change_limit_reached(user):
    # Check if user has reached email change limit
    # This could be done by checking the number of times the user has changed their email in the past month
    return False



def email_change_limit_reached(user):
    limit = 3  # Change this to the desired number of allowed email changes
    time_period = 30  # Change this to the desired time period (in days)
    time_threshold = datetime.datetime.now() - datetime.timedelta(days=time_period)
    email_changes = EmailHistory.objects.filter(user=user, timestamp__gte=time_threshold)
    if email_changes.count() >= limit:
        return True
    return False

def password_change_limit_reached(user):
    limit = 3  # Change this to the desired number of allowed email changes
    time_period = 7  # Change this to the desired time period (in days)
    time_threshold = datetime.datetime.now() - datetime.timedelta(days=time_period)
    email_changes = PasswordHistory.objects.filter(user=user, timestamp__gte=time_threshold)
    if email_changes.count() >= limit:
        return True
    return False

def send_confirmation_email(new_email):
    subject = 'Confirm your email change'
    # message = 'Please click the following link to confirm your email change: http://example.com/confirm-email-change/'
    # from_email = 'noreply@example.com'
    # recipient_list = [new_email]
    # send_mail(subject, message, from_email, recipient_list)

def contact_us_send_message(email,sender_name,subject,message):
    message = message
    mail = EmailMessage(
        subject = subject,
        body = message+f'\nfrom {sender_name}.',
        to=['brahim.boughanem@usevoice.io'],
        from_email=email
    )
    mail.send()