# /*
#  * Copyright (C) 2020, KylinSoft Co., Ltd.
#  *
#  * This program is free software: you can redistribute it and/or modify
#  * it under the terms of the GNU General Public License as published by
#  * the Free Software Foundation, either version 3 of the License, or
#  * (at your option) any later version.
#  *
#  * This program is distributed in the hope that it will be useful,
#  * but WITHOUT ANY WARRANTY; without even the implied warranty of
#  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  * GNU General Public License for more details.
#  *
#  * You should have received a copy of the GNU General Public License
#  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
#  */

import logging

CHECK_REGISTRY = {}

def register_check(name, roles=None):
    """
    注册一个检查函数
    - name: 唯一名称
    - roles: 适用的 transaction 角色 (可选, list[str])
    """
    def decorator(func):
        CHECK_REGISTRY[name] = {
            "func": func,
            "roles": roles or []
        }
        return func
    return decorator

def run_checks(trans, G_settings):
    """统一执行需要的检查"""
    skip_list = set(G_settings.get("skip_function", []))
    role = trans.role

    for name, meta in CHECK_REGISTRY.items():
        if name in skip_list:
            logging.info(f"Skip check: {name}")
            continue

        if meta["roles"] and role not in meta["roles"]:
            continue  # 只对特定 role 生效

        meta["func"](trans)
        logging.info(f"Check passed: {name}")
