"""用户所属俱乐部解析。"""
from jituan.constants import CLUB_ID_DEFAULT
from jituan.models import UserWxOpenid


def get_user_club_id(user):
    """
    解析用户归属 club（注册/邀请链隔离用）。
    优先级：User.ClubID → user_wx_openid → 默认 xq。
    """
    if not user:
        return CLUB_ID_DEFAULT

    club_id = getattr(user, 'ClubID', None) or getattr(user, 'club_id', None)
    if club_id:
        return club_id

    uid = getattr(user, 'UserUID', None) or getattr(user, 'yonghuid', None)
    if uid:
        binding = UserWxOpenid.query.filter(yonghuid=uid).order_by('id').first()
        if binding:
            return binding.club_id

    return CLUB_ID_DEFAULT


def ensure_user_club_id(user, club_id):
    """登录/注册后确保 User.ClubID 与绑定 club 一致。"""
    if not user or not club_id:
        return
    current = getattr(user, 'ClubID', None)
    if current != club_id and hasattr(user, 'ClubID'):
        user.ClubID = club_id
        user.save(update_fields=['ClubID'])


def get_wx_openid_for_user(user, club_id=None):
    """
    按俱乐部取微信支付 JSAPI 用 openid。
    多俱乐部用户 openid 在 user_wx_openid；xq 老用户可回落 User.OpenID。
    """
    if not user:
        return ''
    cid = (club_id or '').strip() or get_user_club_id(user)
    uid = getattr(user, 'UserUID', None) or getattr(user, 'yonghuid', None)
    if uid and cid:
        binding = UserWxOpenid.query.filter(yonghuid=uid, club_id=cid).first()
        if binding and binding.openid:
            return binding.openid
    legacy = getattr(user, 'OpenID', None) or getattr(user, 'openid', None)
    return legacy or ''


def get_payment_openid(request, user=None, club_id=None):
    """下单/充值：按请求俱乐部解析 openid。"""
    from jituan.services.club_write import resolve_club_id_for_write
    user = user or getattr(request, 'user', None)
    cid = club_id or resolve_club_id_for_write(request, user)
    return get_wx_openid_for_user(user, cid), cid
