# Script simple pour créer le client mobile Keycloak

Write-Host "Creation du client mobile Keycloak..." -ForegroundColor Cyan

# Obtenir le token d'administration
Write-Host "Obtention du token d'administration..." -ForegroundColor Yellow
$tokenResponse = Invoke-RestMethod -Uri "http://localhost:8180/realms/master/protocol/openid-connect/token" -Method Post -Body "username=admin&password=admin&grant_type=password&client_id=admin-cli" -ContentType "application/x-www-form-urlencoded"
$adminToken = $tokenResponse.access_token
Write-Host "Token obtenu" -ForegroundColor Green

# Créer le client mobile
Write-Host "Creation du client mobile..." -ForegroundColor Yellow
$headers = @{
    Authorization = "Bearer $adminToken"
    "Content-Type" = "application/json"
}

$clientJson = Get-Content "client-simple.json" -Raw

try {
    Invoke-RestMethod -Uri "http://localhost:8180/admin/realms/unionflow/clients" -Method Post -Headers $headers -Body $clientJson
    Write-Host "Client mobile cree avec succes !" -ForegroundColor Green
}
catch {
    if ($_.Exception.Response.StatusCode -eq 409) {
        Write-Host "Client existe deja" -ForegroundColor Yellow
    }
    else {
        Write-Host "Erreur: $($_.Exception.Message)" -ForegroundColor Red
    }
}

Write-Host "Configuration terminee !" -ForegroundColor Green
