$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "================================================" -ForegroundColor Cyan
Write-Host "Fixing Zombie Sync Task" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host ""

Invoke-Command -ComputerName 192.168.0.6 -Credential $cred -ScriptBlock {
    Write-Host "[1/5] Stopping zombie sync task..." -ForegroundColor Yellow
    try {
        Stop-ScheduledTask -TaskName "Sync-FromNAS" -ErrorAction Stop
        Write-Host "  [OK] Task stopped" -ForegroundColor Green
    } catch {
        Write-Host "  [ERROR] Failed to stop task: $($_.Exception.Message)" -ForegroundColor Red
    }
    Start-Sleep -Seconds 2

    Write-Host ""
    Write-Host "[2/5] Killing any hung PowerShell processes..." -ForegroundColor Yellow
    $hungProcs = Get-Process -Name pwsh,powershell -ErrorAction SilentlyContinue |
        Where-Object { $_.StartTime -lt (Get-Date).AddHours(-1) }
    if ($hungProcs) {
        $hungProcs | ForEach-Object {
            Write-Host "  Killing PID $($_.Id) (started $($_.StartTime))" -ForegroundColor White
            Stop-Process -Id $_.Id -Force
        }
        Write-Host "  [OK] Killed $($hungProcs.Count) hung process(es)" -ForegroundColor Green
    } else {
        Write-Host "  [OK] No hung processes found" -ForegroundColor Green
    }

    Write-Host ""
    Write-Host "[3/5] Testing SSH connectivity to NAS..." -ForegroundColor Yellow
    $sshTest = & ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@192.168.0.9 "hostname" 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "  [OK] SSH connection successful: $sshTest" -ForegroundColor Green
    } else {
        Write-Host "  [ERROR] SSH connection failed: $sshTest" -ForegroundColor Red
        Write-Host "  Attempting to test with password auth..." -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[4/5] Testing NAS find command (what was hanging)..." -ForegroundColor Yellow
    $findTest = & ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no root@192.168.0.9 "find /data/test/TS-4R/LOGS -name '*.DAT' -type f 2>/dev/null | head -5" 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "  [OK] Find command executed successfully" -ForegroundColor Green
        Write-Host "  Sample results: $($findTest -join ', ')" -ForegroundColor Gray
    } else {
        Write-Host "  [ERROR] Find command failed: $findTest" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[5/5] Re-enabling sync task..." -ForegroundColor Yellow
    try {
        Start-ScheduledTask -TaskName "Sync-FromNAS" -ErrorAction Stop
        Write-Host "  [OK] Task re-enabled and started" -ForegroundColor Green
    } catch {
        Write-Host "  [ERROR] Failed to start task: $($_.Exception.Message)" -ForegroundColor Red
    }

    Write-Host ""
    Write-Host "[VERIFICATION] Task status after fix..." -ForegroundColor Yellow
    $task = Get-ScheduledTask -TaskName "Sync-FromNAS"
    $taskInfo = Get-ScheduledTaskInfo -TaskName "Sync-FromNAS"
    Write-Host "  State: $($task.State)" -ForegroundColor White
    Write-Host "  Last Run: $($taskInfo.LastRunTime)" -ForegroundColor White
    Write-Host "  Next Run: $($taskInfo.NextRunTime)" -ForegroundColor White
}

Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host "Fix Complete - Monitor sync log for results" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
