# Build Python backend as standalone binary using PyInstaller (Windows)
# Usage: .\scripts\build-backend.ps1 [target-triple]

param(
    [string]$TargetTriple = "x86_64-pc-windows-msvc"
)

$ErrorActionPreference = "Stop"
$ProjectRoot = Resolve-Path (Join-Path $PSScriptRoot "..")

Write-Host "Building agentkit-server for $TargetTriple..."

Set-Location $ProjectRoot

# Install PyInstaller if not present
pip install pyinstaller 2>$null

# Build the binary
pyinstaller --onefile `
    --name "agentkit-server.exe" `
    --hidden-import agentkit.server `
    --hidden-import agentkit.server.app `
    --hidden-import agentkit.server.routes `
    --hidden-import agentkit.server.config `
    --hidden-import agentkit.cli.main `
    --hidden-import uvicorn.logging `
    --hidden-import uvicorn.lifespan.on `
    --hidden-import uvicorn.lifespan.off `
    --hidden-import uvicorn.protocols.websockets.auto `
    --hidden-import uvicorn.protocols.http.auto `
    --hidden-import uvicorn.protocols.websockets.wsproto_impl `
    --hidden-import uvicorn.protocols.websockets.websockets_impl `
    --hidden-import uvicorn.protocols.websockets.impl_11 `
    --hidden-import uvicorn.lifespan `
    --hidden-import sse_starlette `
    --distpath "$ProjectRoot\src-tauri\binaries" `
    --workpath "$ProjectRoot\build\pyinstaller-work" `
    --specpath "$ProjectRoot\build" `
    src\agentkit\__main__.py

# Rename with target triple suffix
Rename-Item "$ProjectRoot\src-tauri\binaries\agentkit-server.exe" "agentkit-server-$TargetTriple.exe"

Write-Host "Built: src-tauri\binaries\agentkit-server-$TargetTriple.exe"
