from app.config import settings

redis_client = None


async def init_redis() -> None:
    global redis_client
    if not settings.REDIS_URL:
        return
    try:
        from redis.asyncio import Redis
        redis_client = Redis.from_url(settings.REDIS_URL, decode_responses=True)
        await redis_client.ping()
    except Exception:
        redis_client = None


async def close_redis() -> None:
    global redis_client
    if redis_client:
        try:
            await redis_client.close()
        except Exception:
            pass
        redis_client = None


def get_redis():
    """Return Redis client or None if not available."""
    return redis_client
