import requests
from django.conf import settings
from gauth.models import Location, LocationManager
from django.core.mail import EmailMessage

SENDER = '"Review Monitor" from SignatureCare Emergency Center. <noreply@ercare24.com>'
CC = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
REVIEW_MAINTAINER_EMAILS = getattr(settings, 'REVIEW_MAINTAINER_EMAILS')
host_url = getattr(settings, 'HOST_URI')


def send_email(subject, message_body, to_list, sender=SENDER, cc=CC):
    if not to_list:
        to_list = REVIEW_MAINTAINER_EMAILS

    mail = EmailMessage(
        subject=subject,
        body=message_body,
        from_email=sender,
        to=to_list,
        cc=cc,
    )
    mail.content_subtype = 'html'
    return mail.send()


def send_email_weekly_summary():
    locations = Location.objects.all()
    # locations = Location.objects.filter(location_id='12541597562633926366')
    for location in locations:
        to_list = list(LocationManager.objects.filter(location_id=location.location_id).values_list('email', flat=True))
        subject = f"Weekly Review Report of Google & Facebook at {location.care_name}."
        url = f'{host_url}/analytics/weekly-report/{location.location_id}'
        message_body = requests.get(url).text
        message_body += '<br><p>Regards,</p><p>SignatureCare Review Team</p>'
        send_email(subject=subject, message_body=message_body, to_list=to_list)