import os
import shutil
import subprocess
import sys
from pathlib import Path

def main():
    print("FFmpeg Setup Utility")
    print("====================")
    
    # Get the current directory
    base_dir = os.path.abspath(os.path.dirname(__file__))
    ffmpeg_dir = os.path.join(base_dir, 'ffmpeg', 'bin')
    
    # Check if FFmpeg files exist
    ffmpeg_exe = os.path.join(ffmpeg_dir, 'ffmpeg.exe')
    ffprobe_exe = os.path.join(ffmpeg_dir, 'ffprobe.exe')
    
    print(f"Looking for FFmpeg in: {ffmpeg_dir}")
    
    if os.path.exists(ffmpeg_exe) and os.path.exists(ffprobe_exe):
        print("✅ FFmpeg and FFprobe executables found!")
        
        # Test execution permissions
        try:
            print("Testing FFmpeg execution...")
            result = subprocess.run([ffmpeg_exe, '-version'], 
                                   capture_output=True, 
                                   text=True, 
                                   timeout=5)
            if result.returncode == 0:
                print("✅ FFmpeg execution successful!")
                print(result.stdout.splitlines()[0])
            else:
                print("❌ FFmpeg execution failed!")
                print(result.stderr)
        except Exception as e:
            print(f"❌ Error testing FFmpeg: {str(e)}")
            
        # Test execution permissions for FFprobe
        try:
            print("\nTesting FFprobe execution...")
            result = subprocess.run([ffprobe_exe, '-version'], 
                                   capture_output=True, 
                                   text=True, 
                                   timeout=5)
            if result.returncode == 0:
                print("✅ FFprobe execution successful!")
                print(result.stdout.splitlines()[0])
            else:
                print("❌ FFprobe execution failed!")
                print(result.stderr)
        except Exception as e:
            print(f"❌ Error testing FFprobe: {str(e)}")
    else:
        print("❌ FFmpeg executables not found!")
        
    # Check config file
    config_path = os.path.join(base_dir, 'app', 'config.py')
    if os.path.exists(config_path):
        with open(config_path, 'r') as f:
            config_content = f.read()
        print("\nConfig File Check:")
        ffmpeg_path_line = next((line for line in config_content.splitlines() 
                                if 'FFMPEG_PATH' in line and 'import' not in line), None)
        if ffmpeg_path_line:
            print(f"FFmpeg path in config: {ffmpeg_path_line.strip()}")
        else:
            print("❌ Couldn't find FFMPEG_PATH in config file")
    
    print("\nSetup Complete!")

if __name__ == "__main__":
    main() 