# Script de verification de la configuration Keycloak

$KEYCLOAK_URL = "https://security.lions.dev"
$REALM = "btpxpress"
$CLIENT_ID = "btpxpress-frontend"
$ADMIN_USER = "admin"
$ADMIN_PASSWORD = "KeycloakAdmin2025!"

Write-Host ""
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host "Verification de la configuration Keycloak" -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host ""

# Obtenir le token
$body = @{
    grant_type = "password"
    client_id = "admin-cli"
    username = $ADMIN_USER
    password = $ADMIN_PASSWORD
}

$tokenResponse = Invoke-RestMethod -Uri "$KEYCLOAK_URL/realms/master/protocol/openid-connect/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $body
$token = $tokenResponse.access_token

$headers = @{
    Authorization = "Bearer $token"
    "Content-Type" = "application/json"
}

# 1. Verifier le client
Write-Host "1. Configuration du client '$CLIENT_ID':" -ForegroundColor Yellow
$clients = Invoke-RestMethod -Uri "$KEYCLOAK_URL/admin/realms/$REALM/clients" -Method Get -Headers $headers
$client = $clients | Where-Object { $_.clientId -eq $CLIENT_ID }

if ($client) {
    Write-Host "   Client ID: $($client.clientId)" -ForegroundColor Green
    Write-Host "   Client Type: Public" -ForegroundColor Green
    Write-Host ""
    Write-Host "   Redirect URIs:" -ForegroundColor Cyan
    $client.redirectUris | ForEach-Object { Write-Host "      - $_" -ForegroundColor White }
    Write-Host ""
    Write-Host "   Web Origins:" -ForegroundColor Cyan
    $client.webOrigins | ForEach-Object { Write-Host "      - $_" -ForegroundColor White }
} else {
    Write-Host "   Client non trouve!" -ForegroundColor Red
}

# 2. Verifier l'utilisateur de test
Write-Host ""
Write-Host "2. Utilisateur de test:" -ForegroundColor Yellow
$users = Invoke-RestMethod -Uri "$KEYCLOAK_URL/admin/realms/$REALM/users?username=test@btpxpress.com" -Method Get -Headers $headers

if ($users.Count -gt 0) {
    $user = $users[0]
    Write-Host "   Username: $($user.username)" -ForegroundColor Green
    Write-Host "   Email: $($user.email)" -ForegroundColor Green
    Write-Host "   Enabled: $($user.enabled)" -ForegroundColor Green
    Write-Host "   Email Verified: $($user.emailVerified)" -ForegroundColor Green

    # Recuperer les roles de l'utilisateur
    $userRoles = Invoke-RestMethod -Uri "$KEYCLOAK_URL/admin/realms/$REALM/users/$($user.id)/role-mappings/realm" -Method Get -Headers $headers

    Write-Host ""
    Write-Host "   Roles assignes:" -ForegroundColor Cyan
    if ($userRoles.Count -gt 0) {
        $userRoles | ForEach-Object { Write-Host "      - $($_.name)" -ForegroundColor White }
    } else {
        Write-Host "      Aucun role assigne" -ForegroundColor Yellow
    }
} else {
    Write-Host "   Utilisateur non trouve!" -ForegroundColor Red
}

# 3. Verification OIDC
Write-Host ""
Write-Host "3. Configuration OIDC:" -ForegroundColor Yellow
try {
    $oidcConfig = Invoke-RestMethod -Uri "$KEYCLOAK_URL/realms/$REALM/.well-known/openid-configuration" -Method Get
    Write-Host "   Issuer: $($oidcConfig.issuer)" -ForegroundColor Green
    Write-Host "   Authorization endpoint: OK" -ForegroundColor Green
    Write-Host "   Token endpoint: OK" -ForegroundColor Green
    Write-Host "   Userinfo endpoint: OK" -ForegroundColor Green
} catch {
    Write-Host "   Erreur lors de la verification OIDC" -ForegroundColor Red
}

Write-Host ""
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host "Verification terminee!" -ForegroundColor Green
Write-Host "==================================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Vous pouvez maintenant demarrer l'application:" -ForegroundColor Yellow
Write-Host "  mvn quarkus:dev" -ForegroundColor Cyan
Write-Host ""
Write-Host "Puis acceder a: http://localhost:8081" -ForegroundColor Yellow
Write-Host "Credentials: test@btpxpress.com / Test123!" -ForegroundColor Yellow
Write-Host ""
