"""Per-account rate limiter shared across document and cloud routers (D-12)."""
from __future__ import annotations

from fastapi import Request
from slowapi import Limiter

from deps.utils import get_client_ip


def _account_key(request: Request) -> str:
    user = getattr(request.state, "current_user", None)
    if user is not None:
        return str(user.id)
    ip = get_client_ip(request)
    return ip if ip is not None else "anonymous"


account_limiter = Limiter(key_func=_account_key)
