# Verify the error logging is working and check where generic errors come from
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    $scriptPath = "C:\Shares\test\scripts\Sync-FromNAS.ps1"
    $content = Get-Content $scriptPath

    Write-Host "=== Verification Check ===" -ForegroundColor Cyan
    Write-Host ""

    Write-Host "[1] Copy-ToNAS function (lines 82-97)" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray
    for ($i = 81; $i -le 96; $i++) {
        if ($content[$i] -match 'SCP PUSH ERROR') {
            Write-Host "$($i+1): $($content[$i])" -ForegroundColor Green
        } else {
            Write-Host "$($i+1): $($content[$i])" -ForegroundColor Gray
        }
    }

    Write-Host ""
    Write-Host "[2] Generic error logging locations" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $genericErrorLines = @()
    for ($i = 0; $i -lt $content.Count; $i++) {
        if ($content[$i] -match 'Write-Log.*ERROR: Failed to push') {
            $genericErrorLines += $i
            Write-Host "Line $($i+1): $($content[$i])" -ForegroundColor Red
        }
    }

    Write-Host ""
    Write-Host "[3] Analysis" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    if ($genericErrorLines.Count -gt 0) {
        Write-Host "[FOUND] $($genericErrorLines.Count) generic error logging location(s)" -ForegroundColor Yellow
        Write-Host ""
        Write-Host "These generic errors are logged by the CALLING CODE," -ForegroundColor Yellow
        Write-Host "which happens AFTER Copy-ToNAS returns false." -ForegroundColor Yellow
        Write-Host ""
        Write-Host "The detailed SCP errors should appear BEFORE the generic errors." -ForegroundColor Cyan
        Write-Host "Example expected log sequence:" -ForegroundColor Gray
        Write-Host "  SCP PUSH ERROR (exit 1): scp: /data/test/path: Permission denied" -ForegroundColor Red
        Write-Host "  ERROR: Failed to push filename.dat" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[4] Recent error examples from log" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $logFile = "C:\Shares\test\scripts\sync-from-nas.log"
    $recentErrors = Get-Content $logFile -Tail 1000 | Where-Object {
        $_ -match "Failed to push.*hvin\.dat" -or $_ -match "Failed to push.*hvsort\.dat" -or $_ -match "SCP PUSH ERROR"
    }

    if ($recentErrors) {
        $recentErrors | Select-Object -Last 10 | ForEach-Object {
            if ($_ -match "SCP PUSH ERROR") {
                Write-Host $_ -ForegroundColor Green
            } else {
                Write-Host $_ -ForegroundColor Red
            }
        }
    } else {
        Write-Host "[INFO] No relevant errors found in recent log" -ForegroundColor Yellow
    }
}

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