# PowerShell helper script to build and verify the WordPress Plugin AI Builder container

param(
    [switch]$NoCache
)

$buildArgs = @('compose', 'build')
if ($NoCache) { $buildArgs += '--no-cache' }

Write-Host "Building WordPress Plugin AI Builder container..."
docker @buildArgs

if ($LASTEXITCODE -ne 0) {
    Write-Error "Build failed"
    exit 1
}

Write-Host "Starting container..."
docker compose up -d

Write-Host "Waiting for container readiness (healthcheck)..."
Start-Sleep -Seconds 5

Write-Host "Verifying tools inside container..."

docker compose exec wordpress-plugin-ai-builder pwsh -NoProfile -Command 'pwsh --version; opencode --version; node --version; if (Get-Command opencode -ErrorAction SilentlyContinue) { Write-Host "opencode available" } else { Write-Error "opencode not found"; exit 1 }'

if ($LASTEXITCODE -ne 0) {
    Write-Error "Verification failed"
    exit 1
}

Write-Host "All checks passed." -ForegroundColor Green
Write-Host "Open http://localhost:4500 for the web interface." -ForegroundColor Cyan
Write-Host "Verifying `github` helper is available in profile (sourcing profile)..."
docker compose exec wordpress-plugin-ai-builder pwsh -NoProfile -Command ". /root/.config/powershell/Microsoft.PowerShell_profile.ps1; Get-Command github"
if ($LASTEXITCODE -ne 0) { Write-Warning "`github` helper not detected. Ensure you built the image with the new profile, or update the volume via README instructions." }
