"""Telethon proxy injection snippet — copy into any Telethon-based project."""

import os
import re
import logging

logger = logging.getLogger(__name__)

def get_proxy_config():
    """Parse HTTP_PROXY env var into Telethon-compatible SOCKS5 proxy tuple.
    
    Returns:
        tuple: (socks.SOCKS5, host, port) or None if no proxy configured
    """
    try:
        import socks
    except ImportError:
        logger.warning("PySocks not installed — proxy disabled. Install with: pip install PySocks")
        return None
    
    proxy_addr = os.getenv('HTTP_PROXY') or os.getenv('http_proxy')
    if not proxy_addr:
        return None
    
    m = re.match(r'https?://([\d.]+):(\d+)', proxy_addr)
    if m:
        host, port = m.group(1), int(m.group(2))
        logger.info(f"Using SOCKS5 proxy: {host}:{port}")
        return (socks.SOCKS5, host, port)
    
    logger.warning(f"Could not parse proxy URL: {proxy_addr}")
    return None


# Usage:
# proxy = get_proxy_config()
# client = TelegramClient('session', api_id, api_hash, proxy=proxy)
