# Script simple pour créer le client unionflow-server
$KeycloakUrl = "http://192.168.1.11:8180"
$Realm = "unionflow"

Write-Host "Creation du client serveur..." -ForegroundColor Cyan

# Obtenir le token
$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
Write-Host "Token obtenu" -ForegroundColor Green

# Configuration du client
$clientConfig = @{
    clientId = "unionflow-server"
    name = "UnionFlow Server API"
    enabled = $true
    clientAuthenticatorType = "client-secret"
    secret = "unionflow-secret-2025"
    publicClient = $false
    standardFlowEnabled = $true
    directAccessGrantsEnabled = $true
    serviceAccountsEnabled = $true
    redirectUris = @("http://192.168.1.11:8080/*")
    webOrigins = @("http://192.168.1.11:8080")
} | ConvertTo-Json

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

try {
    Invoke-RestMethod -Uri "$KeycloakUrl/admin/realms/$Realm/clients" -Method Post -Headers $headers -Body $clientConfig
    Write-Host "Client serveur cree avec succes !" -ForegroundColor Green
}
catch {
    Write-Host "Erreur: $($_.Exception.Message)" -ForegroundColor Red
}
