# (c) cavaliba - data - hook.py


# HOOKS = {
#     "sirene_template": {
#         "detail": [
#             { "add_action": {"label": "Notify", "action": "test"} },
#         ],
#     },
# }


# def get_hook(classname=None, view=None):

#     if not classname:
#         return []

#     if not view:
#         return []

#     try:
#         return HOOKS[classname][view]
#     except Exception:
#         pass

#     return []


def inject_iam_user_detail(instance, context):
    """
    HOOK for *user* schema in instance_detail
    add data structures to context for HTML template:
    - groups
    - roles
    - permissions
    """
    if instance.roles is None:
        instance.compute_aaa()

    iam_groups = []
    for g in instance.direct_groups:
        iam_groups.append(
            {
                "id": g.id,
                "keyname": g.keyname,
                "displayname": g.displayname or g.keyname,
                "description": g.get_attribute_first("description") or "",
                "kind": "DIRECT",
            }
        )
    for g in instance.parent_groups:
        iam_groups.append(
            {
                "id": g.id,
                "keyname": g.keyname,
                "displayname": g.displayname or g.keyname,
                "description": g.get_attribute_first("description") or "",
                "kind": "INDIRECT",
            }
        )
    for g in instance.auto_groups:
        iam_groups.append(
            {
                "id": g.id,
                "keyname": g.keyname,
                "displayname": g.displayname or g.keyname,
                "description": g.get_attribute_first("description") or "",
                "kind": "AUTO",
            }
        )
    context["iam_groups"] = iam_groups

    iam_roles = []
    for r in instance.roles:
        users_field = r.get_attribute("users") or []
        kind = "DIRECT" if instance.keyname in users_field else "INDIRECT"
        iam_roles.append(
            {
                "id": r.id,
                "keyname": r.keyname,
                "displayname": r.displayname or r.keyname,
                "description": r.get_attribute_first("description") or "",
                "kind": kind,
            }
        )
    context["iam_roles"] = iam_roles

    context["iam_permissions"] = instance.permissions or []


def inject_iam_group_detail(instance, context):
    """
    HOOK for *group*  schema in instance_detail
    add data structures to context for HTML template:
    - subgroups
    - users
    """

    # instance.auto_users is populated from DB field (~cache)
    # and recomputed async by a periodic task
    # here we want a forced recompute (for uptodate instance_detail)
    instance.autogroup_recompute()

    # then, all users, capped at MAX_COMPUTED_USER
    users_all = instance.get_users_all()

    iam_subgroups = []
    for g in instance.subgroups:
        iam_subgroups.append(
            {
                "id": g.id,
                "keyname": g.keyname,
                "displayname": g.displayname or g.keyname,
                "description": g.get_attribute_first("description") or "",
            }
        )
    context["iam_subgroups"] = iam_subgroups

    direct_keynames = {u.keyname for u in instance.direct_users}
    auto_keynames = {u.keyname for u in instance.auto_users}

    iam_users = []
    for u in users_all:
        if u.keyname in direct_keynames:
            kind = "DIRECT"
        elif u.keyname in auto_keynames:
            kind = "AUTO"
        else:
            kind = "INDIRECT"
        iam_users.append(
            {
                "id": u.id,
                "keyname": u.keyname,
                "displayname": u.displayname or u.keyname,
                "kind": kind,
                "email": u.get_attribute_first("email") or "",
                "mobile": u.get_attribute_first("mobile") or "",
                "last_login": u.get_attribute_first("last_login") or "",
            }
        )
    context["iam_users"] = iam_users


def inject_iam_role_detail(instance, context):
    """
    HOOK for *role* schema in instance_detail
    add data structures to context for HTML template:
    - direct users
    - groups
    - permissions
    """
    instance.get_users()
    instance.get_groups()
    instance.get_permissions()

    context["iam_users"] = [
        {
            "id": u.id,
            "keyname": u.keyname,
            "displayname": u.displayname or u.keyname,
            "email": u.get_attribute_first("email") or "",
            "mobile": u.get_attribute_first("mobile") or "",
        }
        for u in instance.users
    ]

    context["iam_groups"] = [
        {
            "id": g.id,
            "keyname": g.keyname,
            "displayname": g.displayname or g.keyname,
            "description": g.get_attribute_first("description") or "",
        }
        for g in instance.groups
    ]

    context["iam_permissions"] = instance.permissions or []
