# profilepic.py

import base64
import requests
import logging
import os

logger = logging.getLogger("bot")

DISCORD_API = "https://discord.com/api/v10"

def set_avatar_from_bytes(image_bytes: bytes, token: str) -> bool:
    try:
        b64_avatar = base64.b64encode(image_bytes).decode("utf-8")
        payload = {
            "avatar": f"data:image/png;base64,{b64_avatar}"
        }

        headers = {
            "Authorization": f"Bot {token}",
            "Content-Type": "application/json"
        }

        response = requests.patch(f"{DISCORD_API}/users/@me", json=payload, headers=headers)
        logger.info(f"🖼️ Avatar update status: {response.status_code} - {response.text}")
        return response.status_code == 200
    except Exception as e:
        logger.error(f"❌ Failed to update avatar: {str(e)}")
        return False
