# PowerShell script to build Wails client
$ErrorActionPreference = "Stop"

Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "           BUILDING SECURE WAILS CLIENT       " -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan

# Setup Go path for tools like garble
try {
    $goPath = go env GOPATH
    if ($goPath) {
        $goBin = Join-Path $goPath "bin"
        if (Test-Path $goBin) {
            if ($env:PATH -notlike "*$goBin*") {
                $env:PATH = "$goBin;$env:PATH"
                Write-Host "[*] Added $goBin to PATH temporarily" -ForegroundColor Gray
            }
        }
    }
} catch {
    Write-Host "[!] Could not resolve GOPATH. Proceeding with default PATH..." -ForegroundColor Yellow
}

# Ensure Garble is installed
$garbleInstalled = $null -ne (Get-Command garble -ErrorAction SilentlyContinue)
if (-not $garbleInstalled) {
    Write-Host "[*] Garble is not installed. Installing mvdan.cc/garble@latest..." -ForegroundColor Cyan
    try {
        go install mvdan.cc/garble@latest
        Write-Host "[+] Garble installed successfully!" -ForegroundColor Green
    } catch {
        Write-Host "[!] Failed to install Garble. Will fall back to standard Go compiler." -ForegroundColor Yellow
    }
}

# Check if wails is installed
$wailsInstalled = $null -ne (Get-Command wails -ErrorAction SilentlyContinue)

if ($wailsInstalled) {
    Write-Host "[*] Found Wails CLI. Building via Wails with Obfuscation..." -ForegroundColor Green
    try {
        $garbleInstalled = $null -ne (Get-Command garble -ErrorAction SilentlyContinue)
        if ($garbleInstalled) {
            Write-Host "[*] Compiling using Garble compiler..." -ForegroundColor Green
            wails build -clean -compiler garble -obfuscated -trimpath -m -ldflags "-s -w"
        } else {
            Write-Host "[!] Garble not available. Compiling with default Go compiler..." -ForegroundColor Yellow
            wails build -clean -ldflags "-s -w"
        }
        Write-Host "[+] Build completed successfully using Wails CLI!" -ForegroundColor Green
        exit 0
    } catch {
        Write-Host "[-] Wails build failed. Attempting manual fallback build..." -ForegroundColor Yellow
    }
} else {
    Write-Host "[!] Wails CLI not found. Falling back to manual build..." -ForegroundColor Yellow
}

# Check for Go
if ($null -eq (Get-Command go -ErrorAction SilentlyContinue)) {
    Write-Host "[!] Go is not installed or not in PATH." -ForegroundColor Red
    exit 1
}

# Check for npm
if ($null -eq (Get-Command npm -ErrorAction SilentlyContinue)) {
    Write-Host "[!] Node.js/npm is not installed or not in PATH." -ForegroundColor Red
    exit 1
}

# Step 1: Build Frontend
Write-Host "[*] Building frontend assets..." -ForegroundColor Cyan
Push-Location frontend

try {
    if (-not (Test-Path "node_modules")) {
        Write-Host "[*] node_modules not found. Running 'npm install'..." -ForegroundColor Cyan
        npm install
    }
    
    Write-Host "[*] Running 'npm run build'..." -ForegroundColor Cyan
    npm run build
} finally {
    Pop-Location
}

# Step 2: Build Backend Go binary
Write-Host "[*] Building Go application..." -ForegroundColor Cyan

try {
    $garbleInstalled = $null -ne (Get-Command garble -ErrorAction SilentlyContinue)
    if ($garbleInstalled) {
        Write-Host "[*] Compiling manual build with Garble..." -ForegroundColor Green
        garble build -ldflags="-s -w -H windowsgui" -o client.exe main.go app.go
    } else {
        Write-Host "[!] Garble not available. Compiling manual build with standard Go..." -ForegroundColor Yellow
        go build -ldflags="-s -w -H windowsgui" -o client.exe main.go app.go
    }
    Write-Host "[+] Build completed successfully! Generated client.exe" -ForegroundColor Green
} catch {
    Write-Host "[-] Go build failed." -ForegroundColor Red
    exit 1
}
