@echo off
setlocal enabledelayedexpansion

echo ========================================
echo    Market Data Proxy Starter
echo ========================================
echo.

REM Check if running as administrator (needed for hosts file modification)
net session >nul 2>&1
if %errorLevel% == 0 (
    echo [✓] Running as administrator
) else (
    echo [✗] This script requires administrator privileges
    echo    Right-click and "Run as administrator"
    echo.
    pause
    exit /b 1
)

echo.
echo [1/4] Adding market-price.insightbull.io to hosts file...
set "hosts_file=%windir%\System32\drivers\etc\hosts"
set "entry=127.0.0.1 market-price.insightbull.io"

REM Check if entry already exists
findstr /C:"market-price.insightbull.io" "%hosts_file%" >nul 2>&1
if !errorLevel! == 0 (
    echo [✓] market-price.insightbull.io already exists in hosts file
) else (
    echo. >> "%hosts_file%"
    echo !entry! >> "%hosts_file%"
    echo [✓] Added market-price.insightbull.io to hosts file
)

echo.
echo [2/4] Starting Node.js API server...
echo Starting API on port 3001...

REM Get current directory and store it for both services
set "PROJECT_DIR=%cd%"
set "NGINX_DIR=%PROJECT_DIR%\nginx-1.24.0"

REM Start Node.js API server from project directory
start "MarketDataAPI" cmd /k "cd /d "%PROJECT_DIR%" && node src/server.js"

REM Wait for API to start
timeout /t 5 /nobreak >nul

REM Test if API server started successfully
curl -s http://localhost:3001/health >nul 2>&1
if !errorLevel! == 0 (
    echo [✓] Node.js API server started successfully
) else (
    echo [✗] Failed to start API server
    goto :error
)

echo.
echo [3/4] Starting Nginx proxy server...

REM Start Nginx from its own directory
start "NginxProxy" cmd /k "cd /d "%NGINX_DIR%" && nginx.exe"

REM Wait for Nginx to start
timeout /t 3 /nobreak >nul

echo.
echo [4/4] Testing proxy connection...
echo Testing basic connectivity...
curl -s http://market-price.insightbull.io >nul 2>&1
if !errorLevel! == 0 (
    echo [✓] Proxy server is responding
) else (
    echo [✗] Proxy server not responding
    goto :error
)

echo.
echo Testing API through proxy...
curl -s http://market-price.insightbull.io/api/health >nul 2>&1
if !errorLevel! == 0 (
    echo [✓] API proxy is working correctly
) else (
    echo [✗] API proxy not working
    goto :error
)

echo.
echo ========================================
echo    SUCCESS! Proxy is ready!
echo ========================================
echo.
echo Your proxy is now running:
echo - API Server: http://localhost:3001
echo - Proxy URL:  http://market-price.insightbull.io
echo - MT5 URL:   http://market-price.insightbull.io/api/endpoint
echo.
echo You can now use http://market-price.insightbull.io/api/ in your MT5 EA
echo.
echo Press any key to close this window...
pause >nul
goto :eof

:error
echo.
echo ========================================
echo    ERROR! Something went wrong
echo ========================================
echo.
echo Please check:
echo 1. Is Node.js installed?
echo 2. Run 'npm install' if dependencies are missing
echo 3. Check if port 3001 is available
echo 4. Check Nginx error logs in nginx-1.24.0/logs/
echo.
echo Press any key to close this window...
pause >nul
exit /b 1
