# CapInt task helper (PowerShell)
# Usage: .\scripts\capint-task.ps1 "prd hazırla"
#        $env:CAPINT_DEFAULT_CONFIRM="plan_first"; .\scripts\capint-task.ps1 "i18n"

param(
  [Parameter(Mandatory = $true, Position = 0)]
  [string]$Task
)

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
$capintJs = Join-Path $root "bin\capint.js"

if (Get-Command capint -ErrorAction SilentlyContinue) {
  $capint = @("capint")
} elseif (Test-Path $capintJs) {
  $capint = @("node", $capintJs)
} else {
  Write-Error "capint not found — npm install -g @bcelep/capint"
}

& @capint[0] @capint[1..($capint.Length - 1)] pipeline $Task --force 2>$null
if ($LASTEXITCODE -ne 0) {
  & @capint[0] @capint[1..($capint.Length - 1)] doctor
}

$json = & @capint[0] @capint[1..($capint.Length - 1)] route --json $Task | Out-String
$data = $json | ConvertFrom-Json
$ei = $data.execution_intent
[PSCustomObject]@{
  intent            = $ei.intent
  capability        = $ei.capability
  resolution        = $ei.resolution
  resolution_status = $ei.resolution_status
  plan              = $ei.plan
  confirm_default   = $ei.confirm_default_option
  files_to_read     = @($ei.explanation.files_to_read)
} | ConvertTo-Json -Depth 5
