@echo off
setlocal EnableDelayedExpansion
title Installation - Jade Hub (Coach WoW IA)
chcp 65001 >nul

echo =======================================================
echo            INSTALLATION DU HUB JADE - COACH WOW IA
echo =======================================================
echo.
echo   Le Hub est le lanceur central. Il telecharge et lance
echo   le Coach WoW. Laisse cette fenetre ouverte jusqu'a la fin.
echo.

:: 1. Configuration
set "SERVER_URL=https://jade-ia.duckdns.org"
set "PYTHON_URL=https://www.python.org/ftp/python/3.12.9/python-3.12.9-amd64.exe"
set "PYTHON_INSTALLER=python_installer.exe"
set "ZIP_NAME=Jade_Bundle.zip"
set "INSTALL_DIR=%CD%\CoachWoW_IA"

:: 2. Verification / installation de Python 3.12
set "PY_EXE="
for %%v in (3.12 3.13) do (
    py -%%v --version >nul 2>&1
    if not errorlevel 1 (
        set "PY_EXE=py -%%v"
        goto :PY_FOUND
    )
)
python --version >nul 2>&1
if not errorlevel 1 (
    for /f "tokens=2" %%V in ('python --version 2^>^&1') do set "PY_VER=%%V"
    echo !PY_VER! | findstr /R "^3\.1[2-9]\." >nul 2>&1
    if not errorlevel 1 (
        set "PY_EXE=python"
        goto :PY_FOUND
    )
)
echo [INFO] Python 3.12 introuvable. Telechargement de Python 3.12.9...
where curl.exe >nul 2>&1
if not errorlevel 1 (
    curl.exe -L --ssl-no-revoke --retry 2 -o "!PYTHON_INSTALLER!" "!PYTHON_URL!"
) else (
    powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Enum]::ToObject([Net.SecurityProtocolType], 3072); (New-Object Net.WebClient).DownloadFile('!PYTHON_URL!', '!PYTHON_INSTALLER!')"
)
if exist "!PYTHON_INSTALLER!" (
    echo [INFO] Installation silencieuse de Python 3.12 ^(1 a 2 minutes^)...
    start /wait "" "!PYTHON_INSTALLER!" /quiet InstallAllUsers=0 PrependPath=1 Include_test=0 Include_doc=0
    del "!PYTHON_INSTALLER!"
    echo [INFO] Python 3.12 installe.
    for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USERPATH=%%B"
    if defined USERPATH set "PATH=!PATH!;!USERPATH!"
    set "PY_EXE=python"
) else (
    echo [ERREUR] Telechargement de Python impossible. Verifie ta connexion.
    pause
    exit /b 1
)
:PY_FOUND
echo [INFO] Python detecte : !PY_EXE!

:: 3. Telechargement du Hub Jade (gratuit, sans JWT requis)
if not exist "!INSTALL_DIR!" mkdir "!INSTALL_DIR!"
echo.
echo [INFO] Telechargement du Hub Jade depuis le serveur ^(patiente^)...

:: Methode 1 : curl.exe (Windows 10+ natif, meilleure gestion TLS)
where curl.exe >nul 2>&1
if not errorlevel 1 (
    curl.exe -L --ssl-no-revoke --retry 2 --retry-delay 3 -o "!ZIP_NAME!" "!SERVER_URL!/api/download/hub"
    if exist "!ZIP_NAME!" goto :ZIP_OK
    echo [WARN] curl.exe a echoue, tentative via PowerShell...
)

:: Methode 2 : PowerShell avec TLS 1.2/1.3 et bypass certificat
powershell -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Enum]::ToObject([Net.SecurityProtocolType], 3072 -bor 12288); [Net.ServicePointManager]::ServerCertificateValidationCallback = [Net.Security.RemoteCertificateValidationCallback]{ $true }; (New-Object Net.WebClient).DownloadFile('!SERVER_URL!/api/download/hub', '!ZIP_NAME!'); Write-Host 'OK' } catch { Write-Host ('ERREUR: ' + $_.Exception.Message) }"

if not exist "!ZIP_NAME!" (
    echo [ERREUR] Telechargement impossible. Verifie ta connexion internet.
    echo Lien manuel : !SERVER_URL!/api/download/hub
    pause
    exit /b 1
)
:ZIP_OK

:: 4. Extraction
echo [INFO] Extraction des fichiers...
powershell -Command "Expand-Archive -Path '!ZIP_NAME!' -DestinationPath '!INSTALL_DIR!' -Force"
del "!ZIP_NAME!"

:: 5. Environnement Python + dependances
echo [INFO] Configuration de l'environnement Python...
cd /d "!INSTALL_DIR!"
if not exist "venv" (
    !PY_EXE! -m venv venv
)
call venv\Scripts\activate.bat
python -m pip install --upgrade pip --quiet

:: Dependances Hub (CORE/)
if exist "CORE\requirements.txt" (
    echo [INFO] Installation des dependances Hub...
    pip install -r CORE\requirements.txt --quiet
)
:: Dependances Coach WoW si present (telechargement post-login via le Hub)
if exist "MODULES\COACH_WOW\requirements.txt" (
    echo [INFO] Installation des dependances Coach WoW...
    pip install -r MODULES\COACH_WOW\requirements.txt --quiet
)
if exist "MODULES\COACH_WOW\requirements-vocal.txt" (
    echo [INFO] Installation des dependances vocales...
    pip install -r MODULES\COACH_WOW\requirements-vocal.txt --quiet
)
echo [INFO] Installation de PyAudio ^(micro^)...
pip install pyaudio --quiet 2>nul

:: 6. FFmpeg (moteur audio pour le vocal Gemini Live)
set "FFMPEG_ZIP=ffmpeg.zip"
if not exist "!INSTALL_DIR!\MODULES\COACH_WOW\ffmpeg.exe" (
    echo [INFO] Telechargement du moteur audio FFmpeg ^(patiente^)...
    set "FFMPEG_URL=https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip"
    where curl.exe >nul 2>&1
    if not errorlevel 1 (
        curl.exe -L --ssl-no-revoke --retry 2 -o "!FFMPEG_ZIP!" "!FFMPEG_URL!"
    ) else (
        powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Enum]::ToObject([Net.SecurityProtocolType], 3072); (New-Object Net.WebClient).DownloadFile('!FFMPEG_URL!', '!FFMPEG_ZIP!')"
    )
    if exist "!FFMPEG_ZIP!" (
        powershell -Command "Expand-Archive -Path '!FFMPEG_ZIP!' -DestinationPath 'ffmpeg_temp' -Force"
        copy /Y "ffmpeg_temp\*\bin\ffmpeg.exe"  "!INSTALL_DIR!\MODULES\COACH_WOW\" >nul 2>nul
        copy /Y "ffmpeg_temp\*\bin\ffprobe.exe" "!INSTALL_DIR!\MODULES\COACH_WOW\" >nul 2>nul
        rmdir /S /Q "ffmpeg_temp" 2>nul
        del "!FFMPEG_ZIP!" 2>nul
    )
)

:: 7. Raccourci Bureau (le Hub = point d'entree)
echo [INFO] Creation du raccourci "Jade Hub" sur le Bureau...
set "SHORTCUT_PATH=%USERPROFILE%\Desktop\Jade Hub.lnk"
set "TARGET_PATH=!INSTALL_DIR!\CORE\hub_launcher.py"
set "WORKING_DIR=!INSTALL_DIR!\CORE"
powershell -Command "$wshell = New-Object -ComObject WScript.Shell; $s = $wshell.CreateShortcut('!SHORTCUT_PATH!'); $s.TargetPath = '!INSTALL_DIR!\venv\Scripts\pythonw.exe'; $s.Arguments = '\"!TARGET_PATH!\"'; $s.WorkingDirectory = '!WORKING_DIR!'; $s.Save()"

echo.
echo =======================================================
echo    SUCCES ! LE HUB JADE EST INSTALLE.
echo.
echo    1^) Lance "Jade Hub" depuis le raccourci du Bureau.
echo    2^) Connecte-toi (identifiants fournis sur Discord).
echo    3^) Le Hub met a jour et lance le Coach WoW.
echo.
echo    ADDON WOW - copie le dossier :
echo      !INSTALL_DIR!\MODULES\COACH_WOW\WOW_ADDON\CoachWoWIA\
echo    dans :
echo      ...\World of Warcraft\_retail_\Interface\AddOns\
echo =======================================================
pause
