"""
Utils for mail
"""
import logging

from django.core.exceptions import ValidationError

from dashboard.models import ProgramEnrollment
from financialaid.api import get_formatted_course_price
from financialaid.constants import (
    FINANCIAL_AID_APPROVAL_MESSAGE,
    FINANCIAL_AID_APPROVAL_SUBJECT,
    FINANCIAL_AID_DOCUMENTS_RECEIVED_MESSAGE,
    FINANCIAL_AID_DOCUMENTS_RESET_MESSAGE,
    FINANCIAL_AID_RESET_SUBJECT,
    FINANCIAL_AID_DOCUMENTS_RECEIVED_SUBJECT,
    FINANCIAL_AID_EMAIL_BODY,
    FinancialAidStatus
)


log = logging.getLogger(__name__)

RECIPIENT_VARIABLE_NAMES = {
    'PreferredName': 'preferred_name',
    'Email': 'email',
}


def generate_financial_aid_email(financial_aid):
    """
    Generates the email subject and body for a FinancialAid status update. Accepted statuses are
    FinancialAidStatus.APPROVED and FinancialAidStatus.PENDING_MANUAL_APPROVAL (documents have been received).
    Args:
        financial_aid (FinancialAid): The FinancialAid object in question
    Returns:
        dict: {"subject": (str), "body": (str)}
    """
    if financial_aid.status == FinancialAidStatus.APPROVED:
        program_enrollment = ProgramEnrollment.objects.get(
            user=financial_aid.user,
            program=financial_aid.tier_program.program
        )
        message = FINANCIAL_AID_APPROVAL_MESSAGE.format(
            program_name=financial_aid.tier_program.program.title,
            price=get_formatted_course_price(program_enrollment)["price"]
        )
        subject = FINANCIAL_AID_APPROVAL_SUBJECT.format(program_name=financial_aid.tier_program.program.title)
    elif financial_aid.status == FinancialAidStatus.PENDING_MANUAL_APPROVAL:
        message = FINANCIAL_AID_DOCUMENTS_RECEIVED_MESSAGE
        subject = FINANCIAL_AID_DOCUMENTS_RECEIVED_SUBJECT.format(
            program_name=financial_aid.tier_program.program.title
        )
    elif financial_aid.status == FinancialAidStatus.RESET:
        message = FINANCIAL_AID_DOCUMENTS_RESET_MESSAGE
        subject = FINANCIAL_AID_RESET_SUBJECT.format(
            program_name=financial_aid.tier_program.program.title
        )
    else:
        # django.core.exceptions.ValidationError
        raise ValidationError("Invalid status on FinancialAid for generate_financial_aid_email()")
    body = FINANCIAL_AID_EMAIL_BODY.format(
        first_name=financial_aid.user.profile.first_name,
        message=message,
        program_name=financial_aid.tier_program.program.title
    )
    return {"subject": subject, "body": body}


def generate_mailgun_response_json(response):
    """
    Generates the json object for the mailgun response.

    This is necessary because of inconsistent Response object formatting. Calling response.json() will return a valid
    JSON-serializable dictionary object, except when the response returns 401 (and maybe others) from mailgun, in which
    it will raise an exception because of improperly formatted content for the .json() call.

    This function solves that problem by raising ImproperlyConfigured if the response returns 401, which will be caught
    by a micromasters.utils.custom_exception_handler():

    Args:
        response (requests.Response): response object
    Returns:
        dict
    """
    try:
        response_json = response.json()
    except ValueError:  # Includes JSONDecodeError since it inherits from ValueError
        response_json = {
            "message": response.reason
        }
    return response_json


def filter_recipient_variables(text):
    """
    Filter out recipient variables, like [PreferredName], and substitute it with %recipient.preferred_name%
    Args:
        text (string): subject or body of the email
    Returns:
        string: with replaced correct recipient variables
    """

    for key, value in RECIPIENT_VARIABLE_NAMES.items():
        text = text.replace('[{}]'.format(key), '%recipient.{}%'.format(value))

    return text


def get_email_footer(url):
    """
    Construct a footer for email
    Args:
        url: To change the settings
    Returns:
        string: with the html styled footer
    """
    text = ("You are receiving this e-mail because you signed up for MITx"
            " MicroMasters. If you don't want to receive these emails in the"
            " future, you can <a href='{0}'>edit your settings</a>"
            " or <a href='{0}'>unsubscribe</a>").format(url)
    return ("<div style='margin-top:80px'>"
            "<p style='margin:auto; color: #757575; max-width:60%; text-align: center;'>{0}</p>"
            "</div>").format(text)

@@ -4220,16 +4220,21 @@
 Masters.
+%3Cbr/%3E
  If you 
@@ -4305,16 +4305,21 @@
  you can
+%3Cbr/%3E
  %3Ca href
@@ -4395,16 +4395,17 @@
 ribe%3C/a%3E
+.
 %22).forma
@@ -4451,16 +4451,53 @@
 top:80px
+; text-align: center; color: #757575;
 '%3E%22%0A    
@@ -4506,17 +4506,19 @@
       %22%3C
-p
+div
  style='
@@ -4529,32 +4529,16 @@
 in:auto;
- color: #757575;
  max-wid
@@ -4544,37 +4544,145 @@
 dth:
-60%25; text-align: center;'%3E%7B0%7D
+50%25;'%3E%3Cp%3E%7B0%7D%3C/p%3E%22%0A            %22%3Cp%3EMIT Office of Digital Learning%3Cbr/%3E%22%0A            %22600 Technology Square, 2nd Floor, Cambridge, MA 02139
 %3C/p%3E
@@ -4702,16 +4702,22 @@
  %22%3C/div%3E
+%3C/div%3E
 %22).forma

Add postal address to email footer (#3922)