@echo off
setlocal enabledelayedexpansion
title LeAI Enterprise Demo (Port 9090)

echo ========================================================
echo   LeAI Enterprise Demo - Port 9090
echo   Auth callback + Webhook + iframe embed
echo ========================================================
echo.

:: --- JDK ---
set "JAVA_HOME=M:\SDK\jdk8u482-b08"
if not exist "%JAVA_HOME%\bin\java.exe" (
    echo [ERROR] JDK not found: %JAVA_HOME%
    echo         Please edit JAVA_HOME in this script
    pause
    exit /b 1
)
set "PATH=%JAVA_HOME%\bin;%PATH%"
echo [OK] JDK: %JAVA_HOME%

:: --- Kill port 9090 ---
for /f "tokens=5" %%a in ('netstat -aon ^| findstr ":9090 " ^| findstr "LISTENING"') do (
    echo [!] Killing PID %%a on port 9090
    taskkill /F /PID %%a >nul 2>&1
)

:: --- Maven build ---
echo.
echo [*] Building with Maven...
if exist "mvnw.cmd" (
    call mvnw.cmd clean package -DskipTests -q
) else (
    call mvn clean package -DskipTests -q
)

:: --- Find JAR ---
set "JAR_FILE="
for /f "delims=" %%f in ('dir /b /s target\*.jar 2^>nul ^| findstr /v "original"') do (
    set "JAR_FILE=%%f"
)
if "!JAR_FILE!"=="" (
    echo [ERROR] JAR file not found. Build may have failed.
    pause
    exit /b 1
)
echo [OK] JAR: !JAR_FILE!

:: --- Start ---
echo.
echo ========================================================
echo   Starting Enterprise Demo...
echo.
echo   Home:     http://192.168.1.72:9090
echo   iframe:   http://192.168.1.72:9090/enterprise-sim.html
echo   Auth:     http://192.168.1.72:9090/leai-auth
echo   Webhook:  http://192.168.1.72:9090/webhook/leai
echo.
echo   Requires: LeAI backend(8080) + H5 frontend(3001)
echo ========================================================
echo.

java -jar -Xms128m -Xmx256m "!JAR_FILE!"

pause
