# app/core/dependency.py
from typing import Annotated
from fastapi import Header, HTTPException, Depends
from app.core.ctx import set_ctx_weixin_user
from app.models import WeixinUser

async def get_weixin_user(token: str = Header(..., description="token验证")) -> WeixinUser:
    user = await WeixinUser.filter(userid=token).first()
    if not user:
        raise HTTPException(status_code=401, detail="Weixin User Authentication failed")
    # CTX_USER_ID.set(int(user.id))
    set_ctx_weixin_user(int(user.id), f'{user.username}({user.english_name})')
    return user

DependWeixinUser = Depends(get_weixin_user)
WeixinUserDep = Annotated[WeixinUser, DependWeixinUser]
