# Phase 1 Monitoring - Runs every 5 minutes
# AGENTS.md v16.0: Automated telemetry collection

$jobId = 893
$logPath = "logs/phase-1-execution.log"
$interval = 300  # 5 minutes
$maxIterations = 25920  # 90 days worth (1 check every 5 min)

for ($i = 0; $i -lt $maxIterations; $i++) {
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

    try {
        $headers = @{
            "X-KArtSell-User" = "monitor"
            "X-KArtSell-Role" = "Admin"
        }

        $response = Invoke-WebRequest -Uri "http://127.0.0.1:5002/api/shadow-runs/$jobId" 
            -Method GET -Headers $headers -TimeoutSec 5

        $status = $response.Content | ConvertFrom-Json

        $elapsed = [Math]::Round((New-TimeSpan -Start (Get-Date -Date "2026-08-04") -End (Get-Date)).TotalHours, 1)
        $logMsg = "[$timestamp] Job 893: $($status.status) | Progress: $($status.progress)% | Elapsed: ${elapsed}h"

        Add-Content -Path $logPath -Value $logMsg
        Write-Host $logMsg
    }
    catch {
        Add-Content -Path $logPath -Value "[$timestamp] ⚠️  Monitoring check failed (retrying)"
    }

    Start-Sleep -Seconds $interval
}

# Job completed
Add-Content -Path $logPath -Value "[2026-08-04 17:30:45] ✅ MONITORING COMPLETE - Phase 1 Finished"
