@echo off
chcp 936 >nul 2>&1
title Install Dependencies

echo ============================================
echo   Install Dependencies - One Click
echo ============================================
echo.

cd /d "%~dp0"

:: --- Python ---
echo [1/3] Checking Python...
set "PY="
where python >nul 2>&1 && set "PY=python"
if not defined PY where py >nul 2>&1 && set "PY=py"
if defined PY (
    for /f "tokens=*" %%v in ('%PY% --version 2^>^&1') do echo     Found: %%v
) else (
    echo     [!] NOT FOUND!
     echo     Download: https://www.python.org/downloads/
     echo     MUST check "Add Python to PATH"!
)

:: --- Flask ---
echo.
echo [2/3] Installing Flask...
if defined PY (
    %PY% -c "import flask" >nul 2>&1
    if errorlevel 1 (
        echo     Installing...
        %PY% -m pip install flask -q
        if errorlevel 1 (echo     [!] FAILED) else (echo     [OK] Done)
    ) else (
        echo     [OK] Already installed
    )
) else (echo     [!!] Skipped - no Python)

:: --- FFmpeg ---
echo.
echo [3/3] Checking FFmpeg...
where ffmpeg >nul 2>&1
if errorlevel 1 (
    if exist "tools\ffmpeg\ffmpeg.exe" (
        echo     [OK] Already in tools\ffmpeg\
    ) else (
        echo     [!] Not found.
         choice /C YN /M "     Download FFmpeg now (~80MB)?"
        if errorlevel 1 call :DL_FFMPEG
    )
) else (echo     [OK] In system PATH)

echo.
echo ============================================
echo   DONE! Now double-click start
echo ============================================
pause
goto :eof

:DL_FFMPEG
mkdir tools\ffmpeg 2>nul
echo     Downloading (please wait)...
powershell -Command "$ProgressPreference='SilentlyContinue';[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;Invoke-WebRequest 'https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip' -OutFile '%TEMP%\ff.zip' -UseBasicParsing"
if errorlevel 1 (
    echo     [!] Download failed. Manual: https://www.gyan.dev/ffmpeg/builds/
    goto :eof
)
echo     Extracting...
powershell -Command "Expand-Archive '%TEMP%\ff.zip' '%TEMP%\ff_ex' -Force"
for /d %%d in ("%TEMP%\ff_ex\ffmpeg-*") do (
    copy /Y "%%d\bin\ffmpeg.exe" "tools\ffmpeg\" >nul 2>&1
    copy /Y "%%d\bin\ffprobe.exe" "tools\ffmpeg\" >nul 2>&1
)
del "%TEMP%\ff.zip" 2>nul & rmdir /s /q "%TEMP%\ff_ex" 2>nul
if exist "tools\ffmpeg\ffmpeg.exe" (echo     [OK] FFmpeg ready!) else (echo     [!] Extract failed)
goto :eof
