# Script pour vérifier l'état de Keycloak (realm unionflow)
# Usage: .\check-keycloak-state.ps1

Write-Host "=== Vérification Keycloak ===" -ForegroundColor Cyan
Write-Host ""

try {
    # Test connexion Keycloak
    $testUrl = Invoke-WebRequest -Uri 'http://localhost:8180' -UseBasicParsing -Method Get
    Write-Host "[OK] Keycloak accessible" -ForegroundColor Green
} catch {
    Write-Host "[ERREUR] Keycloak non accessible sur http://localhost:8180" -ForegroundColor Red
    Write-Host "Démarrer avec: docker-compose up -d keycloak" -ForegroundColor Yellow
    exit 1
}

# Obtenir le token admin
try {
    $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
    Write-Host "[OK] Authentifié avec succès" -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Host "[ERREUR] Impossible de s'authentifier à Keycloak" -ForegroundColor Red
    Write-Host "Vérifier les credentials admin/admin" -ForegroundColor Yellow
    exit 1
}

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

Write-Host "=== REALMS DISPONIBLES ===" -ForegroundColor Cyan
$realms | ForEach-Object { Write-Host "  - $($_.realm)" }

# Vérifier realm unionflow
$unionflow = $realms | Where-Object { $_.realm -eq 'unionflow' }

if (-not $unionflow) {
    Write-Host "`n[ERREUR] Realm 'unionflow' non trouvé" -ForegroundColor Red
    exit 1
}

Write-Host "`n[OK] Realm 'unionflow' existe" -ForegroundColor Green

# Utilisateurs
$users = Invoke-RestMethod -Method Get `
    -Uri 'http://localhost:8180/admin/realms/unionflow/users' `
    -Headers @{ Authorization = "Bearer $token" }

Write-Host "`n=== UTILISATEURS (total: $($users.Count)) ===" -ForegroundColor Cyan
$users | ForEach-Object {
    Write-Host "  - $($_.username) | $($_.email)"
}

# Clients
$clients = Invoke-RestMethod -Method Get `
    -Uri 'http://localhost:8180/admin/realms/unionflow/clients' `
    -Headers @{ Authorization = "Bearer $token" }

Write-Host "`n=== CLIENTS ===" -ForegroundColor Cyan
$mobileClient = $null
$clients | Where-Object { $_.clientId -like '*unionflow*' } | ForEach-Object {
    Write-Host "  - $($_.clientId)"
    if ($_.clientId -eq 'unionflow-mobile') {
        $mobileClient = $_
    }
}

if ($mobileClient) {
    Write-Host "`n[OK] Client 'unionflow-mobile' existe" -ForegroundColor Green
} else {
    Write-Host "`n[ERREUR] Client 'unionflow-mobile' non trouvé" -ForegroundColor Red
}

Write-Host "`n=== RÉSUMÉ ===" -ForegroundColor Cyan
Write-Host "Realm unionflow: OK" -ForegroundColor Green
Write-Host "Client unionflow-mobile: $(if ($mobileClient) { 'OK' } else { 'MANQUANT' })" -ForegroundColor $(if ($mobileClient) { 'Green' } else { 'Red' })
Write-Host "Utilisateurs: $($users.Count)"
Write-Host ""
Write-Host "Prêt pour les tests d'intégration mobile !" -ForegroundColor Green
