#!/usr/bin/env python3
"""
InsightFlow Multimodal Module Test Script
测试多模态支持模块
"""

import os
import sys

# 添加 backend 目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

print("=" * 60)
print("InsightFlow 多模态模块测试")
print("=" * 60)

# 测试导入
print("\n1. 测试模块导入...")

try:
    from multimodal_processor import get_multimodal_processor

    print("   ✓ multimodal_processor 导入成功")
except ImportError as e:
    print(f"   ✗ multimodal_processor 导入失败: {e}")

try:
    from image_processor import get_image_processor

    print("   ✓ image_processor 导入成功")
except ImportError as e:
    print(f"   ✗ image_processor 导入失败: {e}")

try:
    from multimodal_entity_linker import get_multimodal_entity_linker

    print("   ✓ multimodal_entity_linker 导入成功")
except ImportError as e:
    print(f"   ✗ multimodal_entity_linker 导入失败: {e}")

# 测试初始化
print("\n2. 测试模块初始化...")

try:
    processor = get_multimodal_processor()
    print("   ✓ MultimodalProcessor 初始化成功")
    print(f"     - 临时目录: {processor.temp_dir}")
    print(f"     - 帧提取间隔: {processor.frame_interval}秒")
except Exception as e:
    print(f"   ✗ MultimodalProcessor 初始化失败: {e}")

try:
    img_processor = get_image_processor()
    print("   ✓ ImageProcessor 初始化成功")
    print(f"     - 临时目录: {img_processor.temp_dir}")
except Exception as e:
    print(f"   ✗ ImageProcessor 初始化失败: {e}")

try:
    linker = get_multimodal_entity_linker()
    print("   ✓ MultimodalEntityLinker 初始化成功")
    print(f"     - 相似度阈值: {linker.similarity_threshold}")
except Exception as e:
    print(f"   ✗ MultimodalEntityLinker 初始化失败: {e}")

# 测试实体关联功能
print("\n3. 测试实体关联功能...")

try:
    linker = get_multimodal_entity_linker()

    # 测试字符串相似度
    sim = linker.calculate_string_similarity("Project Alpha", "Project Alpha")
    assert sim == 1.0, "完全匹配应该返回1.0"
    print(f"   ✓ 字符串相似度计算正常 (完全匹配: {sim})")

    sim = linker.calculate_string_similarity("K8s", "Kubernetes")
    print(f"   ✓ 字符串相似度计算正常 (不同字符串: {sim:.2f})")

    # 测试实体相似度
    entity1 = {"name": "Project Alpha", "type": "PROJECT", "definition": "核心项目"}
    entity2 = {"name": "Project Alpha", "type": "PROJECT", "definition": "主要项目"}
    sim, match_type = linker.calculate_entity_similarity(entity1, entity2)
    print(f"   ✓ 实体相似度计算正常 (相似度: {sim:.2f}, 类型: {match_type})")

except Exception as e:
    print(f"   ✗ 实体关联功能测试失败: {e}")

# 测试图片处理功能（不需要实际图片）
print("\n4. 测试图片处理器功能...")

try:
    processor = get_image_processor()

    # 测试图片类型检测（使用模拟数据）
    print(f"   ✓ 支持的图片类型: {list(processor.IMAGE_TYPES.keys())}")
    print(f"   ✓ 图片类型描述: {processor.IMAGE_TYPES}")

except Exception as e:
    print(f"   ✗ 图片处理器功能测试失败: {e}")

# 测试视频处理配置
print("\n5. 测试视频处理器配置...")

try:
    processor = get_multimodal_processor()

    print(f"   ✓ 视频目录: {processor.video_dir}")
    print(f"   ✓ 帧目录: {processor.frames_dir}")
    print(f"   ✓ 音频目录: {processor.audio_dir}")

    # 检查目录是否存在
    for dir_name, dir_path in [
        ("视频", processor.video_dir),
        ("帧", processor.frames_dir),
        ("音频", processor.audio_dir),
    ]:
        if os.path.exists(dir_path):
            print(f"   ✓ {dir_name}目录存在: {dir_path}")
        else:
            print(f"   ✗ {dir_name}目录不存在: {dir_path}")

except Exception as e:
    print(f"   ✗ 视频处理器配置测试失败: {e}")

# 测试数据库方法（如果数据库可用）
print("\n6. 测试数据库多模态方法...")

try:
    from db_manager import get_db_manager

    db = get_db_manager()

    # 检查多模态表是否存在
    conn = db.get_conn()
    tables = ["videos", "video_frames", "images", "multimodal_mentions", "multimodal_entity_links"]

    for table in tables:
        try:
            conn.execute(f"SELECT 1 FROM {table} LIMIT 1")
            print(f"   ✓ 表 '{table}' 存在")
        except Exception as e:
            print(f"   ✗ 表 '{table}' 不存在或无法访问: {e}")

    conn.close()

except Exception as e:
    print(f"   ✗ 数据库多模态方法测试失败: {e}")

print("\n" + "=" * 60)
print("测试完成")
print("=" * 60)
