@echo off
chcp 65001 >nul
echo.
echo ========================================
echo   AX Copilot - Build Script
echo ========================================
echo.

set APP=src\AxCopilot\AxCopilot.csproj
set ENCRYPTOR=src\AxKeyEncryptor\AxKeyEncryptor.csproj
set OFFLINE=src\AxCopilot.Installer\AxCopilot.Installer.csproj
set OUT=dist

:: Kill running app
tasklist /FI "IMAGENAME eq AxCopilot.exe" 2>nul | find /i "AxCopilot.exe" >nul
if %ERRORLEVEL%==0 (
    echo [0] Stopping AxCopilot...
    taskkill /IM AxCopilot.exe /F >nul 2>nul
    timeout /t 2 /nobreak >nul
)
:: Kill legacy process
tasklist /FI "IMAGENAME eq AxCommander.exe" 2>nul | find /i "AxCommander.exe" >nul
if %ERRORLEVEL%==0 (
    echo [0] Stopping legacy AxCommander...
    taskkill /IM AxCommander.exe /F >nul 2>nul
    timeout /t 2 /nobreak >nul
)

if exist "%OUT%" rd /s /q "%OUT%" 2>nul
mkdir "%OUT%"
mkdir "%OUT%\AxCopilot"

:: ========================================
:: 1. Main app (self-contained, folder)
:: ========================================
echo [1/4] Building main app (self-contained)...
dotnet publish "%APP%" -c Release -o "%OUT%\AxCopilot" --self-contained true --nologo -v quiet
if %ERRORLEVEL% NEQ 0 ( echo [FAILED] Main app build & pause & exit /b 1 )
echo   OK - dist\AxCopilot\
echo.

:: ========================================
:: 2. AxKeyEncryptor (developer tool)
:: ========================================
echo [2/4] Building AxKeyEncryptor (WinForms)...
mkdir "%OUT%\AxKeyEncryptor" 2>nul
dotnet publish "%ENCRYPTOR%" -c Release -o "%OUT%\AxKeyEncryptor" --self-contained false --nologo -v quiet
if %ERRORLEVEL% NEQ 0 ( echo [FAILED] AxKeyEncryptor build & pause & exit /b 1 )
del /q "%OUT%\AxKeyEncryptor\*.pdb" 2>nul
echo   OK - dist\AxKeyEncryptor\
echo.

:: ========================================
:: 3. Create payload ZIP for installer
:: ========================================
echo [3/4] Creating installer payload ZIP...
powershell -NoProfile -Command "Compress-Archive -Path '%OUT%\AxCopilot\*' -DestinationPath 'src\AxCopilot.Installer\payload.zip' -Force"
echo   OK - payload.zip
echo.

:: ========================================
:: 4. Build installer (.NET Framework 4.8)
:: ========================================
echo [4/4] Building installer (.NET Framework 4.8)...
dotnet build "%OFFLINE%" -c Release --nologo -v quiet
if %ERRORLEVEL% NEQ 0 ( echo [FAILED] Installer build & pause & exit /b 1 )
copy /Y "src\AxCopilot.Installer\bin\Release\net48\AxCopilot_Setup.exe" "%OUT%\" >nul
for %%F in ("%OUT%\AxCopilot_Setup.exe") do echo   OK - AxCopilot_Setup.exe (%%~zF bytes)
echo.

:: ========================================
:: Cleanup
:: ========================================
:: Remove debug symbols and metadata (anti-decompile)
del /q "%OUT%\*.pdb" 2>nul
del /q "%OUT%\AxCopilot\*.pdb" 2>nul
del /q "%OUT%\AxCopilot\*.xml" 2>nul
del /q "%OUT%\*.deps.json" 2>nul
del /q "%OUT%\*.runtimeconfig.json" 2>nul
del /q "src\AxCopilot.Installer\payload.zip" 2>nul

echo ========================================
echo   Build Complete!
echo ========================================
echo.
echo   dist\AxCopilot\                Main app (EXE + DLL)
echo   dist\AxKeyEncryptor\           Settings Encryptor (dev tool)
echo   dist\AxCopilot_Setup.exe       Installer (offline, .NET 4.8)
echo.
pause
