# =============================================================================
# WEBMoney - Quick Deploy (Conforme Diretrizes v3.0)
# =============================================================================
# AVISO: Este script faz deploy SEM commit
# Use apenas para iteracoes rapidas durante desenvolvimento
# Para release oficial, use SEMPRE: .\scripts\release.ps1
# =============================================================================

param(
    [Parameter(Mandatory=$true)]
    [ValidateSet('frontend', 'backend', 'both')]
    [string]$Target
)

$ErrorActionPreference = "Stop"
$ProjectRoot = "c:\Users\marco\OneDrive\Documents\WebMoney"
$Server = "root@213.165.93.60"
$ProductionUrl = "https://webmoney.cnxifly.com"

Write-Host ""
Write-Host "=======================================================" -ForegroundColor Yellow
Write-Host "  QUICK DEPLOY - Apenas para desenvolvimento" -ForegroundColor Yellow
Write-Host "=======================================================" -ForegroundColor Yellow
Write-Host ""
Write-Host "  AVISO: Este deploy NAO incrementa versao nem faz commit" -ForegroundColor Red
Write-Host "  Para release oficial use: .\scripts\release.ps1" -ForegroundColor Red
Write-Host ""

# Validar SSH
Write-Host "[1/3] Verificando conexao SSH..." -ForegroundColor Gray
$sshTest = ssh -o ConnectTimeout=5 -o BatchMode=yes $Server "echo OK" 2>$null
if ($sshTest -ne "OK") {
    Write-Host "[ERRO] SSH falhou. Execute: .\scripts\setup-ssh.ps1" -ForegroundColor Red
    exit 1
}
Write-Host "[OK] SSH conectado" -ForegroundColor Green

$startTime = Get-Date

# Frontend
if ($Target -eq 'frontend' -or $Target -eq 'both') {
    Write-Host "[2/3] Deploy Frontend..." -ForegroundColor Yellow
    
    Push-Location (Join-Path $ProjectRoot "frontend")
    npm run build 2>&1 | Out-Null
    ssh $Server "rm -rf /var/www/webmoney/frontend/dist/*" 2>&1 | Out-Null
    scp -r ".\dist\*" "${Server}:/var/www/webmoney/frontend/dist/" 2>&1 | Out-Null
    Pop-Location
    
    Write-Host "[OK] Frontend deployed" -ForegroundColor Green
}

# Backend
if ($Target -eq 'backend' -or $Target -eq 'both') {
    Write-Host "[2/3] Deploy Backend..." -ForegroundColor Yellow
    
    Push-Location (Join-Path $ProjectRoot "backend")
    $tar = "$env:TEMP\wm-backend.tar.gz"
    tar -czf $tar --exclude='.git' --exclude='node_modules' --exclude='vendor' --exclude='storage/logs/*' --exclude='.env' . 2>&1 | Out-Null
    scp $tar "${Server}:/tmp/wm-backend.tar.gz" 2>&1 | Out-Null
    ssh $Server "cd /var/www/webmoney/backend && tar -xzf /tmp/wm-backend.tar.gz && rm /tmp/wm-backend.tar.gz && composer install --no-dev -q && php artisan config:cache && systemctl restart php8.4-fpm" 2>&1 | Out-Null
    Remove-Item $tar -ErrorAction SilentlyContinue
    Pop-Location
    
    Write-Host "[OK] Backend deployed" -ForegroundColor Green
}

# Teste obrigatorio
Write-Host "[3/3] Abrindo para teste..." -ForegroundColor Yellow
Start-Process $ProductionUrl

$elapsed = ((Get-Date) - $startTime).TotalSeconds

Write-Host ""
Write-Host "=======================================================" -ForegroundColor Green
Write-Host "  Deploy em $([math]::Round($elapsed, 1))s - $ProductionUrl" -ForegroundColor Green
Write-Host "=======================================================" -ForegroundColor Green
Write-Host ""
Write-Host "  LEMBRE-SE (Diretriz v3.0):" -ForegroundColor Yellow
Write-Host "  Apos finalizar desenvolvimento, faca release oficial:" -ForegroundColor Gray
Write-Host "  .\scripts\release.ps1 -VersionType patch -ChangeDescription '...'" -ForegroundColor White
Write-Host ""
