@echo off
title SAMI Caddy API Server
echo ========================================
echo Installing SAMI Caddy API Server...
echo ========================================

REM Check if Node.js is installed
echo Checking for Node.js...
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo ERROR: Node.js is not installed or not in PATH
    echo Please install Node.js from https://nodejs.org/
    echo.
    pause
    exit /b 1
)

echo Node.js found: 
node --version

echo.
echo Installing dependencies...
echo.

REM Install npm dependencies
npm install

if %errorlevel% neq 0 (
    echo.
    echo ERROR: Failed to install dependencies
    echo Check the error messages above
    echo.
    pause
    exit /b 1
)

echo.
echo ========================================
echo Dependencies installed successfully!
echo ========================================
echo.
echo Starting Caddy API server...
echo.
echo Server URL: http://localhost:3001
echo Test URL: http://localhost:3001/api/caddy/test
echo.
echo Press Ctrl+C to stop the server
echo ========================================
echo.

REM Start the server and keep window open on error
npm start
if %errorlevel% neq 0 (
    echo.
    echo ERROR: Server failed to start
    echo Check the error messages above
    echo.
)

echo.
echo Server stopped.
pause