# Copyright (c) 2025, JINGROW and contributors
# For license information, please see license.txt

"""
钩子系统初始化 - 在框架启动时自动加载和应用钩子
"""

import logging
from typing import Optional
from .loader import register_loaded_hooks

logger = logging.getLogger(__name__)


def init_hooks(app_name: Optional[str] = None, clear_cache: bool = True) -> None:
    """
    初始化钩子系统 - 加载并注册所有应用的钩子
    
    :param app_name: 应用名称，如果为 None 则加载所有应用
    :param clear_cache: 是否清除缓存
    """
    if clear_cache:
        from .loader import _loader_instance
        _loader_instance.clear_cache()
    
    try:
        register_loaded_hooks(app_name)
        logger.info(f"钩子系统初始化完成" + (f" (应用: {app_name})" if app_name else ""))
    except Exception as e:
        logger.error(f"钩子系统初始化失败: {e}", exc_info=True)
        raise


def reload_hooks(app_name: Optional[str] = None) -> None:
    """重新加载钩子（开发模式）"""
    init_hooks(app_name, clear_cache=True)

