#!/usr/bin/env python3
"""
Install AI Video Dependencies
安装 AI 视频生成所需的依赖包
"""

import subprocess
import sys
import os

def install_package(package_name, import_name=None):
    """Install a Python package using pip"""
    if import_name is None:
        import_name = package_name
    
    try:
        __import__(import_name)
        print(f"✅ {package_name} is already installed")
        return True
    except ImportError:
        print(f"📦 Installing {package_name}...")
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", package_name])
            print(f"✅ {package_name} installed successfully")
            return True
        except subprocess.CalledProcessError as e:
            print(f"❌ Failed to install {package_name}: {e}")
            return False

def main():
    """Main installation function"""
    print("🚀 Installing AI Video Dependencies...")
    print("=" * 50)
    
    # List of required packages
    packages = [
        ("requests", "requests"),
        ("cos-python-sdk-v5", "qcloud_cos"),
        ("loguru", "loguru"),
    ]
    
    success_count = 0
    total_count = len(packages)
    
    for package_name, import_name in packages:
        if install_package(package_name, import_name):
            success_count += 1
    
    print("=" * 50)
    print(f"📊 Installation Summary: {success_count}/{total_count} packages installed")
    
    if success_count == total_count:
        print("🎉 All dependencies installed successfully!")
        
        # Test AI video module import
        print("\n🧪 Testing AI video module import...")
        try:
            from ai_video import VideoGenerator
            print("✅ AI video module imported successfully!")
        except Exception as e:
            print(f"❌ AI video module import failed: {e}")
            return False
        
        return True
    else:
        print("❌ Some dependencies failed to install. Please check the errors above.")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)
