#Requires -RunAsAdministrator
$ErrorActionPreference = "Continue"
. (Join-Path $PSScriptRoot "_env.ps1")

Write-Host "Stopping CPC Sota capture watcher..." -ForegroundColor Yellow

Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" -ErrorAction SilentlyContinue |
  Where-Object { $_.CommandLine -match 'Watch-SotaCapture\.ps1' -and $_.CommandLine -match 'claude-proxy-control' } |
  ForEach-Object {
    Write-Host ("  kill pid " + $_.ProcessId)
    Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
  }

try {
  Stop-ScheduledTask -TaskName $TaskWatcher -ErrorAction SilentlyContinue
  Write-Host ("  scheduled task stopped: " + $TaskWatcher) -ForegroundColor Green
} catch {
  Write-Host ("  stop task warn: " + $_.Exception.Message)
}

$statusDir = Join-Path $MergedDir "sota-captured"
New-Item -ItemType Directory -Force -Path $statusDir | Out-Null
@{
  state = "stopped"
  time = (Get-Date).ToString("o")
  pid = $null
} | ConvertTo-Json | Set-Content (Join-Path $statusDir "watcher-status.json") -Encoding UTF8

if (-not $env:CLAUDE_PROXY_NOPAUSE) { Pause }
