from atunsapp.models import mailer


def get_user_display_name(user):
    """
    Return the most appropriate display name for a user.
    """
    first_name = (
        getattr(user, "first_name", "")
        or getattr(user, "firstName", "")
        or ""
    )

    last_name = (
        getattr(user, "last_name", "")
        or getattr(user, "lastName", "")
        or ""
    )

    full_name = f"{first_name} {last_name}".strip()

    if full_name:
        return full_name

    username = getattr(user, "username", "") or ""

    if username:
        return username

    return "there"

def send_login_notification(user):

    if not user.email:
        return

    user_name = get_user_display_name(user)

    subject = "New Login Detected"

    message = (
        "<p>"
        "We detected a successful login to your ExactSub account."
        "</p>"

        "<p>"
        "If this was you, no further action is required."
        "</p>"

        "<p>"
        "If you do not recognize this activity, please change "
        "your password immediately to help secure your account."
        "</p>"
    )

    mailer(
        user.email,
        subject,
        message,
        user_name,
        preheader="A successful login to your ExactSub account was detected."
    )


def send_welcome_email(user):

    if not user.email:
        return

    user_name = get_user_display_name(user)

    subject = "Welcome to ExactSub — Your Account Is Ready"

    message = (
        "<p>"
        "Welcome to ExactSub. Your account has been "
        "created successfully."
        "</p>"

        "<p>"
        "You can now sign in and start using our "
        "services with ease."
        "</p>"

        "<p>"
        "We're glad to have you with us."
        "</p>"
    )

    mailer(
        user.email,
        subject,
        message,
        user_name,
        button_text="Go to ExactSub",
        button_url="https://exactsub.com/",
        preheader="Your ExactSub account is ready."
    )


def send_wallet_funding_notification(
    user,
    amount,
    new_balance,
    funding_source=""
):

    if not user.email:
        return

    user_name = get_user_display_name(user)

    subject = "Wallet Funding Successful"

    funding_method = (
        funding_source
        or "Wallet Funding"
    )

    message = (
        "<p>"
        "Your ExactSub wallet has been funded successfully."
        "</p>"

        "<p>"
        "<strong>Amount credited:</strong> "
        f"₦{amount:,.2f}"
        "</p>"

        "<p>"
        "<strong>New wallet balance:</strong> "
        f"₦{new_balance:,.2f}"
        "</p>"

        "<p>"
        "<strong>Funding method:</strong> "
        f"{funding_method}"
        "</p>"

        "<p>"
        "Your funds are now available for use on ExactSub."
        "</p>"
    )

    mailer(
        user.email,
        subject,
        message,
        user_name,
        button_text="Open ExactSub",
        button_url="https://exactsub.com/",
        preheader=(
            f"₦{amount:,.2f} has been added "
            "to your ExactSub wallet."
        )
    )


def send_activation_email(user, domain):

    if not user.email:
        return

    from atunsapp.tokens import account_activation_token
    from django.utils.encoding import force_bytes
    from django.utils.http import urlsafe_base64_encode

    user_name = get_user_display_name(user)

    uid = urlsafe_base64_encode(
        force_bytes(user.pk)
    )

    token = account_activation_token.make_token(user)

    activation_url = (
        f"https://{domain}/activate/{uid}/{token}/"
    )

    subject = "Activate your ExactSub account"

    message = (
        "<p>"
        "Please verify your email address to activate "
        "your ExactSub account."
        "</p>"

        "<p>"
        "Click the button below to complete your "
        "email verification."
        "</p>"

        "<p>"
        "If you did not create this account, you can "
        "safely ignore this email."
        "</p>"
    )

    mailer(
        user.email,
        subject,
        message,
        user_name,
        button_text="Verify my email address",
        button_url=activation_url,
        preheader="Verify your email address to activate your ExactSub account."
    )


def send_admin_notification(user, message):
    """
    Send an administrative notification to a customer.
    """
    if not user.email:
        return

    user_name = get_user_display_name(user)

    subject = "Notification From ExactSub"

    html_message = (
        f"<p>{message}</p>"
    )

    mailer(
        user.email,
        subject,
        html_message,
        user_name
    )


def send_airtime_to_cash_admin_notification(admin_email, user, amount, network):
    """
    Send an internal notification to the admin after a customer
    submits an Airtime-to-Cash transaction.
    """
    if not admin_email:
        return

    subject = "Airtime To Cash Notification from ExactSub"

    message = (
        "<p>A customer has submitted an Airtime-to-Cash transaction.</p>"
        f"<p><strong>Customer:</strong> {user.username}</p>"
        f"<p><strong>Amount:</strong> ₦{amount}</p>"
        f"<p><strong>Network:</strong> {network}</p>"
        f"<p><strong>Phone Number:</strong> {user.phoneNumber}</p>"
        "<p>Please confirm the transfer and reach out to the customer "
        "if necessary.</p>"
    )

    mailer(
        admin_email,
        subject,
        message,
        "Admin"
    )

def send_manual_funding_admin_notification(admin_email, user, amount):
    """
    Send an internal notification to the admin after a customer
    submits a manual wallet funding request.
    """
    if not admin_email:
        return

    subject = "Manual Funding Notification from ExactSub"

    message = (
        "<p>A customer has submitted a manual wallet funding notification.</p>"
        f"<p><strong>Customer:</strong> {user.username}</p>"
        f"<p><strong>Amount:</strong> ₦{amount}</p>"
        "<p>Please confirm the payment before processing the funding.</p>"
    )

    mailer(
        admin_email,
        subject,
        message,
        "Admin"
    )