# Phase 1 모니터링 및 Phase 2 자동 트리거 스크립트
# 용도: Phase 1 완료 감지 → Phase 2 검증 메트릭 자동 계산

param(
    [int]$CheckIntervalSeconds = 30,
    [int]$MaxWaitMinutes = 120,
    [bool]$AutoRunPhase2 = $true
)

$Green = "Green"
$Yellow = "Yellow"
$Red = "Red"
$Cyan = "Cyan"

Write-Host "`n╔════════════════════════════════════════════╗" -ForegroundColor $Green
Write-Host "║  Phase 1 모니터링 & Phase 2 자동 트리거   ║" -ForegroundColor $Green
Write-Host "╚════════════════════════════════════════════╝`n" -ForegroundColor $Green

# 설정
$ProjectDir = "C:\Job_Roomz\KArtSell.Aegis"
$HostUrl = "http://127.0.0.1:5002"
$RunId = "54e53e70-19b6-4525-8118-b76a12f85a96"  # 방금 시작한 Shadow Run ID
$Phase2ScriptPath = Join-Path $ProjectDir "tools\phase2_automation.ps1"
$LogDir = Join-Path $ProjectDir "logs"
New-Item -ItemType Directory -Path $LogDir -Force -ErrorAction SilentlyContinue | Out-Null

$MonitorLogFile = Join-Path $LogDir "phase1_monitor_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"

Write-Host "📋 모니터링 설정:" -ForegroundColor $Cyan
Write-Host "   RunId: $RunId"
Write-Host "   CheckInterval: ${CheckIntervalSeconds}초"
Write-Host "   MaxWaitTime: ${MaxWaitMinutes}분"
Write-Host "   Phase2Auto: $AutoRunPhase2"
Write-Host "   로그: $MonitorLogFile`n"

# 모니터링 함수
function Get-ShadowRunStatus {
    param(
        [string]$RunId,
        [string]$HostUrl
    )

    try {
        $headers = @{
            "X-KArtSell-User" = "phase-monitor"
            "X-KArtSell-Role" = "Admin"
            "Content-Type" = "application/json"
        }

        $uri = "$HostUrl/api/shadow-runs/$RunId"
        $response = Invoke-WebRequest -Uri $uri `
            -Method GET `
            -Headers $headers `
            -ErrorAction Stop `
            -TimeoutSec 10

        return $response.Content | ConvertFrom-Json
    } catch {
        return $null
    }
}

# 메인 모니터링 루프
$startTime = Get-Date
$elapsedMinutes = 0
$phase1Complete = $false
$statusCheckCount = 0

Write-Host "🔍 Phase 1 모니터링 시작... (최대 ${MaxWaitMinutes}분)" -ForegroundColor $Yellow
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor $Yellow

while ($elapsedMinutes -lt $MaxWaitMinutes) {
    $statusCheckCount++

    $status = Get-ShadowRunStatus -RunId $RunId -HostUrl $HostUrl

    if ($status) {
        $currentStatus = $status.status
        $currentElapsed = [Math]::Round(((Get-Date) - $startTime).TotalMinutes, 1)

        # 로그 기록
        "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] Check #$statusCheckCount - Status: $currentStatus (${currentElapsed}분 경과)" | Tee-Object -FilePath $MonitorLogFile -Append | Out-Null

        # 콘솔 출력
        switch ($currentStatus) {
            "Queued" {
                Write-Host "⏳ 상태: Queued (작업 대기 중) - ${currentElapsed}분 경과" -ForegroundColor $Yellow
            }
            "Running" {
                Write-Host "🟡 상태: Running (실행 중) - ${currentElapsed}분 경과" -ForegroundColor $Cyan
            }
            "Completed" {
                Write-Host "`n✅ Phase 1 완료됨! (${currentElapsed}분 소요)" -ForegroundColor $Green
                $phase1Complete = $true
                break
            }
            "Failed" {
                Write-Host "`n❌ Phase 1 실패!" -ForegroundColor $Red
                Write-Host "에러: $($status.errorMessage)" -ForegroundColor $Red
                exit 1
            }
            default {
                Write-Host "⏳ 상태: $currentStatus - ${currentElapsed}분 경과" -ForegroundColor $Yellow
            }
        }
    } else {
        Write-Host "⚠️  상태 조회 실패 (Host 미응답)" -ForegroundColor $Yellow
    }

    # 다음 확인까지 대기
    Start-Sleep -Seconds $CheckIntervalSeconds
    $elapsedMinutes = [Math]::Round(((Get-Date) - $startTime).TotalMinutes, 1)
}

Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`n" -ForegroundColor $Yellow

if ($phase1Complete) {
    # Phase 2 자동 시작
    Write-Host "📊 Phase 2 검증 메트릭 계산 준비..." -ForegroundColor $Cyan
    Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

    # Phase 1 데이터 내보내기
    Write-Host "`n1️⃣  Phase 1 데이터 내보내기..." -ForegroundColor $Yellow

    try {
        # 데이터 디렉토리 준비
        $DataDir = Join-Path $ProjectDir "data\phase1"
        New-Item -ItemType Directory -Path $DataDir -Force -ErrorAction SilentlyContinue | Out-Null

        # CSV 파일 경로
        $phase1DataCsv = Join-Path $DataDir "phase1_results_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"

        # 실제로는 DB에서 데이터를 내보내야 하지만, 여기서는 경로만 준비
        Write-Host "   데이터 저장 경로: $phase1DataCsv" -ForegroundColor $Green

        # Phase 2 스크립트 실행
        if ($AutoRunPhase2) {
            Write-Host "`n2️⃣  Phase 2 검증 스크립트 실행..." -ForegroundColor $Yellow

            Write-Host "   명령어: & '$Phase2ScriptPath' -DataFile '$phase1DataCsv'" -ForegroundColor $Gray

            # Phase 2 자동화 스크립트 실행
            & $Phase2ScriptPath -DataFile $phase1DataCsv 2>&1 | Tee-Object -FilePath $MonitorLogFile -Append

            Write-Host "`n✅ Phase 2 실행 완료" -ForegroundColor $Green
        } else {
            Write-Host "`n2️⃣  Phase 2 검증 스크립트 준비 완료 (수동 실행 필요)" -ForegroundColor $Yellow
            Write-Host "   실행 명령어:" -ForegroundColor $Gray
            Write-Host "   & '$Phase2ScriptPath' -DataFile '$phase1DataCsv'" -ForegroundColor $Gray
        }

    } catch {
        Write-Host "❌ 오류: $($_.Exception.Message)" -ForegroundColor $Red
        "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] ERROR: $($_.Exception.Message)" | Tee-Object -FilePath $MonitorLogFile -Append | Out-Null
    }
} else {
    Write-Host "⏱️  모니터링 타임아웃 (${MaxWaitMinutes}분 경과)" -ForegroundColor $Yellow
    Write-Host "Phase 1이 여전히 실행 중입니다. 더 오래 대기하려면 스크립트를 다시 실행하세요." -ForegroundColor $Yellow
}

Write-Host "`n═══════════════════════════════════════════" -ForegroundColor $Cyan
Write-Host "모니터링 및 자동화 완료" -ForegroundColor $Cyan
Write-Host "═══════════════════════════════════════════`n" -ForegroundColor $Cyan
