# Monitor the next scheduled sync run to verify the fix
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Monitoring Next Sync Run ===" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    $logFile = "C:\Shares\test\scripts\sync-from-nas.log"

    Write-Host "[1] Current time: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Yellow
    Write-Host "[2] Scheduled task runs every 15 minutes" -ForegroundColor Yellow
    Write-Host ""

    # Get current log size
    $initialSize = (Get-Item $logFile).Length
    Write-Host "[3] Waiting for next sync run..." -ForegroundColor Cyan
    Write-Host "    (watching log file for new entries)" -ForegroundColor Gray
    Write-Host ""

    # Wait for new log entries (max 16 minutes)
    $timeout = 960  # 16 minutes in seconds
    $elapsed = 0
    $newContent = $null

    while ($elapsed -lt $timeout) {
        Start-Sleep -Seconds 10
        $elapsed += 10

        $currentSize = (Get-Item $logFile).Length

        if ($currentSize -gt $initialSize) {
            # New content detected
            Write-Host "[OK] New sync activity detected!" -ForegroundColor Green
            Start-Sleep -Seconds 30  # Wait for sync to complete
            break
        }

        # Show progress
        $remaining = [math]::Round(($timeout - $elapsed) / 60, 1)
        Write-Host "    Waiting... ($remaining minutes until timeout)" -ForegroundColor Gray
    }

    if ($currentSize -eq $initialSize) {
        Write-Host "[WARNING] No new sync activity within timeout period" -ForegroundColor Yellow
        Write-Host "Showing last 20 lines of existing log:" -ForegroundColor Gray
        Get-Content $logFile -Tail 20 | ForEach-Object {
            if ($_ -match "ERROR|error") {
                Write-Host "  $_" -ForegroundColor Red
            } else {
                Write-Host "  $_" -ForegroundColor Gray
            }
        }
        return
    }

    Write-Host ""
    Write-Host "[4] Analyzing new log entries" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # Get all content and extract the new portion
    $allContent = Get-Content $logFile -Raw
    $newBytes = $currentSize - $initialSize
    $newContent = $allContent.Substring([math]::Max(0, $allContent.Length - $newBytes - 1000))

    # Show new log entries
    $newContent -split "`n" | Select-Object -Last 50 | ForEach-Object {
        if ($_ -match "SCP ERROR|ERROR.*push|ERROR.*pull") {
            Write-Host "  $_" -ForegroundColor Red
        } elseif ($_ -match "Pushed:|Pulled:") {
            Write-Host "  $_" -ForegroundColor Green
        } elseif ($_ -match "Starting sync|sync complete") {
            Write-Host "  $_" -ForegroundColor Cyan
        } else {
            Write-Host "  $_" -ForegroundColor Gray
        }
    }

    Write-Host ""
    Write-Host "[5] Error summary" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $scpErrors = $newContent -split "`n" | Select-String -Pattern "SCP ERROR"

    if ($scpErrors) {
        Write-Host "[FOUND] SCP errors in this sync run:" -ForegroundColor Red
        Write-Host ""
        $scpErrors | ForEach-Object {
            Write-Host "  $_" -ForegroundColor Red
        }
    } else {
        Write-Host "[SUCCESS] No SCP errors found in this sync run!" -ForegroundColor Green
        Write-Host "The known_hosts path fix appears to be working." -ForegroundColor Green
    }
}

Write-Host ""
Write-Host "=== Monitoring Complete ===" -ForegroundColor Cyan
