# Test de la configuration Keycloak pour mobile
$KeycloakUrl = "http://192.168.1.11:8180"
$Realm = "unionflow"
$ClientId = "unionflow-mobile"

Write-Host "=== TEST CONFIGURATION KEYCLOAK MOBILE ===" -ForegroundColor Cyan
Write-Host ""

# 1. Test du realm
Write-Host "1. Test du realm '$Realm'..." -ForegroundColor Yellow
try {
    $realmConfig = Invoke-RestMethod -Uri "$KeycloakUrl/realms/$Realm/.well-known/openid_configuration"
    Write-Host "   ✅ Realm accessible" -ForegroundColor Green
    Write-Host "   Authorization endpoint: $($realmConfig.authorization_endpoint)" -ForegroundColor Gray
    Write-Host "   Token endpoint: $($realmConfig.token_endpoint)" -ForegroundColor Gray
} catch {
    Write-Host "   ❌ Realm non accessible: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 2. Test du client mobile
Write-Host "2. Test du client '$ClientId'..." -ForegroundColor Yellow
try {
    # Obtenir token admin
    $tokenBody = "username=admin&password=admin&grant_type=password&client_id=admin-cli"
    $tokenResponse = Invoke-RestMethod -Uri "$KeycloakUrl/realms/master/protocol/openid-connect/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $tokenBody
    $accessToken = $tokenResponse.access_token
    
    # Vérifier le client
    $headers = @{ "Authorization" = "Bearer $accessToken" }
    $clients = Invoke-RestMethod -Uri "$KeycloakUrl/admin/realms/$Realm/clients?clientId=$ClientId" -Method Get -Headers $headers
    
    if ($clients.Count -gt 0) {
        $client = $clients[0]
        Write-Host "   ✅ Client trouvé" -ForegroundColor Green
        Write-Host "   Enabled: $($client.enabled)" -ForegroundColor Gray
        Write-Host "   Public Client: $($client.publicClient)" -ForegroundColor Gray
        Write-Host "   Standard Flow: $($client.standardFlowEnabled)" -ForegroundColor Gray
        Write-Host "   Redirect URIs: $($client.redirectUris -join ', ')" -ForegroundColor Gray
        
        # Vérifier les redirect URIs
        $expectedUri = "com.unionflow.mobile://login-callback/*"
        if ($client.redirectUris -contains $expectedUri) {
            Write-Host "   ✅ Redirect URI correct" -ForegroundColor Green
        } else {
            Write-Host "   ⚠️ Redirect URI manquant: $expectedUri" -ForegroundColor Yellow
        }
    } else {
        Write-Host "   ❌ Client non trouvé" -ForegroundColor Red
    }
} catch {
    Write-Host "   ❌ Erreur: $($_.Exception.Message)" -ForegroundColor Red
}

# 3. Test d'authentification utilisateur
Write-Host "3. Test d'authentification utilisateur..." -ForegroundColor Yellow
try {
    $userBody = "username=test@unionflow.dev&password=test123&grant_type=password&client_id=$ClientId"
    $userResponse = Invoke-RestMethod -Uri "$KeycloakUrl/realms/$Realm/protocol/openid-connect/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $userBody
    
    Write-Host "   ✅ Authentification réussie" -ForegroundColor Green
    Write-Host "   Access token reçu: $($userResponse.access_token.Substring(0, 50))..." -ForegroundColor Gray
    Write-Host "   Token type: $($userResponse.token_type)" -ForegroundColor Gray
    Write-Host "   Expires in: $($userResponse.expires_in) secondes" -ForegroundColor Gray
} catch {
    Write-Host "   ❌ Échec authentification: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "=== DIAGNOSTIC TERMINÉ ===" -ForegroundColor Cyan
