"""Redis client provider — a single shared async connection pool."""

from redis.asyncio import Redis

from app.core.config import get_settings

_client: Redis | None = None


def get_redis() -> Redis:
    """Return the process-wide Redis client (created on first use)."""
    global _client
    if _client is None:
        _client = Redis.from_url(
            str(get_settings().redis_url),
            encoding="utf-8",
            decode_responses=True,
        )
    return _client


async def close_redis() -> None:
    global _client
    if _client is not None:
        await _client.aclose()
        _client = None
