#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Real-time deployment status verification
.DESCRIPTION
    Checks current deployment status across all phases
#>

param(
    [switch]$RealTime
)

$ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)

Write-Host "╔════════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║         K-ArtSell Aegis v16.0: DEPLOYMENT STATUS          ║" -ForegroundColor Cyan
Write-Host "╚════════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""

# Check Phase 1: Backend
Write-Host "PHASE 1: Backend Deployment" -ForegroundColor Yellow
$backendDll = Join-Path $ProjectRoot "publish" "KArtSell.Host.dll"
if (Test-Path $backendDll) {
    $size = (Get-Item $backendDll).Length / 1MB
    Write-Host "  ✅ Backend binary ready: $([Math]::Round($size, 2)) MB" -ForegroundColor Green
} else {
    Write-Host "  ⏳ Backend not yet built" -ForegroundColor Yellow
}

# Check Phase 2: Frontend
Write-Host ""
Write-Host "PHASE 2: Frontend Build" -ForegroundColor Yellow
$indexHtml = Join-Path $ProjectRoot "frontend" "dist" "index.html"
if (Test-Path $indexHtml) {
    $distSize = (Get-ChildItem -Path (Join-Path $ProjectRoot "frontend" "dist") -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
    Write-Host "  ✅ Frontend dist ready: $([Math]::Round($distSize, 2)) MB" -ForegroundColor Green
} else {
    Write-Host "  ⏳ Frontend not yet built" -ForegroundColor Yellow
}

# Check Phase 3: Nginx
Write-Host ""
Write-Host "PHASE 3: Nginx Configuration" -ForegroundColor Yellow
$nginxConfig = Join-Path $ProjectRoot "nginx-kartsell.conf"
if (Test-Path $nginxConfig) {
    Write-Host "  ✅ Nginx configuration ready" -ForegroundColor Green
} else {
    Write-Host "  ⏳ Nginx configuration not yet generated" -ForegroundColor Yellow
}

# Check Phase 1 Runtime
Write-Host ""
Write-Host "PHASE 1 RUNTIME: Job 893 Status" -ForegroundColor Yellow
$logFile = Join-Path $ProjectRoot "logs" "phase-1-execution.log"
if (Test-Path $logFile) {
    $lastLines = Get-Content $logFile -Tail 5
    Write-Host "  ✅ Phase 1 logs found:" -ForegroundColor Green
    foreach ($line in $lastLines) {
        Write-Host "     $line" -ForegroundColor Gray
    }
} else {
    Write-Host "  ⏳ Phase 1 execution started (check logs later)" -ForegroundColor Yellow
}

# Summary
Write-Host ""
Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "SUMMARY" -ForegroundColor Yellow

$readyCount = 0
if (Test-Path $backendDll) { $readyCount++ }
if (Test-Path $indexHtml) { $readyCount++ }
if (Test-Path $nginxConfig) { $readyCount++ }

Write-Host "  Ready for deployment: $readyCount / 3 phases" -ForegroundColor Green
Write-Host ""
Write-Host "NEXT STEPS:" -ForegroundColor Yellow
Write-Host "  1. Run: .\scripts\COMPLETE_DEPLOYMENT_AUTOMATION.ps1" -ForegroundColor Cyan
Write-Host "  2. Follow output instructions" -ForegroundColor Cyan
Write-Host "  3. Deploy to production server" -ForegroundColor Cyan
Write-Host ""
Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Cyan
