[CmdletBinding()]
param(
    [string]$Python = "python"
)

$ErrorActionPreference = "Stop"
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot "..")).Path
$distRoot = Join-Path $projectRoot "dist\ProcessMonitor"
$workRoot = Join-Path $projectRoot "build\pyinstaller"

& $Python -m PyInstaller --noconfirm --clean --distpath $distRoot --workpath $workRoot (Join-Path $PSScriptRoot "ProcessMonitor.spec")
if ($LASTEXITCODE -ne 0) { throw "ProcessMonitor GUI build failed." }
& $Python -m PyInstaller --noconfirm --clean --distpath $distRoot --workpath $workRoot (Join-Path $PSScriptRoot "ProcessMonitorRelay.spec")
if ($LASTEXITCODE -ne 0) { throw "ProcessMonitor relay build failed." }

$configDir = New-Item -ItemType Directory -Force -Path (Join-Path $distRoot "config")
$driverDir = New-Item -ItemType Directory -Force -Path (Join-Path $distRoot "driver")
New-Item -ItemType Directory -Force -Path (Join-Path $distRoot "data") | Out-Null
New-Item -ItemType Directory -Force -Path (Join-Path $distRoot "logs") | Out-Null
Copy-Item -LiteralPath (Join-Path $projectRoot "config\process_monitor_config.json") -Destination $configDir.FullName -Force
Copy-Item -LiteralPath (Join-Path $projectRoot "x64\Debug\ProcessMonitor\ProcessMonitor.sys") -Destination $driverDir.FullName -Force
Copy-Item -LiteralPath (Join-Path $PSScriptRoot "validate_clean_vm.ps1") -Destination $distRoot -Force

Write-Host "Standalone output: $distRoot"
