@echo off
setlocal EnableDelayedExpansion
title MP Manager Restarter
echo ===================================================
echo   MP Manager - Reiniciando Servidor
echo ===================================================
echo.

cd /d "%~dp0"

:: ----------------------------------------------------------------------
:: 1. Determinar modo activo (normal / persistent)
:: ----------------------------------------------------------------------
set "MODE=normal"
set "LAST_MODE_FILE=generated\runtime\last_mode"
if exist %LAST_MODE_FILE% (
    for /f "usebackq tokens=*" %%a in (`type %LAST_MODE_FILE%`) do set "MODE=%%a"
)
if not "!MODE!"=="normal" if not "!MODE!"=="persistent" set "MODE=normal"
echo [SISTEMA] Modo detectado: !MODE!

:: ----------------------------------------------------------------------
:: 2. Verificar Python
:: ----------------------------------------------------------------------
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] No se detecto Python en el PATH.
    pause
    exit /b 1
)

:: ----------------------------------------------------------------------
:: 3. Detener el server actual de forma segura (solo MP Manager, jamas
::    procesos de otros proyectos en el mismo puerto).
:: ----------------------------------------------------------------------
python runtime_control.py stop --force

:: ----------------------------------------------------------------------
:: 4. Limpiar Chromium headless huerfanos (de scripts de Playwright).
:: ----------------------------------------------------------------------
echo [SISTEMA] Limpiando zombies de Chromium headless si los hay...
taskkill /f /im chrome-headless-shell.exe >nul 2>&1

:: ----------------------------------------------------------------------
:: 5. Limpiar batch files interrumpidos del directorio runtime.
:: ----------------------------------------------------------------------
if exist generated\runtime\batch\_bulk_batch_*.json (
    echo [SISTEMA] Limpiando batch files huerfanos...
    del generated\runtime\batch\_bulk_batch_*.json >nul 2>&1
)
if exist generated\runtime\batch\_test_batch.json del generated\runtime\batch\_test_batch.json >nul 2>&1

:: ----------------------------------------------------------------------
:: 6. Pequena espera para liberar el puerto antes de relanzar.
:: ----------------------------------------------------------------------
echo [SISTEMA] Esperando 2 segundos para liberar puerto...
timeout /t 2 >nul

:: ----------------------------------------------------------------------
:: 7. Relanzar en el modo correcto.
:: ----------------------------------------------------------------------
if not exist "generated\runtime" mkdir "generated\runtime"
if "!MODE!"=="persistent" (
    set "GHL_BROWSER_PROFILE_DIR=%~dp0generated\browser\profile"
    echo [SISTEMA] Relanzando en modo PERFIL PERSISTENTE.
    echo [SISTEMA] GHL_BROWSER_PROFILE_DIR=!GHL_BROWSER_PROFILE_DIR!
    if not exist "!GHL_BROWSER_PROFILE_DIR!" (
        echo.
        echo [ADVERTENCIA] El perfil persistente aun no existe.
        echo               Cuando arranque, usa "Renovar sesion Bucefalo" en el dashboard.
        echo.
    )
    > %LAST_MODE_FILE% echo persistent
    start "MP Manager Server (Perfil Persistente)" cmd /k "set GHL_BROWSER_PROFILE_DIR=!GHL_BROWSER_PROFILE_DIR! && python ""%~dp0main.py"""
) else (
    echo [SISTEMA] Relanzando en modo NORMAL.
    > %LAST_MODE_FILE% echo normal
    start "MP Manager Server" python "%~dp0main.py"
)

echo.
echo ===================================================
echo   Servidor reiniciado en modo !MODE!.
echo   Para detener: stop.bat
echo ===================================================
timeout /t 4 >nul
endlocal
exit /b 0
