# -*- coding: utf-8 -*-
"""
Accounts utilities for the Adtlas application.
"""

from django.conf import settings
from django.utils.html import format_html


def get_avatar_url(user_or_profile, size=None):
    """
    Get avatar URL for a user or profile, with fallback to default avatar.
    
    Args:
        user_or_profile: User or Profile instance
        size: Optional size parameter for future use
        
    Returns:
        str: Avatar URL
    """
    # If it's a User instance, get the profile
    if hasattr(user_or_profile, 'profile'):
        profile = user_or_profile.profile
    else:
        profile = user_or_profile
    
    # Check if profile has avatar
    if hasattr(profile, 'avatar') and profile.avatar:
        return profile.avatar.url
    
    # Return default avatar
    return f"{settings.STATIC_URL}images/default-avatar.png"


def get_avatar_html(user_or_profile, size=40, css_class="", alt_text="Avatar"):
    """
    Get HTML img tag for avatar with proper styling.
    
    Args:
        user_or_profile: User or Profile instance
        size: Size in pixels (default: 40)
        css_class: Additional CSS classes
        alt_text: Alt text for the image
        
    Returns:
        str: HTML img tag
    """
    avatar_url = get_avatar_url(user_or_profile)
    
    css_classes = f"avatar {css_class}".strip()
    
    return format_html(
        '<img src="{}" width="{}" height="{}" class="{}" alt="{}" '
        'style="border-radius: 50%; object-fit: cover;" />',
        avatar_url, size, size, css_classes, alt_text
    )


def get_user_display_name(user):
    """
    Get display name for a user.
    
    Args:
        user: User instance
        
    Returns:
        str: Display name
    """
    if user.first_name and user.last_name:
        return f"{user.first_name} {user.last_name}"
    elif user.first_name:
        return user.first_name
    elif user.username:
        return user.username
    else:
        return user.email


def get_user_avatar_and_name(user, avatar_size=30):
    """
    Get combined avatar and name HTML for user display.
    
    Args:
        user: User instance
        avatar_size: Size of avatar in pixels
        
    Returns:
        str: HTML with avatar and name
    """
    avatar_html = get_avatar_html(user, size=avatar_size)
    display_name = get_user_display_name(user)
    
    return format_html(
        '{} <span style="margin-left: 8px; vertical-align: middle;">{}</span>',
        avatar_html, display_name
    )

