from celery import shared_task
from utils.celery.celery_base_task import *
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, get_connection
from django.template.loader import render_to_string
import logging, os
from utils.email_templates import appointment_sender_copy, appointment_receiver_copy

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


@shared_task
def send_emails(scheduled_appointment):  # pragma: no cover
    # Send appointment copy to sender
    is_temp_email_service = bool(os.environ.get("IS_TEMP_EMAIL_SERVICE", False))

    if is_temp_email_service:
        appointment_sender_copy(details=scheduled_appointment)
        appointment_receiver_copy(details=scheduled_appointment)
        return

    logging.info(
        f"Sending copy of appointment to sender {scheduled_appointment["email"]}"
    )
    context = {
        "name": f"{scheduled_appointment["first_name"]} {scheduled_appointment["last_name"]}",
        "email": scheduled_appointment["email"],
        "contact_number": scheduled_appointment["contact_number"],
        "company_name": scheduled_appointment["company_name"],
        "branch": scheduled_appointment["branch"],
        "date_and_time": scheduled_appointment["date_and_time"].strftime(
            "%B %-d, %Y, %-I:%M%p"
        ),
    }

    email_connection = get_connection(
        username=settings.EMAIL_HOST_USER,
        password=settings.EMAIL_HOST_PASSWORD,
    )

    email_html_message = render_to_string("email/appointment_sender.html", context)

    email = EmailMultiAlternatives(
        subject="Caracole Appointment Request Received",
        body="An email for dexterton appointment",
        from_email=settings.EMAIL_HOST_USER,
        to=[scheduled_appointment["email"]],
        connection=email_connection,
    )
    email.attach_alternative(email_html_message, "text/html")
    email.send()

    # Send appointment copy to receiver
    logging.info(f"Sending copy of appointment to receiver {settings.INQUIRY_RECEIVER}")
    context = {
        "name": f"{scheduled_appointment["first_name"]} {scheduled_appointment["last_name"]}",
        "email": scheduled_appointment["email"],
        "contact_number": scheduled_appointment["contact_number"],
        "company_name": scheduled_appointment["company_name"],
        "branch": scheduled_appointment["branch"],
        "date_and_time": scheduled_appointment["date_and_time"].strftime(
            "%B %-d, %Y, %-I:%M%p"
        ),
    }
    email_html_message = render_to_string("email/appointment_receiver.html", context)
    email = EmailMultiAlternatives(
        subject=f"Appointment From {scheduled_appointment["email"]}",
        body="An email for dexterton appointment",
        from_email=scheduled_appointment["email"],
        to=settings.INQUIRY_RECEIVER,
        cc=[settings.EMAIL_WEBDEV],
        connection=email_connection,
    )
    email.attach_alternative(email_html_message, "text/html")
    email.send()
