from functools import wraps
from flask import redirect, session


def subadmin_login_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        if not session.get("user_id"):
            return redirect("/login")
        role = session.get("role") or session.get("admin_role", "")
        if role != "sub_admin":
            return redirect("/login")
        return f(*args, **kwargs)
    return decorated