# Get detailed sync error messages
Write-Host "=== Analyzing Sync Error Details ===" -ForegroundColor Cyan
Write-Host ""

$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 {
    $logFile = "C:\Shares\test\scripts\sync-from-nas.log"

    Write-Host "Looking for detailed error messages..." -ForegroundColor Yellow
    Write-Host ""

    # Get context around errors (lines before and after ERROR lines)
    $content = Get-Content $logFile -Tail 2000

    Write-Host "[1] Error Context (last 20 errors with surrounding lines)" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray
    Write-Host ""

    $errorIndices = @()
    for ($i = 0; $i -lt $content.Count; $i++) {
        if ($content[$i] -match "ERROR:") {
            $errorIndices += $i
        }
    }

    # Show last 20 errors with context
    $errorIndices | Select-Object -Last 20 | ForEach-Object {
        $index = $_

        # Show 2 lines before, the error, and 2 lines after
        $start = [Math]::Max(0, $index - 2)
        $end = [Math]::Min($content.Count - 1, $index + 2)

        for ($i = $start; $i -le $end; $i++) {
            if ($i -eq $index) {
                Write-Host ">>> $($content[$i])" -ForegroundColor Red
            } else {
                Write-Host "    $($content[$i])" -ForegroundColor Gray
            }
        }
        Write-Host ""
    }

    Write-Host ""
    Write-Host "[2] Checking for specific error messages" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # Look for common error patterns in more detail
    $sshErrors = $content | Select-String -Pattern "ssh|plink|pscp" -Context 0,1
    $permErrors = $content | Select-String -Pattern "denied|permission" -Context 0,1
    $fileErrors = $content | Select-String -Pattern "not found|no such|cannot find" -Context 0,1
    $networkErrors = $content | Select-String -Pattern "timeout|connection|network" -Context 0,1

    if ($sshErrors) {
        Write-Host "SSH/Connection Errors:" -ForegroundColor Red
        $sshErrors | Select-Object -First 5 | ForEach-Object {
            Write-Host "  $($_.Line)" -ForegroundColor Red
        }
        Write-Host ""
    }

    if ($permErrors) {
        Write-Host "Permission Errors:" -ForegroundColor Red
        $permErrors | Select-Object -First 5 | ForEach-Object {
            Write-Host "  $($_.Line)" -ForegroundColor Red
        }
        Write-Host ""
    }

    if ($fileErrors) {
        Write-Host "File Not Found Errors:" -ForegroundColor Red
        $fileErrors | Select-Object -First 5 | ForEach-Object {
            Write-Host "  $($_.Line)" -ForegroundColor Red
        }
        Write-Host ""
    }

    if ($networkErrors) {
        Write-Host "Network/Timeout Errors:" -ForegroundColor Red
        $networkErrors | Select-Object -First 5 | ForEach-Object {
            Write-Host "  $($_.Line)" -ForegroundColor Red
        }
        Write-Host ""
    }

    Write-Host ""
    Write-Host "[3] Checking one of the failing files" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    # Check if one of the failing files actually exists
    $testFile = "C:\Shares\test\TS-11L\ProdSW\HVDATA\hvin.dat"
    if (Test-Path $testFile) {
        $file = Get-Item $testFile
        Write-Host "Sample failing file EXISTS on AD2:" -ForegroundColor Green
        Write-Host "  Path: $($file.FullName)" -ForegroundColor White
        Write-Host "  Size: $($file.Length) bytes" -ForegroundColor White
        Write-Host "  Modified: $($file.LastWriteTime)" -ForegroundColor White
        Write-Host ""
        Write-Host "This suggests the issue is with the PUSH to NAS, not the source file." -ForegroundColor Yellow
    } else {
        Write-Host "Sample file does NOT exist: $testFile" -ForegroundColor Red
    }
}
