# Build WoW_MultiTool.exe (GUI)
# Usage: .\build_exe.ps1

$ErrorActionPreference = "Stop"
Set-Location $PSScriptRoot

Write-Host "Checking dependencies..." -ForegroundColor Cyan
& pip show pyinstaller 2>$null | Out-Null
if ($LASTEXITCODE -ne 0) {
    Write-Host "Installing dependencies..." -ForegroundColor Yellow
    pip install -r requirements.txt
}

Write-Host "Building WoW_MultiTool.exe ..." -ForegroundColor Cyan
pyinstaller --noconfirm build_wow_multikey.spec
if ($LASTEXITCODE -ne 0) { exit 1 }

$outExe = "dist\WoW_MultiTool.exe"

Write-Host "Copying recorder folder to dist..." -ForegroundColor Cyan
if (Test-Path "dist\recorder") { Remove-Item "dist\recorder" -Recurse -Force -ErrorAction SilentlyContinue }
if (Test-Path "recorder") { Copy-Item "recorder" "dist\recorder" -Recurse -Force }

Write-Host "Copying game_state_config.json to dist..." -ForegroundColor Cyan
if (Test-Path "game_state_config.json") { Copy-Item "game_state_config.json" "dist\game_state_config.json" -Force }

# Optional cleanup of old outputs
if (Test-Path "dist\AutoBotMove.exe") { Remove-Item "dist\AutoBotMove.exe" -Force -ErrorAction SilentlyContinue }
if (Test-Path "dist\vendor.json") { Remove-Item "dist\vendor.json" -Force -ErrorAction SilentlyContinue }
if (Test-Path "dist\waypoints.json") { Remove-Item "dist\waypoints.json" -Force -ErrorAction SilentlyContinue }

if (Test-Path $outExe) {
    Write-Host "`nBuild done!" -ForegroundColor Green
    Write-Host "Output: $outExe" -ForegroundColor Green
    if (Test-Path "dist\recorder") { Write-Host "Copied: dist\recorder" -ForegroundColor Green }
    exit 0
} else {
    Write-Host "Build failed: $outExe not found." -ForegroundColor Red
    exit 1
}
