import smtplib
from html import escape
from email.message import EmailMessage
from email.utils import formatdate, make_msgid

from config import Config


def build_from_header(from_email: str, from_name: str | None = None) -> str:
    if from_name:
        return f"{from_name} <{from_email}>"
    return from_email


def force_https_url(url: str) -> str:
    if url.startswith("http://"):
        return "https://" + url[len("http://"):]
    return url


def build_invite_email(
    username: str,
    invite_url: str,
    expiry_text: str,
    group_name: str | None = None,
    role_name: str | None = None,
) -> tuple[str, str]:
    safe_user = escape(username)
    safe_url = escape(invite_url)
    safe_expiry = escape(expiry_text)
    safe_group = escape(group_name) if group_name else None
    safe_role = escape(role_name) if role_name else None

    if safe_group:
        role_part = f" as <strong>{safe_role}</strong>" if safe_role else ""
        intro_html = (
            f"You have been invited to join the group <strong>{safe_group}</strong> "
            f"on MCLogger{role_part}."
        )
        role_text = f" as {role_name}" if role_name else ""
        intro_text = f"You have been invited to join the group '{group_name}' on MCLogger{role_text}."
    else:
        intro_html = "You have been invited to create an account on <strong>MCLogger</strong>."
        intro_text = "You have been invited to create an account on MCLogger."

    text_body = (
        f"Hello {username},\n\n"
        f"{intro_text}\n"
        f"Open this link to create your account:\n\n{invite_url}\n\n"
        f"This invite expires {expiry_text}.\n"
    )

    html_body = f"""
<!doctype html>
<html>
    <body style="margin:0;padding:0;background:#f6f8fb;font-family:Arial,Helvetica,sans-serif;color:#1f2937;">
        <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="padding:24px 12px;">
            <tr>
                <td align="center">
                    <table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="max-width:620px;background:#ffffff;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden;">
                        <tr>
                            <td style="background:#111827;color:#ffffff;padding:16px 20px;font-size:18px;font-weight:700;">
                                MCLogger Invitation
                            </td>
                        </tr>
                        <tr>
                            <td style="padding:24px 20px 20px 20px;font-size:14px;line-height:1.6;">
                                <p style="margin:0 0 12px 0;">Hello <strong>{safe_user}</strong>,</p>
                                <p style="margin:0 0 12px 0;">{intro_html}</p>
                                <p style="margin:0 0 20px 0;">Click the button below to create your account:</p>
                                <p style="margin:0 0 20px 0;">
                                    <a href="{safe_url}" style="display:inline-block;background:#2563eb;color:#ffffff;text-decoration:none;font-weight:700;padding:11px 16px;border-radius:8px;">Create Account</a>
                                </p>
                                <p style="margin:0 0 12px 0;">This invite expires <strong>{safe_expiry}</strong>.</p>
                                <p style="margin:16px 0 0 0;font-size:12px;color:#6b7280;word-break:break-all;">If the button does not work, use this link:<br>{safe_url}</p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>
""".strip()

    return text_body, html_body



def send_mail(settings: dict, recipient: str, subject: str, text_body: str, html_body: str | None = None):
    msg = EmailMessage()
    msg["Subject"] = subject
    msg["From"] = build_from_header(settings["from_email"], settings.get("from_name"))
    msg["To"] = recipient
    msg["Date"] = formatdate(localtime=True)
    sender_domain = (settings.get("from_email", "noreply@example.com").split("@")[-1] or "example.com")
    msg["Message-ID"] = make_msgid(domain=sender_domain)
    msg.set_content(text_body)
    if html_body:
        msg.add_alternative(html_body, subtype="html")

    with smtplib.SMTP(settings["host"], settings["port"], timeout=Config.MAIL_TIMEOUT) as smtp:
        smtp.ehlo()
        if settings.get("use_tls", True):
            smtp.starttls()
            smtp.ehlo()
        smtp.login(settings["username"], settings["password"])
        smtp.send_message(msg)
