import contextvars

from starlette.background import BackgroundTasks

CTX_USER_ID: contextvars.ContextVar[int] = contextvars.ContextVar("user_id", default=0)
CTX_USER_NAME: contextvars.ContextVar[str] = contextvars.ContextVar("user_name", default="")
CTX_BG_TASKS: contextvars.ContextVar[BackgroundTasks] = contextvars.ContextVar("bg_task", default=None)

def set_ctx_weixin_user(user_id: int, user_name: str):
    CTX_USER_ID.set(user_id)
    CTX_USER_NAME.set(user_name)

def get_ctx_weixin_user():
    return {
        "user_id": CTX_USER_ID.get(),
        "user_name": CTX_USER_NAME.get(),
    }
