# Verify the sync script update and run a test sync
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Verifying Sync Script Update ===" -ForegroundColor Cyan
Write-Host ""

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

    Write-Host "[1] Verifying OpenSSH tool paths" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $content = Get-Content $scriptPath -Raw

    if ($content -match '\$SCP\s*=\s*"[^"]*OpenSSH[^"]*scp\.exe"') {
        Write-Host "[OK] SCP path updated to OpenSSH" -ForegroundColor Green
    } else {
        Write-Host "[ERROR] SCP path not found or incorrect" -ForegroundColor Red
    }

    if ($content -match '\$SSH\s*=\s*"[^"]*OpenSSH[^"]*ssh\.exe"') {
        Write-Host "[OK] SSH path updated to OpenSSH" -ForegroundColor Green
    } else {
        Write-Host "[ERROR] SSH path not found or incorrect" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[2] Verifying Copy-ToNAS function" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    if ($content -match "SCP PUSH ERROR") {
        Write-Host "[OK] Copy-ToNAS has error logging" -ForegroundColor Green
    } else {
        Write-Host "[WARNING] Error logging may not be present" -ForegroundColor Yellow
    }

    if ($content -match "StrictHostKeyChecking=accept-new") {
        Write-Host "[OK] Auto host key acceptance configured" -ForegroundColor Green
    } else {
        Write-Host "[WARNING] Host key acceptance may not be configured" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[3] Running manual sync test" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray
    Write-Host "Triggering a manual sync run to test OpenSSH and capture errors..." -ForegroundColor White
    Write-Host ""

    # Run the sync script
    try {
        & powershell.exe -ExecutionPolicy Bypass -File $scriptPath *>&1 | Tee-Object -Variable syncOutput

        Write-Host ""
        Write-Host "[4] Checking sync log for detailed errors" -ForegroundColor Yellow
        Write-Host "=" * 80 -ForegroundColor Gray

        $logFile = "C:\Shares\test\scripts\sync-from-nas.log"
        $recentErrors = Get-Content $logFile -Tail 30 | Select-String -Pattern "SCP.*ERROR|ERROR.*push|ERROR.*pull"

        if ($recentErrors) {
            Write-Host "Found detailed error messages:" -ForegroundColor Cyan
            $recentErrors | ForEach-Object {
                Write-Host "  $_" -ForegroundColor Red
            }
        } else {
            Write-Host "No detailed SCP errors found in recent log" -ForegroundColor Yellow
            Write-Host "Showing last 15 lines of log:" -ForegroundColor Gray
            Get-Content $logFile -Tail 15 | ForEach-Object {
                Write-Host "  $_" -ForegroundColor Gray
            }
        }

    } catch {
        Write-Host "[ERROR] Sync script failed to run: $($_.Exception.Message)" -ForegroundColor Red
    }
}

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