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

from apps.carers.models import UserActivationCode


def _get_time_of_day():
    # Get the current time
    current_time = timezone.now()

    # Determine if it's morning or afternoon
    if 0 <= current_time.hour < 12:
        return "morning"
    else:
        return "afternoon"


def send_staff_welcome_email(user, organisation):
    # create uuid
    activation_obj, _ = UserActivationCode.objects.get_or_create(user=user)

    first_name = None

    if user.first_name:
        first_name = user.first_name
    else:
        first_name = user.username

    time_of_day = _get_time_of_day()

    url = reverse('activate_account', kwargs={'uuid': activation_obj.uuid})

    context = {
        'first_name': first_name,
        'username': user.username,
        "time_of_day": time_of_day,
        'url': url,
        'base_url': settings.SITE_BASE_URL,
        'organisation_name': organisation.name,
    }

    html_message = render_to_string("emails/staff_welcome_email.html", context=context)  # Path to your HTML template
    plain_message = strip_tags(html_message)

    message = EmailMultiAlternatives(
        subject=f"Exciting news: You are now part of Peopleoo!",
        body=plain_message,
        from_email='hello@peopleoo.com',  # Uses default from settings.py
        to=[f"{user.email}"],
    )
    message.attach_alternative(html_message, "text/html")
    message.send()

def send_admin_welcome_email(user, organisation):
    # create uuid
    activation_obj, _ = UserActivationCode.objects.get_or_create(user=user)
    first_name = None

    if user.first_name:
        first_name = user.first_name
    else:
        first_name = user.username

    time_of_day = _get_time_of_day()

    url = reverse('activate_account', kwargs={'uuid': activation_obj.uuid})

    context = {
        'first_name': first_name,
        'username': user.username,
        "time_of_day": time_of_day,
        'url': url,
        'base_url': settings.SITE_BASE_URL,
        'organisation_name': organisation.name,
    }

    html_message = render_to_string("emails/admin_welcome_email.html", context=context)  # Path to your HTML template
    plain_message = strip_tags(html_message)

    message = EmailMultiAlternatives(
        subject=f"Exciting news: You are now part of Peopleoo!",
        body=plain_message,
        from_email='hello@peopleoo.com',  # Uses default from settings.py
        to=[f"{user.email}"],
    )
    message.attach_alternative(html_message, "text/html")
    message.send()



def send_organisation_welcome_email(organisation):

    time_of_day = _get_time_of_day()

    url = reverse('org_login')

    context = {
        "time_of_day": time_of_day,
        'url': url,
        'base_url': settings.SITE_BASE_URL,
        'organisation_name': organisation.name,
    }

    html_message = render_to_string("emails/organisation_welcome_email.html", context=context)  # Path to your HTML template
    plain_message = strip_tags(html_message)

    message = EmailMultiAlternatives(
        subject=f"Exciting news: You are now part of Peopleoo!",
        body=plain_message,
        from_email='hello@peopleoo.com',  # Uses default from settings.py
        to=[f"{organisation.email}"],
    )
    message.attach_alternative(html_message, "text/html")
    message.send()
