from xpinyin import Pinyin
from fastapi import Request

def generate_english_name(chinese_name: str) -> str:
    """
    Generate english name from chinese name.
    Rule: Surname initial + First name full pinyin.
    Example: 张三 -> zsan
    """
    if not chinese_name:
        return ""
    
    p = Pinyin()
    # Assuming first char is surname
    surname = chinese_name[0]
    firstname = chinese_name[1:]
    
    surname_pinyin = p.get_pinyin(surname, "").lower()
    firstname_initial = p.get_initials(firstname, "").lower()
    
    return f"{surname_pinyin}{firstname_initial}"

def get_client_ip(request: Request) -> str:
    """
    Get client IP address from request headers or fallback to client host.
    Prioritizes X-Forwarded-For and X-Real-IP headers.
    """
    forwarded_for = request.headers.get("X-Forwarded-For")
    if forwarded_for:
        return forwarded_for.split(",")[0].strip()
    
    real_ip = request.headers.get("X-Real-IP")
    if real_ip:
        return real_ip
        
    return request.client.host if request.client else "0.0.0.0"
