import requests
from django.conf import settings
from gauth.models import Location
from analytics.send_email import send_email

to = getattr(settings, 'ADMIN_MAINTAINER_EMAILS')
host_url = getattr(settings, 'HOST_URI')


def send_email_weekly_summary():
    locations = Location.objects.all()
    # locations = Location.objects.filter(location_id='14748677429039074158')
    for location in locations:
        to.append(location.recipient_email) if location.recipient_email else None
        subject = f"Weekly report for {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(to_list=to, subject=subject, message=message_body)
        if location.recipient_email in to:
            to.remove(location.recipient_email)


def background_task_7_days_interval():
    send_email_weekly_summary()