# config.py
import os
import json
import sys

LOG_LEVEL = os.getenv('PLUGIN_LOG_LEVEL', 'DEBUG')

def get_log_level():
    """获取日志等级"""
    return LOG_LEVEL    

def get_base_dir():
    """获取插件根目录 (main.py 和 config.py 所在目录)"""
    return os.path.dirname(os.path.abspath(__file__))

def get_project_path():
    """获取项目根目录 (插件目录的父目录)"""
    return os.path.dirname(get_base_dir())

def get_lib_path():
    """获取 lib 库目录"""
    return os.path.join(get_base_dir(), 'lib')

def get_lua_path():
    return os.path.join(get_project_path(), "lua")

def get_config():
    """获取插件配置"""
    base_dir = get_base_dir()
    config_path = os.path.join(os.path.dirname(base_dir), 'config.json')
    
    try:
        with open(config_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except Exception as e:
        print(f"ERROR: 无法加载配置文件 {config_path}: {str(e)}", file=sys.stderr)
        return {
            "api_base_url": "http://www.bgxiong.com/client/v1/",
            "login_endpoint": "auth/login",
            "reg_endpoint": "auth/register",
            "activation_endpoint": "auth/activate",
            "hot_tags_endpoint": "tags/hot"
        }

def get_data_cache_dir():
    argv = sys.argv
    """获取数据缓存目录"""
    if len(argv) > 1 and os.path.isabs(argv[1]):
        return argv[1]
    else:
        # 兜底方案
        return os.path.join(os.path.dirname(__file__), 'bgxiong_data_cache')
    
def get_base_url():
    """获取 API 基础 URL"""
    config = get_config()
    return config.get("api_base_url", "http://www.bgxiong.com/client/v1/")