# Script de démarrage pour les tests d'intégration mobile-backend
# Vérifie tous les prérequis et guide l'utilisateur

Write-Host "=== Démarrage Tests d'Intégration Finance Workflow ===" -ForegroundColor Cyan
Write-Host ""

$allGood = $true

# 1. Vérifier Quarkus
Write-Host "[1/3] Vérification Backend Quarkus..." -ForegroundColor Yellow
try {
    $quarkusHealth = Invoke-RestMethod -Uri 'http://localhost:8085/q/health' -Method Get
    Write-Host "  [OK] Quarkus actif sur port 8085" -ForegroundColor Green
} catch {
    Write-Host "  [KO] Quarkus non accessible" -ForegroundColor Red
    Write-Host "      Démarrer avec:" -ForegroundColor Yellow
    Write-Host "      cd unionflow/unionflow-server-impl-quarkus" -ForegroundColor Gray
    Write-Host "      mvn compile quarkus:dev -D`"quarkus.http.port=8085`"" -ForegroundColor Gray
    $allGood = $false
}

# 2. Vérifier Keycloak
Write-Host "`n[2/3] Vérification Keycloak..." -ForegroundColor Yellow
try {
    $keycloakTest = Invoke-WebRequest -Uri 'http://localhost:8180' -UseBasicParsing -Method Get
    Write-Host "  [OK] Keycloak actif sur port 8180" -ForegroundColor Green

    # Vérifier realm unionflow
    $tokenResponse = Invoke-RestMethod -Method Post `
        -Uri 'http://localhost:8180/realms/master/protocol/openid-connect/token' `
        -ContentType 'application/x-www-form-urlencoded' `
        -Body 'username=admin&password=admin&grant_type=password&client_id=admin-cli'

    $token = $tokenResponse.access_token
    $realms = Invoke-RestMethod -Method Get `
        -Uri 'http://localhost:8180/admin/realms' `
        -Headers @{ Authorization = "Bearer $token" }

    $unionflow = $realms | Where-Object { $_.realm -eq 'unionflow' }

    if ($unionflow) {
        Write-Host "  [OK] Realm 'unionflow' existe" -ForegroundColor Green
    } else {
        Write-Host "  [KO] Realm 'unionflow' manquant" -ForegroundColor Red
        $allGood = $false
    }
} catch {
    Write-Host "  [KO] Keycloak non accessible" -ForegroundColor Red
    Write-Host "      Démarrer avec:" -ForegroundColor Yellow
    Write-Host "      cd unionflow" -ForegroundColor Gray
    Write-Host "      docker-compose up -d keycloak" -ForegroundColor Gray
    $allGood = $false
}

# 3. Vérifier PostgreSQL
Write-Host "`n[3/3] Vérification PostgreSQL..." -ForegroundColor Yellow
try {
    $pgTest = Test-NetConnection -ComputerName localhost -Port 5432 -WarningAction SilentlyContinue
    if ($pgTest.TcpTestSucceeded) {
        Write-Host "  [OK] PostgreSQL actif sur port 5432" -ForegroundColor Green
    } else {
        Write-Host "  [KO] PostgreSQL non accessible" -ForegroundColor Red
        Write-Host "      Démarrer avec:" -ForegroundColor Yellow
        Write-Host "      cd unionflow" -ForegroundColor Gray
        Write-Host "      docker-compose up -d postgres" -ForegroundColor Gray
        $allGood = $false
    }
} catch {
    Write-Host "  [KO] Impossible de vérifier PostgreSQL" -ForegroundColor Red
    $allGood = $false
}

# Résumé
Write-Host "`n=== RÉSUMÉ ===" -ForegroundColor Cyan

if ($allGood) {
    Write-Host "[OK] Tous les services sont prêts !" -ForegroundColor Green
    Write-Host ""
    Write-Host "Prochaine étape:" -ForegroundColor Yellow
    Write-Host "  1. Lancer l'app mobile:" -ForegroundColor Gray
    Write-Host "     cd unionflow/unionflow-mobile-apps" -ForegroundColor Gray
    Write-Host "     flutter run --dart-define=ENV=dev" -ForegroundColor Gray
    Write-Host ""
    Write-Host "  2. Consulter le guide de test:" -ForegroundColor Gray
    Write-Host "     unionflow-mobile-apps/docs/TESTS_INTEGRATION_FINANCE_WORKFLOW.md" -ForegroundColor Gray
    Write-Host ""
} else {
    Write-Host "[KO] Certains services ne sont pas prêts" -ForegroundColor Red
    Write-Host "Corriger les problèmes ci-dessus avant de lancer les tests" -ForegroundColor Yellow
}
