Write-Host "Connecting to masterbox..."

try {
    $remoteCommand = @'
Write-Host "Checking current network connections..."
net use

Write-Host "`nDisconnecting from NEXUS shares..."
# First disconnect from specific NEXUS shares
net use \\NEXUS\amazon /delete /y 2>$null
net use \\NEXUS\flamenco /delete /y 2>$null
net use \\NAS\amazon /delete /y 2>$null
# Then disconnect any mapped drives
net use A: /delete /y 2>$null
net use F: /delete /y 2>$null
net use N: /delete /y 2>$null
Write-Host "Existing NEXUS connections cleared."

$response = Read-Host "`nDo you want to stop any existing Flamenco workers? (y/n)"
if ($response -eq 'y') {
    taskkill /IM flamenco-worker.exe /F 2>$null
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Worker process terminated."
    } else {
        Write-Host "No worker process found."
    }
}

Write-Host "`nMapping network drives..."
Write-Host "Connecting drives to NEXUS..."

# First connect to the share without a drive letter
Write-Host "Establishing initial NEXUS connection..."
net use \\NEXUS\amazon /user:Nathan /persistent:yes
if ($LASTEXITCODE -eq 0) {
    Write-Host "Initial NEXUS connection established."
    Start-Sleep -Seconds 2

    Write-Host "`nMapping A: drive..."
    net use A: \\NEXUS\amazon /persistent:yes
    if ($LASTEXITCODE -eq 0) {
        Write-Host "A: drive connected successfully."
        
        Write-Host "`nMapping F: drive..."
        net use F: \\NEXUS\flamenco /persistent:yes
        if ($LASTEXITCODE -eq 0) {
            Write-Host "F: drive connected successfully."
        } else {
            Write-Host "Failed to connect F: drive" -ForegroundColor Red
        }
    } else {
        Write-Host "Failed to connect A: drive" -ForegroundColor Red
    }
} else {
    Write-Host "Failed to establish initial NEXUS connection" -ForegroundColor Red
}

Write-Host "`nMapping N: drive..."
net use N: \\NAS\amazon /user:Nathan /persistent:yes
if ($LASTEXITCODE -eq 0) {
    Write-Host "N: drive connected successfully."
} else {
    Write-Host "Failed to connect N: drive" -ForegroundColor Red
}

Write-Host "`nVerifying connections..."
net use

Write-Host "`nStarting Flamenco worker..."
if (Test-Path "C:\Program Files\Blender Foundation\Flamenco 3.6") {
    Set-Location "C:\Program Files\Blender Foundation\Flamenco 3.6"
    if (Test-Path "flamenco-worker.exe") {
        Write-Host "Running flamenco-worker.exe directly..."
        # Run the executable directly in the console instead of using Start-Process
        .\flamenco-worker.exe
        # This line will only execute after the worker process terminates
        Write-Host "Flamenco worker process has terminated."
    } else {
        Write-Host "Error: flamenco-worker.exe not found in Flamenco directory" -ForegroundColor Red
    }
} else {
    Write-Host "Error: Flamenco directory not found" -ForegroundColor Red
}

Write-Host "`nPress Enter to continue..."
Read-Host
'@

    # Encode the command to handle special characters
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($remoteCommand)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    
    # Execute the encoded command on the remote machine
    ssh -t masterbox "powershell -EncodedCommand $encodedCommand"

} catch {
    Write-Host "`nAn error occurred:" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
}

Write-Host "`nPress Enter to exit..."
Read-Host 