# config.py
import os
import sys
from pathlib import Path
import json

def get_app_base_path():
    """
    获取应用程序基础路径，兼容开发和打包环境
    """
    if getattr(sys, 'frozen', False):
        # 如果是打包后的exe文件
        if hasattr(sys, '_MEIPASS'):
            # 临时解压目录（包含打包的资源文件）
            return Path(sys._MEIPASS)
        else:
            # exe文件所在目录
            return Path(sys.executable).parent
    else:
        # 开发环境：当前脚本所在目录
        return Path(__file__).parent

# 获取基础路径
BASE_PATH = get_app_base_path()

# API 配置 - 硬编码
QWEN_API_URL = os.getenv(
    "QWEN_API_URL",
    os.getenv("DASH_SCOPE_API_BASE", "https://dashscope.aliyuncs.com/compatible-mode/v1"),
)
QWEN_API_KEY = (
    os.getenv("DASHSCOPE_API_KEY")
    or os.getenv("DASH_SCOPE_API_KEY")
    or os.getenv("QWEN_API_KEY")
    or ""
)
QWEN_CHAT_MODEL = os.getenv("QWEN_CHAT_MODEL", "qwen-max")
QWEN_EMBEDDING_MODEL = os.getenv("QWEN_EMBEDDING_MODEL", "text-embedding-v4")

# 项目路径配置 - 硬编码
PROJECT_ROOT = BASE_PATH
Default_PROJECT_ROOT = BASE_PATH / "PrjAttCtrlMng"

# 数据文件路径配置 - 硬编码
VECTOR_DB_PATH = ""
Default_VECTOR_DB_PATH = str(BASE_PATH / "satellite_rag.faiss")
METADATA_PATH = ""
Default_METADATA_PATH = str(BASE_PATH / "satellite_rag_metadata.json")
GRAPH_DATA_PATH = ""
Default_GRAPH_DATA_PATH = str(BASE_PATH / "code_knowledge_graph.json")

# 代码处理配置 - 硬编码
MAX_CODE_LENGTH = 800
CPP_EXTENSIONS = {'.c', '.cpp', '.h', '.hpp', '.tcc'}
IGNORE_DIRS = {'build', 'cmake-build', '.git', 'vendor', 'lib', 'external', 'Debug'}

# 功能检索配置 - 硬编码
KNOWLEDGE_GRAPH_PATH = ""
Default_KNOWLEDGE_GRAPH_PATH = str(BASE_PATH / "project_code_knowledge_graph.json")
TOP_K = 8
MAX_HOPS = 3
MIN_SIMILARITY_THRESHOLD = 0.6

# 辅助函数，用于实际使用时的动态路径获取
def get_vector_db_path():
    """获取向量数据库实际路径"""
    return VECTOR_DB_PATH or Default_VECTOR_DB_PATH

def get_metadata_path():
    """获取元数据文件实际路径"""
    return METADATA_PATH or Default_METADATA_PATH

def get_graph_data_path():
    """获取图谱数据实际路径"""
    return GRAPH_DATA_PATH or Default_GRAPH_DATA_PATH

def get_knowledge_graph_path():
    """获取知识图谱数据实际路径"""
    return KNOWLEDGE_GRAPH_PATH or Default_KNOWLEDGE_GRAPH_PATH

# 导出路径信息，便于调试
def print_path_info():
    """打印路径信息，用于调试"""
    print("=== 配置文件路径信息 ===")
    print(f"BASE_PATH: {BASE_PATH}")
    print(f"PROJECT_ROOT: {PROJECT_ROOT}")
    print(f"Default_PROJECT_ROOT: {Default_PROJECT_ROOT}")
    print(f"Default_VECTOR_DB_PATH: {Default_VECTOR_DB_PATH}")
    print(f"Default_METADATA_PATH: {Default_METADATA_PATH}")
    print(f"Default_GRAPH_DATA_PATH: {Default_GRAPH_DATA_PATH}")
    print(f"Default_KNOWLEDGE_GRAPH_PATH: {Default_KNOWLEDGE_GRAPH_PATH}")
    print(f"是否打包环境: {getattr(sys, 'frozen', False)}")
    print("=======================")

# 如果直接运行此文件，打印路径信息
if __name__ == "__main__":
    print_path_info()
