#!/usr/bin/env python3
"""
测试修复后的场景检测功能
"""

import os
import sys
import tempfile
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

def test_pyscenedetect_fix():
    """测试PySceneDetect修复"""
    print("🎯 测试PySceneDetect场景检测修复")
    print("=" * 50)
    
    # 查找测试视频
    assets_dir = project_root / "assets"
    video_files = list(assets_dir.rglob("*.mp4"))
    
    if not video_files:
        print("❌ 没有找到测试视频")
        return False
    
    test_video = str(video_files[0])
    print(f"📹 测试视频: {test_video}")
    
    try:
        from python_core.services.media_manager import get_media_manager
        
        media_manager = get_media_manager()
        
        # 检查依赖
        opencv_available = media_manager.dependency_manager.is_available('opencv')
        scenedetect_available = media_manager.dependency_manager.is_available('scenedetect')
        
        print(f"🔍 依赖检查:")
        print(f"   OpenCV: {'✅' if opencv_available else '❌'}")
        print(f"   PySceneDetect: {'✅' if scenedetect_available else '❌'}")
        
        # 测试场景检测
        print(f"\n🎯 测试场景检测...")
        scene_changes = media_manager._detect_scene_changes(test_video, threshold=30.0)
        
        print(f"✅ 场景检测结果:")
        print(f"   场景变化点数量: {len(scene_changes)}")
        print(f"   时间点: {[f'{t:.2f}s' for t in scene_changes]}")
        
        # 验证结果
        if len(scene_changes) >= 2:
            print(f"✅ 场景检测正常 - 至少有开始和结束时间")
            return True
        else:
            print(f"❌ 场景检测异常 - 只有 {len(scene_changes)} 个时间点")
            return False
            
    except Exception as e:
        print(f"❌ 测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

def test_video_upload_with_segments():
    """测试视频上传和分镜头生成"""
    print("\n" + "=" * 50)
    print("🎬 测试视频上传和分镜头生成")
    print("=" * 50)
    
    # 查找测试视频
    assets_dir = project_root / "assets"
    video_files = list(assets_dir.rglob("*.mp4"))
    
    if not video_files:
        print("❌ 没有找到测试视频")
        return False
    
    test_video = str(video_files[0])
    print(f"📹 测试视频: {test_video}")
    
    # 创建临时目录
    temp_dir = tempfile.mkdtemp(prefix="video_test_")
    print(f"📁 临时目录: {temp_dir}")
    
    try:
        from unittest.mock import patch
        
        with patch('python_core.config.settings') as mock_settings:
            mock_settings.temp_dir = Path(temp_dir)
            
            from python_core.services.media_manager import MediaManager
            
            media_manager = MediaManager()
            
            # 上传视频
            print(f"\n📤 上传视频...")
            result = media_manager.upload_video_file(
                source_path=test_video,
                filename=os.path.basename(test_video),
                tags=["测试", "修复验证"]
            )
            
            print(f"📊 上传结果:")
            print(f"   是否重复: {result.get('is_duplicate', False)}")
            print(f"   分镜头数量: {len(result.get('segments', []))}")
            
            segments = result.get('segments', [])
            if segments:
                print(f"\n✅ 分镜头生成成功:")
                total_duration = 0
                for i, segment in enumerate(segments):
                    if isinstance(segment, dict):
                        duration = segment.get('duration', 0)
                        start_time = segment.get('start_time', 0)
                        end_time = segment.get('end_time', 0)
                        filename = segment.get('filename', 'unknown')
                    else:
                        duration = segment.duration
                        start_time = segment.start_time
                        end_time = segment.end_time
                        filename = segment.filename
                    
                    print(f"   片段 {i+1}: {filename}")
                    print(f"     时长: {duration:.2f}秒")
                    print(f"     时间范围: {start_time:.2f}s - {end_time:.2f}s")
                    total_duration += duration
                
                print(f"\n📊 统计:")
                print(f"   片段总时长: {total_duration:.2f}秒")
                
                return True
            else:
                print(f"❌ 分镜头生成失败")
                return False
                
    except Exception as e:
        print(f"❌ 测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False
    finally:
        # 清理临时目录
        import shutil
        shutil.rmtree(temp_dir, ignore_errors=True)

def main():
    """主函数"""
    print("🚀 场景检测修复验证测试")
    
    try:
        # 测试场景检测修复
        success1 = test_pyscenedetect_fix()
        
        # 测试完整的视频上传流程
        success2 = test_video_upload_with_segments()
        
        print("\n" + "=" * 50)
        print("📊 测试总结")
        print("=" * 50)
        
        if success1 and success2:
            print("🎉 所有测试通过！场景检测修复成功！")
            print("\n✅ 修复要点:")
            print("   1. PySceneDetect没有检测到场景时，自动添加视频结束时间")
            print("   2. 确保场景变化点至少包含开始和结束时间")
            print("   3. 视频分镜头功能正常工作")
            return 0
        else:
            print("⚠️ 部分测试失败，需要进一步检查")
            return 1
            
    except Exception as e:
        print(f"❌ 测试过程中出错: {e}")
        import traceback
        traceback.print_exc()
        return 1

if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)
