# Script de redémarrage des applications lions-user-manager en mode DEV
# Usage: .\restart-dev.ps1

Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "Redémarrage lions-user-manager - Mode DEV" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host ""

# Vérifier que Keycloak est accessible
Write-Host "[1/4] Vérification de Keycloak..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost:8180" -Method GET -UseBasicParsing -TimeoutSec 3 -ErrorAction Stop
    Write-Host "✅ Keycloak est accessible sur http://localhost:8180" -ForegroundColor Green
} catch {
    Write-Host "⚠️  Keycloak n'est pas accessible sur http://localhost:8180" -ForegroundColor Yellow
    Write-Host "   Le backend ne pourra pas récupérer les utilisateurs." -ForegroundColor Yellow
    Write-Host ""
    $continue = Read-Host "Continuer quand même ? (o/N)"
    if ($continue -ne "o" -and $continue -ne "O") {
        Write-Host "Arrêt du script." -ForegroundColor Red
        exit 1
    }
}
Write-Host ""

# Arrêter les processus Java existants (Quarkus)
Write-Host "[2/4] Arrêt des processus Quarkus existants..." -ForegroundColor Yellow
$quarkusProcesses = Get-Process -Name "java" -ErrorAction SilentlyContinue | Where-Object {
    $_.CommandLine -like "*quarkus*" -or $_.CommandLine -like "*lions-user-manager*"
}

if ($quarkusProcesses) {
    Write-Host "   Arrêt de $($quarkusProcesses.Count) processus Quarkus..." -ForegroundColor Gray
    $quarkusProcesses | Stop-Process -Force
    Start-Sleep -Seconds 2
    Write-Host "✅ Processus arrêtés" -ForegroundColor Green
} else {
    Write-Host "   Aucun processus Quarkus en cours d'exécution" -ForegroundColor Gray
}
Write-Host ""

# Compilation des modules
Write-Host "[3/4] Compilation des modules..." -ForegroundColor Yellow
Write-Host ""

Write-Host "   → Compilation du serveur..." -ForegroundColor Gray
Set-Location "lions-user-manager-server-impl-quarkus"
$compileServer = & mvn compile -DskipTests -q 2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "   ✅ Serveur compilé" -ForegroundColor Green
} else {
    Write-Host "   ❌ Erreur compilation serveur" -ForegroundColor Red
    Write-Host $compileServer
}
Set-Location ".."

Write-Host "   → Compilation du client..." -ForegroundColor Gray
Set-Location "lions-user-manager-client-quarkus-primefaces-freya"
$compileClient = & mvn compile -DskipTests -q 2>&1
if ($LASTEXITCODE -eq 0) {
    Write-Host "   ✅ Client compilé" -ForegroundColor Green
} else {
    Write-Host "   ❌ Erreur compilation client" -ForegroundColor Red
    Write-Host $compileClient
}
Set-Location ".."

Write-Host ""

# Instructions pour démarrage
Write-Host "[4/4] Prêt à démarrer" -ForegroundColor Yellow
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "Pour démarrer les applications:" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Terminal 1 (Backend):" -ForegroundColor White
Write-Host "  cd lions-user-manager-server-impl-quarkus" -ForegroundColor Gray
Write-Host "  mvn quarkus:dev" -ForegroundColor Gray
Write-Host ""
Write-Host "Terminal 2 (Frontend):" -ForegroundColor White
Write-Host "  cd lions-user-manager-client-quarkus-primefaces-freya" -ForegroundColor Gray
Write-Host "  mvn quarkus:dev" -ForegroundColor Gray
Write-Host ""
Write-Host "Accès:" -ForegroundColor White
Write-Host "  Frontend:  http://localhost:8082" -ForegroundColor Gray
Write-Host "  Backend:   http://localhost:8081" -ForegroundColor Gray
Write-Host "  Keycloak:  http://localhost:8180" -ForegroundColor Gray
Write-Host ""
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "Corrections appliquées:" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "✅ Timeout REST Client: 30s → 90s" -ForegroundColor Green
Write-Host "✅ Keycloak Admin realm: lions-user-manager → master" -ForegroundColor Green
Write-Host "✅ Timeout Keycloak Admin: 30s → 60s" -ForegroundColor Green
Write-Host "✅ MyFaces vues en session: 50 → 100" -ForegroundColor Green
Write-Host "✅ MyFaces timeout client: 1h → 2h" -ForegroundColor Green
Write-Host ""
Write-Host "Consultez CORRECTIONS_TIMEOUT_VIEWEXPIRED.md pour plus de details" -ForegroundColor Cyan
Write-Host ""
