import logging

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.conf import settings

logger = logging.getLogger(__name__)


def send_reviewer_added_email(call, request):

    emails_dir = "marinerg_test_access/emails/reviewer/"
    template = emails_dir + "invite_reviewer"

    reviewers = call.board_members.all()

    url = f"{request.scheme}://{request.get_host()}/access_calls/detail/{call.id}"

    for reviewer in reviewers:

        username = reviewer.email
        if reviewer.first_name and reviewer.last_name:
            username = f"{reviewer.first_name} {reviewer.last_name}"

        context = {
            "call_title": call.title,
            "user_name": username,
            "link": url,
        }

        text = render_to_string(template + ".txt", context=context)
        html = render_to_string(template + ".html", context=context)

        msg = EmailMultiAlternatives(
            subject="MARINERG-i: Invitation to review board",
            body=text,
            from_email=settings.DEFAULT_EMAIL_SENDER,
            to=[reviewer.email],
            headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
        )

        msg.attach_alternative(html, "text/html")
        msg.send()


def send_chair_added_email(call, request):

    username = call.board_chair.email
    if call.board_chair.first_name and call.board_chair.last_name:
        username = f"{call.board_chair.first_name} {call.board_chair.last_name}"

    prefix = f"{request.scheme}://{request.get_host()}"
    url = f"{prefix}/access_calls/detail/{call.id}"

    context = {"user_name": username, "call_title": call.title, "link": url}

    emails_dir = "marinerg_test_access/emails/chair/"

    template = emails_dir + "chair_added_to_call"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: Invitation to chair access call",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=[call.board_chair.email],
        headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def send_review_ack_email(review):

    username = review.reviewer.email
    if review.reviewer.first_name and review.reviewer.last_name:
        username = f"{review.reviewer.first_name} {review.reviewer.last_name}"

    context = {
        "user_name": username,
        "call_title": review.application.call.title,
        "application_id": review.application.id,
    }

    emails_dir = "marinerg_test_access/emails/reviewer/"

    template = emails_dir + "review_acknowledgement"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: Review Received",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=[review.reviewer.email],
        headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def send_decision_ack_email(decision):

    username = decision.reviewer.email
    if decision.reviewer.first_name and decision.reviewer.last_name:
        username = f"{decision.reviewer.first_name} {decision.reviewer.last_name}"

    context = {
        "user_name": username,
        "call_title": decision.application.call.title,
        "application_id": decision.application.id,
    }

    emails_dir = "marinerg_test_access/emails/chair/"

    template = emails_dir + "decision_acknowledgement"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: Decision Received",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=[decision.reviewer.email],
        headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def send_decision_email(decision, request):

    logger.info("Sending decision email")

    if decision.decision == "1":
        decision_str = "ACCEPT"
    else:
        decision_str = "REJECT"

    prefix = f"{request.scheme}://{request.get_host()}"
    url = f"{prefix}/access_applications/detail/{decision.application.id}"

    context = {
        "decision": decision_str,
        "call_title": decision.application.call.title,
        "decision_link": url,
    }

    emails_dir = "marinerg_test_access/emails/applicant/"

    template = emails_dir + "application_decision"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: Access Application Decision",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=[decision.application.applicant.email],
        headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def email_applicant(application):
    context = {
        "application_id": application.id,
        "call_title": application.call.title,
        "feedback_form_url": "",
        "email_contacts": "marinergi@ucc.ie and ross.oconnell@ucc.ie",
    }

    emails_dir = "marinerg_test_access/emails/applicant/"

    template = emails_dir + "application_submitted"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: Access Application Received",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=[application.applicant.email],
        headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def email_reviewers(application, request):

    emails_dir = "marinerg_test_access/emails/reviewer/"
    template = emails_dir + "request_review"

    reviewers = application.call.board_members.all()

    prefix = f"{request.scheme}://{request.get_host()}"
    url = f"{prefix}/access_applications/detail/{application.id}"

    for reviewer in reviewers:

        username = reviewer.email
        if reviewer.first_name and reviewer.last_name:
            username = f"{reviewer.first_name} {reviewer.last_name}"

        context = {
            "application_id": application.id,
            "call_title": application.call.title,
            "user_name": username,
            "submission_date": application.submitted,
            "review_link": url,
        }

        text = render_to_string(template + ".txt", context=context)
        html = render_to_string(template + ".html", context=context)

        msg = EmailMultiAlternatives(
            subject="MARINERG-i: Review Requested",
            body=text,
            from_email=settings.DEFAULT_EMAIL_SENDER,
            to=[reviewer.email],
            headers={"List-Unsubscribe": f"<mailto:{settings.DEFAULT_EMAIL_UNSUB}>"},
        )

        msg.attach_alternative(html, "text/html")
        msg.send()


def email_facility_for_review(application):
    return
    context = {
        "domain": "http://localhost:4200",
        "application_id": application.id,
        "call": application.call.title,
        "facility_name": (
            application.chosen_facility.name if application.chosen_facility else ""
        ),
    }

    # Email applicant and facility
    emails_dir = "marinerg_test_access/emails"

    template = emails_dir + "/facility/invite_facility_application_review"
    text = render_to_string(template + ".txt", context=context)
    html = render_to_string(template + ".html", context=context)

    msg = EmailMultiAlternatives(
        subject="MARINERG-i: New Access Application for Review",
        body=text,
        from_email=settings.DEFAULT_EMAIL_SENDER,
        to=["reviewer_email"],
        headers={"List-Unsubscribe": "<mailto:unsub@example.com>"},
    )

    msg.attach_alternative(html, "text/html")
    msg.send()


def send_application_submit_emails(application, request):

    email_applicant(application)

    if application.call.notify_reviewers:
        email_reviewers(application, request)
