"""HTML-E-Mail-Vorlagen für Magic Links, Einladungen und Benachrichtigungen."""


def magic_link_login_email(username: str, link: str) -> tuple[str, str]:
    """Erzeugt Login-E-Mail mit Magic Link.

    Returns:
        (subject, html_body)
    """
    subject = f"AegisSight Monitor - Anmeldung"
    html = f"""<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"></head>
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0f172a; color: #e2e8f0; padding: 40px 20px;">
  <div style="max-width: 480px; margin: 0 auto; background: #1e293b; border-radius: 12px; padding: 32px; border: 1px solid #334155;">
    <h1 style="color: #f0b429; font-size: 20px; margin: 0 0 24px 0;">AegisSight Monitor</h1>

    <p style="margin: 0 0 16px 0;">Hallo {username},</p>

    <p style="margin: 0 0 24px 0;">Klicken Sie auf den Button, um sich anzumelden:</p>

    <div style="text-align: center; margin: 0 0 24px 0;">
      <a href="{link}" style="display: inline-block; background: #f0b429; color: #0f172a; padding: 14px 40px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 16px;">Jetzt anmelden</a>
    </div>

    <p style="color: #94a3b8; font-size: 13px; margin: 0 0 12px 0;">Oder kopieren Sie diesen Link in Ihren Browser:</p>
    <p style="color: #64748b; font-size: 11px; word-break: break-all; margin: 0 0 24px 0;">{link}</p>

    <p style="color: #94a3b8; font-size: 13px; margin: 0;">Dieser Link ist 10 Minuten gültig. Falls Sie diese Anmeldung nicht angefordert haben, ignorieren Sie diese E-Mail.</p>
  </div>
</body>
</html>"""
    return subject, html


def incident_notification_email(
    username: str,
    incident_title: str,
    notifications: list[dict],
    dashboard_url: str,
    incident_type: str = "adhoc",
) -> tuple[str, str]:
    """Erzeugt Benachrichtigungs-E-Mail für Lagen-Updates.

    Args:
        username: Empfaenger-Name
        incident_title: Titel der Lage/Recherche
        notifications: Liste von {"text": ..., "icon": ...} Dicts
        dashboard_url: Link zum Dashboard
        incident_type: "adhoc" oder "research"

    Returns:
        (subject, html_body)
    """
    is_research = incident_type == "research"
    type_label = "Recherche" if is_research else "Lagebild"
    type_label_lower = "Recherche" if is_research else "Lage"
    subject = f"AegisSight - {incident_title}"

    icon_map = {
        "success": "&#10003;",
        "warning": "&#9888;",
        "error": "&#10007;",
        "info": "&#9432;",
    }
    color_map = {
        "success": "#22c55e",
        "warning": "#f0b429",
        "error": "#ef4444",
        "info": "#94a3b8",
    }

    items_html = ""
    for n in notifications:
        icon = icon_map.get(n.get("icon", "info"), "&#9432;")
        color = color_map.get(n.get("icon", "info"), "#94a3b8")
        text = n.get("text", "")
        items_html += f"""
      <div style="display: flex; align-items: flex-start; gap: 10px; padding: 10px 0; border-bottom: 1px solid #334155;">
        <span style="color: {color}; font-size: 18px; line-height: 1;">{icon}</span>
        <span style="color: #e2e8f0; font-size: 14px; line-height: 1.4;">{text}</span>
      </div>"""

    html = f"""<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"></head>
<body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0f172a; color: #e2e8f0; padding: 40px 20px;">
  <div style="max-width: 480px; margin: 0 auto; background: #1e293b; border-radius: 12px; padding: 32px; border: 1px solid #334155;">
    <h1 style="color: #f0b429; font-size: 20px; margin: 0 0 8px 0;">AegisSight Monitor</h1>
    <p style="color: #94a3b8; font-size: 12px; margin: 0 0 24px 0;">{type_label} - Benachrichtigung</p>

    <p style="margin: 0 0 8px 0;">Hallo {username},</p>
    <p style="margin: 0 0 20px 0;">es gibt Neuigkeiten zur {type_label_lower} <strong style="color: #f0b429;">{incident_title}</strong>:</p>

    <div style="background: #0f172a; border-radius: 8px; padding: 4px 16px; margin: 0 0 24px 0;">
      {items_html}
    </div>

    <div style="text-align: center; margin: 0 0 24px 0;">
      <a href="{dashboard_url}" style="display: inline-block; background: #f0b429; color: #0f172a; padding: 12px 32px; border-radius: 6px; text-decoration: none; font-weight: 600;">Im Dashboard ansehen</a>
    </div>

    <p style="color: #64748b; font-size: 12px; margin: 0;">Diese Benachrichtigung kann in den Einstellungen im Dashboard deaktiviert werden.</p>
  </div>
</body>
</html>"""
    return subject, html
