# Deploy Database Performance Optimizations to AD2 (SMB only)
$password = ConvertTo-SecureString 'Paper123!@#' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential('INTRANET\sysadmin', $password)

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Test Database Performance Optimization" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan

# Step 1: Mount AD2 share
Write-Host "[1/4] Mounting AD2 C$ share..." -ForegroundColor Green
New-PSDrive -Name AD2 -PSProvider FileSystem -Root "\\192.168.0.6\C$" -Credential $cred -ErrorAction Stop | Out-Null
Write-Host "  [OK] Share mounted" -ForegroundColor Green

# Step 2: Backup existing api.js
Write-Host "`n[2/4] Backing up existing api.js..." -ForegroundColor Green
$timestamp = Get-Date -Format "yyyy-MM-dd-HHmmss"
$backupPath = "AD2:\Shares\testdatadb\routes\api.js.backup-$timestamp"
Copy-Item "AD2:\Shares\testdatadb\routes\api.js" $backupPath
Write-Host "  [OK] Backup created: api.js.backup-$timestamp" -ForegroundColor Green

# Step 3: Deploy optimized api.js
Write-Host "`n[3/4] Deploying optimized api.js..." -ForegroundColor Green
$optimizedContent = Get-Content "D:\ClaudeTools\api-js-optimized.js" -Raw
$optimizedContent | Set-Content "AD2:\Shares\testdatadb\routes\api.js" -Encoding UTF8
Write-Host "  [OK] Optimized api.js deployed" -ForegroundColor Green

# Step 4: Verify deployment
Write-Host "`n[4/4] Verifying deployment..." -ForegroundColor Green
$deployedFile = Get-Item "AD2:\Shares\testdatadb\routes\api.js"
Write-Host "  [OK] File size: $($deployedFile.Length) bytes" -ForegroundColor Green
Write-Host "  [OK] Modified: $($deployedFile.LastWriteTime)" -ForegroundColor Green

# Cleanup
Remove-PSDrive -Name AD2 -ErrorAction SilentlyContinue

Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "Deployment Complete" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "[OK] Backup created" -ForegroundColor Green
Write-Host "[OK] Optimized code deployed" -ForegroundColor Green
Write-Host "`nOptimizations Applied:" -ForegroundColor Cyan
Write-Host "  - Connection timeout: 10 seconds" -ForegroundColor Cyan
Write-Host "  - WAL mode: Enabled (better concurrency)" -ForegroundColor Cyan
Write-Host "  - Cache size: 64MB" -ForegroundColor Cyan
Write-Host "  - Memory-mapped I/O: 256MB" -ForegroundColor Cyan
Write-Host "  - Synchronous mode: NORMAL (faster, safe)" -ForegroundColor Cyan
Write-Host "`n[ACTION REQUIRED] Restart Node.js Server:" -ForegroundColor Yellow
Write-Host "  1. Connect to AD2 (SSH or RDP)" -ForegroundColor Yellow
Write-Host "  2. Stop existing Node.js process:" -ForegroundColor Yellow
Write-Host "     taskkill /F /IM node.exe" -ForegroundColor Cyan
Write-Host "  3. Start server:" -ForegroundColor Yellow
Write-Host "     cd C:\Shares\testdatadb" -ForegroundColor Cyan
Write-Host "     node server.js" -ForegroundColor Cyan
Write-Host "`nWeb Interface: http://192.168.0.6:3000" -ForegroundColor Green
Write-Host "`nRollback (if needed):" -ForegroundColor Yellow
Write-Host "  Copy-Item C:\Shares\testdatadb\routes\api.js.backup-$timestamp C:\Shares\testdatadb\routes\api.js" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan
