# Fix the known_hosts path issue in Sync-FromNAS.ps1 (no interactive test)
$password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("INTRANET\sysadmin", $password)

Write-Host "=== Fixing Known Hosts Path ===" -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] Creating backup" -ForegroundColor Yellow
    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
    Copy-Item $scriptPath "$scriptPath.backup-$timestamp"
    Write-Host "[OK] Backup created: Sync-FromNAS.ps1.backup-$timestamp" -ForegroundColor Green

    Write-Host ""
    Write-Host "[2] Updating SCP commands with absolute path" -ForegroundColor Yellow

    $content = Get-Content $scriptPath
    $updated = $false

    for ($i = 0; $i -lt $content.Count; $i++) {
        # Look for SCP commands with UserKnownHostsFile parameter
        if ($content[$i] -match 'UserKnownHostsFile="\$SCRIPTS_DIR\\.ssh\\known_hosts"') {
            # Replace with absolute path
            $content[$i] = $content[$i] -replace 'UserKnownHostsFile="\$SCRIPTS_DIR\\.ssh\\known_hosts"', 'UserKnownHostsFile="C:\Shares\test\scripts\.ssh\known_hosts"'
            Write-Host "[UPDATED] Line $($i+1): Changed to absolute path" -ForegroundColor Green
            $updated = $true
        }
    }

    if ($updated) {
        $content | Out-File -FilePath $scriptPath -Encoding UTF8 -Force
        Write-Host "[OK] Script updated successfully" -ForegroundColor Green
    } else {
        Write-Host "[INFO] No changes needed - path already absolute" -ForegroundColor Yellow
    }

    Write-Host ""
    Write-Host "[3] Ensuring .ssh directory exists" -ForegroundColor Yellow
    $sshDir = "C:\Shares\test\scripts\.ssh"
    if (-not (Test-Path $sshDir)) {
        New-Item -Path $sshDir -ItemType Directory -Force | Out-Null
        Write-Host "[OK] Created: $sshDir" -ForegroundColor Green
    } else {
        Write-Host "[OK] Directory exists: $sshDir" -ForegroundColor Green
    }

    Write-Host ""
    Write-Host "[4] Checking known_hosts file" -ForegroundColor Yellow
    $knownHostsPath = "C:\Shares\test\scripts\.ssh\known_hosts"

    if (Test-Path $knownHostsPath) {
        $keyCount = (Get-Content $knownHostsPath | Measure-Object -Line).Lines
        Write-Host "[OK] Exists with $keyCount host key(s)" -ForegroundColor Green
    } else {
        # Create empty file - StrictHostKeyChecking=accept-new will add keys on first connection
        New-Item -Path $knownHostsPath -ItemType File -Force | Out-Null
        Write-Host "[OK] Created empty known_hosts file" -ForegroundColor Green
    }

    Write-Host ""
    Write-Host "[5] Verification - checking updated script" -ForegroundColor Yellow
    $updatedContent = Get-Content $scriptPath -Raw

    if ($updatedContent -match 'UserKnownHostsFile="C:\\Shares\\test\\scripts\\.ssh\\known_hosts"') {
        Write-Host "[SUCCESS] Absolute path is now in the script" -ForegroundColor Green
    } else {
        Write-Host "[WARNING] Could not verify path update" -ForegroundColor Yellow
    }
}

Write-Host ""
Write-Host "=== Fix Complete ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "The sync script will automatically accept the NAS host key" -ForegroundColor Cyan
Write-Host "on the next run (every 15 minutes via scheduled task)." -ForegroundColor Cyan
