import logging
from fastapi import APIRouter, Request
from fastapi.responses import Response
from devplacepy.avatar import generate_avatar_svg

logger = logging.getLogger(__name__)
router = APIRouter()

_cache = {}


@router.get("/{style}/{seed}")
async def avatar_proxy(request: Request, style: str, seed: str, size: int = 128):
    cache_key = f"{seed}:{size}"
    if cache_key in _cache:
        return Response(content=_cache[cache_key], media_type="image/svg+xml")

    svg = generate_avatar_svg(seed)
    _cache[cache_key] = svg
    return Response(content=svg, media_type="image/svg+xml")
