@echo off
REM 增强版构建脚本 - 逐步添加功能

echo 🚀 Building Enhanced MixVideo V2 Python Core...
echo.

REM 检查Python环境
echo 📋 Checking Python environment...
python --version
if errorlevel 1 (
    echo ❌ Python not found
    pause
    exit /b 1
)

REM 检查依赖
echo 📦 Checking dependencies...
python -c "import requests; print('✅ requests')" 2>nul || (
    echo Installing requests...
    python -m pip install requests
)

python -c "import PIL; print('✅ Pillow')" 2>nul || (
    echo Installing Pillow...
    python -m pip install Pillow
)

python -c "import PyInstaller; print('✅ PyInstaller')" 2>nul || (
    echo Installing PyInstaller...
    python -m pip install pyinstaller
)

REM 清理之前的构建
echo 🧹 Cleaning previous builds...
if exist build rmdir /s /q build
if exist dist rmdir /s /q dist
if exist __pycache__ rmdir /s /q __pycache__

REM 构建增强版本
echo 🔨 Building enhanced version...
python -m PyInstaller build_enhanced.spec

if errorlevel 1 (
    echo ❌ Enhanced build failed, trying basic version...
    python -m PyInstaller build_simple.spec
    
    if errorlevel 1 (
        echo ❌ All builds failed
        pause
        exit /b 1
    )
)

REM 验证构建
echo 🔍 Verifying build...
if not exist "dist\mixvideo-python-core.exe" (
    echo ❌ Executable not found
    pause
    exit /b 1
)

REM 测试基础功能
echo 🧪 Testing basic functionality...
dist\mixvideo-python-core.exe --version
if errorlevel 1 (
    echo ❌ Version test failed
    pause
    exit /b 1
)

echo Testing hello function...
dist\mixvideo-python-core.exe --module test --action hello
if errorlevel 1 (
    echo ❌ Hello test failed
    pause
    exit /b 1
)

REM 运行完整功能测试
echo 🧪 Running comprehensive tests...
python test_functionality.py

REM 复制到Tauri目录
echo 📁 Copying to Tauri directory...
if not exist "..\src-tauri\binaries" mkdir "..\src-tauri\binaries"
copy "dist\mixvideo-python-core.exe" "..\src-tauri\binaries\"

if errorlevel 1 (
    echo ❌ Failed to copy to Tauri directory
    echo You can manually copy the file:
    echo   copy "dist\mixvideo-python-core.exe" "..\src-tauri\binaries\"
) else (
    echo ✅ Copied to Tauri binaries directory
)

echo.
echo 🎉 Build completed successfully!
echo.
echo 📋 Next steps:
echo 1. Check test results in test_results.json
echo 2. Update src-tauri/tauri.conf.json if needed
echo 3. Test with: cargo tauri dev
echo 4. Build final app: cargo tauri build
echo.

pause
