@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ============================================
echo   Service Party App - Windows Build Script
echo ============================================
echo.

cd /d "%~dp0"

:: Parse command line arguments
set "DO_CLEAN=0"
set "DO_CLEAN_ALL=0"

if "%1"=="clean" set "DO_CLEAN=1"
if "%1"=="--clean" set "DO_CLEAN=1"
if "%1"=="-c" set "DO_CLEAN=1"
if "%1"=="cleanall" set "DO_CLEAN_ALL=1"
if "%1"=="--clean-all" set "DO_CLEAN_ALL=1"

:: Clean all (includes node_modules)
if "%DO_CLEAN_ALL%"=="1" (
    echo [CLEAN] Performing full clean...
    if exist "dist" rmdir /s /q "dist"
    if exist "dist-electron" rmdir /s /q "dist-electron"
    if exist "release" rmdir /s /q "release"
    if exist "node_modules" rmdir /s /q "node_modules"
    if exist "package-lock.json" del /q "package-lock.json"
    if exist "node_modules\.cache" rmdir /s /q "node_modules\.cache"
    echo [OK] Full clean completed
    echo.
)

:: Clean build artifacts only
if "%DO_CLEAN%"=="1" (
    echo [CLEAN] Cleaning build artifacts...
    if exist "dist" rmdir /s /q "dist"
    if exist "dist-electron" rmdir /s /q "dist-electron"
    if exist "release" rmdir /s /q "release"
    if exist "node_modules\.cache" rmdir /s /q "node_modules\.cache"
    echo [OK] Clean completed
    echo.
)

:: Check Node.js
where node >nul 2>nul
if %errorlevel% neq 0 (
    echo [ERROR] Node.js not found. Please install Node.js 18+
    echo Download: https://nodejs.org/
    goto :error
)

:: Check Go
where go >nul 2>nul
if %errorlevel% neq 0 (
    echo [ERROR] Go not found. Please install Go 1.21+
    echo Download: https://go.dev/dl/
    goto :error
)

:: Show versions
echo [INFO] Node.js version:
node --version
echo [INFO] Go version:
go version
echo.

:: Step 1: Build TSS subprocess
echo ============================================
echo [1/4] Building TSS subprocess (Go)...
echo ============================================

cd tss-party
if not exist "..\bin\win32-x64" mkdir "..\bin\win32-x64"

echo Building tss-party.exe...
go build -ldflags="-s -w" -o ..\bin\win32-x64\tss-party.exe .
if %errorlevel% neq 0 (
    echo [ERROR] TSS subprocess build failed
    goto :error
)
echo [OK] tss-party.exe created
cd ..
echo.

:: Step 2: Install dependencies
echo ============================================
echo [2/4] Installing npm dependencies...
echo ============================================

if exist "node_modules" (
    if "%DO_CLEAN_ALL%"=="0" (
        echo Dependencies exist, skipping install
    ) else (
        call npm install
        if %errorlevel% neq 0 (
            echo [ERROR] npm install failed
            goto :error
        )
    )
) else (
    call npm install
    if %errorlevel% neq 0 (
        echo [ERROR] npm install failed
        goto :error
    )
)
echo [OK] Dependencies installed
echo.

:: Step 3: Check resources
echo ============================================
echo [3/4] Checking resources...
echo ============================================

if not exist "build" mkdir "build"
if not exist "build\icon.ico" echo [WARN] build\icon.ico not found, using default icon
echo.

:: Step 4: Build Electron app
echo ============================================
echo [4/4] Building Windows application...
echo ============================================

echo Building frontend code...
call npm run build:win
if %errorlevel% neq 0 (
    echo [ERROR] Application build failed
    goto :error
)

echo.
echo ============================================
echo   Build Complete!
echo ============================================
echo.
echo Output directory: %cd%\release\
echo.

:: List generated files
if exist "release" (
    echo Generated files:
    dir /b release\*.exe 2>nul
)

echo.
echo ============================================
echo   Usage Tips
echo ============================================
echo.
echo   build-windows.bat              Normal build
echo   build-windows.bat clean        Clean and rebuild
echo   build-windows.bat cleanall     Full clean (delete node_modules) and rebuild
echo.
echo Press any key to exit...
pause >nul
exit /b 0

:error
echo.
echo Build failed. Please check error messages.
echo.
echo If you encounter module/type errors, try:
echo   build-windows.bat cleanall
echo.
pause
exit /b 1
