# =============================================================================
# WEBMoney Backend - Script de Deploy para Windows
# =============================================================================
# Este script sincroniza e deploya o backend Laravel para o servidor
# Uso: .\deploy.ps1
# Requer: PuTTY (pscp, plink) instalados no PATH
# =============================================================================

$ErrorActionPreference = "Stop"

# Configuracoes
$SERVER_USER = "root"
$SERVER_HOST = "213.165.93.60"
$SERVER_PASS = "Master9354"
$SERVER_PATH = "/var/www/webmoney/backend"
$LOCAL_PATH = $PSScriptRoot

# Cores
function Write-Color {
    param([string]$Text, [string]$Color = "White")
    Write-Host $Text -ForegroundColor $Color
}

Write-Host ""
Write-Color "========================================" "Cyan"
Write-Color "   WEBMoney Backend - Deploy Script    " "Cyan"
Write-Color "========================================" "Cyan"
Write-Host ""

# 1. Sincronizar pastas principais
Write-Color "[1/5] Enviando pastas do backend..." "Yellow"

$folders = @("app", "bootstrap", "config", "database", "public", "resources", "routes")

foreach ($folder in $folders) {
    if (Test-Path "$LOCAL_PATH\$folder") {
        Write-Host "  -> $folder"
        pscp -r -batch -pw $SERVER_PASS "$LOCAL_PATH\$folder" "${SERVER_USER}@${SERVER_HOST}:${SERVER_PATH}/"
    }
}

Write-Color "Pastas enviadas" "Green"
Write-Host ""

# 2. Enviar arquivos principais
Write-Color "[2/5] Enviando arquivos..." "Yellow"

$files = @("artisan", "composer.json", "composer.lock")

foreach ($file in $files) {
    if (Test-Path "$LOCAL_PATH\$file") {
        Write-Host "  -> $file"
        pscp -batch -pw $SERVER_PASS "$LOCAL_PATH\$file" "${SERVER_USER}@${SERVER_HOST}:${SERVER_PATH}/"
    }
}

Write-Color "Arquivos enviados" "Green"
Write-Host ""

# 3. Instalar dependencias e configurar
Write-Color "[3/5] Instalando dependencias no servidor..." "Yellow"
plink -batch -pw $SERVER_PASS "$SERVER_USER@$SERVER_HOST" "cd $SERVER_PATH; composer install --no-dev --optimize-autoloader 2>&1 | tail -5"

Write-Color "Dependencias instaladas" "Green"
Write-Host ""

# 4. Migracoes e otimizacoes
Write-Color "[4/5] Executando migracoes e otimizacoes..." "Yellow"
plink -batch -pw $SERVER_PASS "$SERVER_USER@$SERVER_HOST" "cd $SERVER_PATH; php artisan migrate --force; php artisan optimize:clear; php artisan config:cache; php artisan route:cache; php artisan view:cache"

Write-Color "Migracoes e caches OK" "Green"
Write-Host ""

# 5. Permissoes e reiniciar PHP-FPM (CRITICO para evitar cache de opcodes)
Write-Color "[5/5] Ajustando permissoes e reiniciando PHP-FPM..." "Yellow"
plink -batch -pw $SERVER_PASS "$SERVER_USER@$SERVER_HOST" "chown -R www-data:www-data $SERVER_PATH/storage $SERVER_PATH/bootstrap/cache; chmod -R 775 $SERVER_PATH/storage $SERVER_PATH/bootstrap/cache; systemctl restart php8.4-fpm"

Write-Color "Permissoes e PHP-FPM OK" "Green"
Write-Host ""

Write-Color "========================================" "Green"
Write-Color "   Deploy concluido com sucesso!       " "Green"
Write-Color "========================================" "Green"
Write-Host ""
Write-Host "API disponivel em: https://webmoney.cnxifly.com/api"
Write-Host ""
