"""
配置文件

该模块包含了项目的所有配置常量和设置。
"""

import os
from typing import Dict, Any
from .src.logger_config import get_logger

# 延迟初始化日志器，避免在导入时立即执行
logger = None

def _ensure_logger():
    """确保日志器已初始化"""
    global logger
    if logger is None:
        logger = get_logger(__name__)
    return logger

# 生产环境
DEFAULT_DEVICE_API_BASE_URL = "http://lzwcai-demp-corp-manager:8086"
DEFAULT_VECTOR_API_BASE_URL = "http://lzwcai-demp-tool-server:5002"
# 本地环境
# DEFAULT_DEVICE_API_BASE_URL = "http://192.168.2.236:8088"
# DEFAULT_VECTOR_API_BASE_URL = "http://192.168.2.236:5002"


# 默认企业ID
# DEFAULT_ENTERPRISE_ID = "1952978233106669569"
DEFAULT_ENTERPRISE_ID = ""

# 默认员工ID
# DEFAULT_EMPLOYEE_ID = "1955949384389005313"
DEFAULT_EMPLOYEE_ID = ""

# 请求配置
REQUEST_TIMEOUT = 30  # 请求超时时间（秒）
MAX_RETRIES = 3  # 最大重试次数

# 向量服务配置
DEFAULT_VECTOR_STORE_NAME = "设备库"
DEFAULT_VECTOR_STORE_DESCRIPTION = "向量库"
DEFAULT_ENCODER_TYPE = "word2vec"
DEFAULT_TOP_K = 1
DEFAULT_AUTO_CREATE = True

# 日志配置
LOG_LEVEL = "INFO"
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"


# 环境变量配置
def get_config() -> Dict[str, Any]:
    """
    获取配置信息，支持环境变量覆盖

    返回:
        Dict[str, Any]: 配置字典
    """
    logger = _ensure_logger()
    logger.debug("开始加载配置...")
    
    config = {
        "device_api_base_url": os.getenv(
            "DEVICE_API_BASE_URL", DEFAULT_DEVICE_API_BASE_URL
        ),
        "vector_api_base_url": os.getenv(
            "VECTOR_API_BASE_URL", DEFAULT_VECTOR_API_BASE_URL
        ),
        "enterprise_id": os.getenv("enterpriseId", DEFAULT_ENTERPRISE_ID),
        "employeeId": os.getenv("employeeId", DEFAULT_EMPLOYEE_ID),
        "request_timeout": int(os.getenv("REQUEST_TIMEOUT", REQUEST_TIMEOUT)),
        "max_retries": int(os.getenv("MAX_RETRIES", MAX_RETRIES)),
        "vector_store_name": os.getenv("VECTOR_STORE_NAME", DEFAULT_VECTOR_STORE_NAME),
        "vector_store_description": os.getenv(
            "VECTOR_STORE_DESCRIPTION", DEFAULT_VECTOR_STORE_DESCRIPTION
        ),
        "encoder_type": os.getenv("ENCODER_TYPE", DEFAULT_ENCODER_TYPE),
        "default_top_k": int(os.getenv("DEFAULT_TOP_K", DEFAULT_TOP_K)),
        "default_auto_create": os.getenv(
            "DEFAULT_AUTO_CREATE", str(DEFAULT_AUTO_CREATE)
        ).lower()
        == "true",
        "log_level": os.getenv("LOG_LEVEL", LOG_LEVEL),
        "log_format": os.getenv("LOG_FORMAT", LOG_FORMAT),
    }
    
    logger.info("配置加载完成")
    logger.debug(f"设备API地址: {config['device_api_base_url']}")
    logger.debug(f"向量API地址: {config['vector_api_base_url']}")
    logger.debug(f"员工ID: {config['employeeId']}")
    logger.debug(f"请求超时: {config['request_timeout']}秒")
    
    return config


# 全局配置实例
CONFIG = get_config()
