# Test the updated sync script
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Testing Updated Sync Script ===" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1] Running sync script manually..." -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray
    Write-Host ""

    # Clear recent errors from log
    $logFile = "C:\Shares\test\scripts\sync-from-nas.log"
    Write-Host "Note: Watching $logFile for new errors..." -ForegroundColor Gray
    Write-Host ""

    # Get current log size
    $logSize = (Get-Item $logFile).Length

    # Run sync (in background to avoid blocking)
    $job = Start-Job -ScriptBlock {
        & powershell.exe -ExecutionPolicy Bypass -File "C:\Shares\test\scripts\Sync-FromNAS.ps1"
    }

    # Wait a bit for it to start
    Start-Sleep -Seconds 10

    # Check for new log entries
    $newContent = Get-Content $logFile -Raw
    $newLines = $newContent.Substring([Math]::Min($logSize, $newContent.Length))

    Write-Host "[2] Recent log output:" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $newLines -split "`n" | Select-Object -Last 50 | ForEach-Object {
        if ($_ -match "ERROR|error") {
            Write-Host $_ -ForegroundColor Red
        } elseif ($_ -match "WARNING|warning") {
            Write-Host $_ -ForegroundColor Yellow
        } elseif ($_ -match "SCP|scp") {
            Write-Host $_ -ForegroundColor Cyan
        } else {
            Write-Host $_ -ForegroundColor Gray
        }
    }

    # Stop the job
    Stop-Job -Job $job -ErrorAction SilentlyContinue
    Remove-Job -Job $job -ErrorAction SilentlyContinue

    Write-Host ""
    Write-Host "[3] Checking if detailed SCP errors are logged" -ForegroundColor Yellow
    Write-Host "=" * 80 -ForegroundColor Gray

    $scpErrors = Get-Content $logFile -Tail 100 | Select-String -Pattern "SCP ERROR"

    if ($scpErrors) {
        Write-Host "[SUCCESS] Found detailed SCP error logging!" -ForegroundColor Green
        Write-Host ""
        Write-Host "Sample SCP errors:" -ForegroundColor Cyan
        $scpErrors | Select-Object -First 5 | ForEach-Object {
            Write-Host "  $_" -ForegroundColor Red
        }
    } else {
        Write-Host "[INFO] No 'SCP ERROR' entries found yet" -ForegroundColor Yellow
        Write-Host "This could mean:" -ForegroundColor Gray
        Write-Host "  - No errors occurred (good!)" -ForegroundColor Gray
        Write-Host "  - Errors happened but weren't logged yet" -ForegroundColor Gray
        Write-Host "  - Need to wait for next scheduled run" -ForegroundColor Gray
    }
}

Write-Host ""
Write-Host "=== Test Complete ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "Note: The script runs every 15 minutes via scheduled task." -ForegroundColor Yellow
Write-Host "Check the log again in a few minutes for full sync results." -ForegroundColor Yellow
