"""请求级俱乐部上下文中间件。"""
from jituan.constants import CLUB_ID_DEFAULT
from jituan.services.club_context import resolve_club_id_from_request, resolve_club_scope


class ClubContextMiddleware:
    """
    为每个请求注入 club_id / club_scope。
    旧客户端不传 Header 时 club_id 恒为 xq，不影响现网。
    """

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        request.club_id = resolve_club_id_from_request(request)
        request.club_scope = resolve_club_scope(request)
        if not request.club_id:
            request.club_id = CLUB_ID_DEFAULT
        return self.get_response(request)
