# retoor <retoor@molodetz.nl>

import hashlib
import logging
from typing import Optional

from devplacepy.avatar import generate_avatar_svg

logger = logging.getLogger(__name__)


def background_color(seed: Optional[str]) -> str:
    value = seed or "?"
    return hashlib.md5(value.encode("utf-8")).hexdigest()[:6]


def avatar_payload(seed: Optional[str]) -> dict:
    value = seed or "anonymous"
    return {"b": background_color(value), "i": f"u/{value}.png"}


def render_png(seed: str, size: int = 128) -> bytes:
    import cairosvg

    svg = generate_avatar_svg(seed)
    return cairosvg.svg2png(
        bytestring=svg.encode("utf-8"), output_width=size, output_height=size
    )
