# Gate 5 Monitoring Dashboard
# Real-time tracking of Job 893 execution
# Updated: 2026-08-03 22:04 KST

param(
    [int]$IntervalSeconds = 300,  # Check every 5 minutes
    [int]$MaxHours = 48           # Run for max 48 hours
)

Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║           GATE 5: Job 893 Monitoring Dashboard             ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""

$startTime = Get-Date
$maxEndTime = $startTime.AddHours($MaxHours)
$jobId = "893"
$hostUrl = "http://127.0.0.1:5002"

# Initialize tracking
$checkCount = 0
$hostDownCount = 0

Write-Host "Configuration:" -ForegroundColor Yellow
Write-Host "  Start Time: $startTime" -ForegroundColor Gray
Write-Host "  Host URL: $hostUrl" -ForegroundColor Gray
Write-Host "  Job ID: $jobId" -ForegroundColor Gray
Write-Host "  Check Interval: ${IntervalSeconds}s" -ForegroundColor Gray
Write-Host "  Max Duration: ${MaxHours}h" -ForegroundColor Gray
Write-Host ""

# Monitoring loop
while ($true) {
    $checkCount++
    $currentTime = Get-Date
    $elapsedTime = $currentTime - $startTime
    $elapsedHours = [math]::Round($elapsedTime.TotalHours, 2)

    Write-Host "[$($currentTime.ToString('HH:mm:ss'))] Check #$checkCount (Elapsed: ${elapsedHours}h)" -ForegroundColor Green

    # Check Host health
    try {
        $response = Invoke-WebRequest -Uri "$hostUrl/health" `
            -TimeoutSec 3 `
            -ErrorAction Stop

        if ($response.StatusCode -eq 200) {
            Write-Host "  ✅ Host: RESPONDING" -ForegroundColor Green
            $hostDownCount = 0
        }
    } catch {
        $hostDownCount++
        Write-Host "  ⚠️ Host: NOT RESPONDING (attempt $hostDownCount)" -ForegroundColor Yellow

        if ($hostDownCount -gt 3) {
            Write-Host "  ❌ Host DOWN - Manual intervention required!" -ForegroundColor Red
        }
    }

    # Check .NET process
    $dotnetProc = Get-Process dotnet -ErrorAction SilentlyContinue
    if ($dotnetProc) {
        $memoryMB = [math]::Round($dotnetProc.WorkingSet / 1MB, 2)
        Write-Host "  ✅ Process: Running (PID: $($dotnetProc.Id), Memory: ${memoryMB}MB)" -ForegroundColor Green
    } else {
        Write-Host "  ❌ Process: NOT RUNNING" -ForegroundColor Red
    }

    # Check time limit
    if ($currentTime -gt $maxEndTime) {
        Write-Host ""
        Write-Host "Max monitoring duration reached. Ending monitoring." -ForegroundColor Yellow
        break
    }

    # Wait for next check
    Write-Host "  Waiting ${IntervalSeconds}s for next check..." -ForegroundColor Gray
    Start-Sleep -Seconds $IntervalSeconds
}

Write-Host ""
Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║              Monitoring Session Ended                      ║" -ForegroundColor Cyan
Write-Host "║  Total Checks: $checkCount" -ForegroundColor Cyan
Write-Host "║  Total Duration: $([math]::Round(((Get-Date) - $startTime).TotalHours, 2))h" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
