import requests

from celery import shared_task
from django.conf import settings


@shared_task
def send_login_email_task(email, url):
    message_body = f"Use this magic link to login to your Dashboard:\n\n{url}"
    # Send mail via Mailgun HTTP API
    response = requests.post(
        f"https://api.mailgun.net/v3/{settings.MAILGUN_DOMAIN}/messages",
        auth=("api", settings.MAILGUN_API_KEY),
        data={
            "from": "adman@howdy.earthmanrpg.com",
            "to": email,
            "subject": "A magic login link to your Dashboard",
            "text": message_body,
        }
    )

    # Log any errors
    if response.status_code != 200:
        print(f"Mailgun API error: {response.status_code}: {response.text}")
