"""
验证修复脚本
测试3个关键修复是否生效
"""
import sys
from loguru import logger

logger.info("=" * 60)
logger.info("🧪 验证修复结果...")
logger.info("=" * 60)

all_passed = True

# 测试 1: IndexResult.skip 方法
logger.info("\n[测试 1] 验证 IndexResult.skip() 方法...")
try:
    from src.types.result import IndexResult
    
    # 调用 skip 方法
    result = IndexResult.skip(
        index_type="test",
        message="测试跳过结果"
    )
    
    assert result.success == True, "skip结果应该是成功的"
    assert result.index is None, "skip结果的index应该是None"
    assert result.index_type == "test", "index_type应该被正确设置"
    assert result.message == "测试跳过结果", "message应该被正确设置"
    
    logger.info("  ✅ IndexResult.skip() 方法工作正常")
    
except Exception as e:
    logger.error(f"  ❌ 测试失败: {e}")
    all_passed = False

# 测试 2: IndexManager 不再使用 vector_store_manager
logger.info("\n[测试 2] 验证 IndexManager._delete_qdrant_index() 使用正确的API...")
try:
    import inspect
    from src.core.index_manager import IndexManager
    
    # 检查方法源代码
    source = inspect.getsource(IndexManager._delete_qdrant_index)
    
    # 检查是否使用了 storage_provider
    uses_storage_provider = 'storage_provider' in source
    uses_vector_store_manager = 'vector_store_manager' in source
    
    assert uses_storage_provider, "应该使用 storage_provider"
    assert not uses_vector_store_manager, "不应该使用 vector_store_manager"
    
    logger.info("  ✅ IndexManager 使用正确的 API (storage_provider)")
    logger.info(f"     不再使用: vector_store_manager")
    
except Exception as e:
    logger.error(f"  ❌ 测试失败: {e}")
    all_passed = False

# 测试 3: QdrantVectorStore 初始化不使用 path 参数
logger.info("\n[测试 3] 验证 QdrantStorageStrategy 不传递 path 参数...")
try:
    import inspect
    from src.storage.strategies.qdrant_strategy import QdrantStorageStrategy
    
    # 检查初始化代码
    source = inspect.getsource(QdrantStorageStrategy.initialize)
    
    # 检查是否仍然传递 path 参数
    has_path_param = 'path=' in source or 'path =' in source
    
    if has_path_param:
        # 检查是否是注释
        lines = source.split('\n')
        path_lines = [line for line in lines if 'path=' in line or 'path =' in line]
        active_path = any('#' not in line.split('path')[0] for line in path_lines)
        
        assert not active_path, "不应该传递 path 参数（除非在注释中）"
    
    logger.info("  ✅ QdrantStorageStrategy 不再传递 path 参数")
    logger.info("     使用新版本 API：client + collection_name")
    
except Exception as e:
    logger.error(f"  ❌ 测试失败: {e}")
    all_passed = False

# 测试 4: 补丁识别新版本
logger.info("\n[测试 4] 验证补丁正确识别新版本...")
try:
    from src.utils.patches import apply_all_patches
    from llama_index.vector_stores.qdrant import QdrantVectorStore
    import inspect
    
    # 应用补丁
    apply_all_patches()
    
    # 检查 API
    sig = inspect.signature(QdrantVectorStore.__init__)
    params = sig.parameters
    has_path = 'path' in params
    
    logger.info(f"  - QdrantVectorStore 有 path 参数: {'是(旧版本)' if has_path else '否(新版本)'}")
    logger.info(f"  ✅ 补丁系统工作正常")
    
except Exception as e:
    logger.error(f"  ❌ 测试失败: {e}")
    all_passed = False

logger.info("\n" + "=" * 60)
if all_passed:
    logger.info("✅ 所有测试通过！修复验证成功")
    logger.info("=" * 60)
    logger.info("\n💡 建议：重启 MCP HTTP 服务器测试实际效果")
    logger.info("   命令: python mcp_http_server.py")
    sys.exit(0)
else:
    logger.error("❌ 部分测试失败，请检查修复")
    logger.info("=" * 60)
    sys.exit(1)
