# Script d'intégration de PrimeFaces Freya Extension dans Lions User Manager
# Usage: .\integrate-freya-extension.ps1

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Intégration Freya Extension" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Chemins
$WORKSPACE = "C:\Users\dadyo\PersonalProjects\lions-workspace"
$FREYA_EXT_PATH = "$WORKSPACE\primefaces-freya-extension"
$USER_MANAGER_PATH = "$WORKSPACE\lions-user-manager"

# Étape 1 : Vérifier que primefaces-freya-extension existe
Write-Host "[1/5] Vérification de primefaces-freya-extension..." -ForegroundColor Yellow
if (-Not (Test-Path $FREYA_EXT_PATH)) {
    Write-Host "❌ ERREUR: primefaces-freya-extension introuvable à $FREYA_EXT_PATH" -ForegroundColor Red
    exit 1
}
Write-Host "✅ primefaces-freya-extension trouvé" -ForegroundColor Green
Write-Host ""

# Étape 2 : Compiler et installer primefaces-freya-extension
Write-Host "[2/5] Compilation et installation de primefaces-freya-extension..." -ForegroundColor Yellow
Set-Location $FREYA_EXT_PATH

Write-Host "Exécution de: mvn clean install -DskipTests" -ForegroundColor Gray
$result = mvn clean install -DskipTests 2>&1

if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ ERREUR lors de la compilation de primefaces-freya-extension" -ForegroundColor Red
    Write-Host $result -ForegroundColor Red
    exit 1
}

Write-Host "✅ primefaces-freya-extension compilé et installé dans le repository Maven local" -ForegroundColor Green
Write-Host ""

# Étape 3 : Vérifier l'installation dans le repository Maven local
Write-Host "[3/5] Vérification de l'installation dans le repository Maven local..." -ForegroundColor Yellow
$M2_REPO = "$env:USERPROFILE\.m2\repository\dev\lions\primefaces-freya-extension-runtime\1.0.0-SNAPSHOT"

if (Test-Path $M2_REPO) {
    Write-Host "✅ Artefact trouvé dans: $M2_REPO" -ForegroundColor Green
} else {
    Write-Host "⚠️  ATTENTION: Artefact non trouvé dans le repository Maven local" -ForegroundColor Yellow
}
Write-Host ""

# Étape 4 : Sauvegarder les pom.xml originaux
Write-Host "[4/5] Sauvegarde des pom.xml originaux..." -ForegroundColor Yellow
Set-Location $USER_MANAGER_PATH

$PARENT_POM = "$USER_MANAGER_PATH\pom.xml"
$CLIENT_POM = "$USER_MANAGER_PATH\lions-user-manager-client-quarkus-primefaces-freya\pom.xml"

if (Test-Path $PARENT_POM) {
    Copy-Item $PARENT_POM "$PARENT_POM.backup" -Force
    Write-Host "✅ Sauvegarde créée: pom.xml.backup" -ForegroundColor Green
}

if (Test-Path $CLIENT_POM) {
    Copy-Item $CLIENT_POM "$CLIENT_POM.backup" -Force
    Write-Host "✅ Sauvegarde créée: lions-user-manager-client-quarkus-primefaces-freya/pom.xml.backup" -ForegroundColor Green
}
Write-Host ""

# Étape 5 : Instructions manuelles
Write-Host "[5/5] Prochaines étapes manuelles..." -ForegroundColor Yellow
Write-Host ""
Write-Host "📝 Vous devez maintenant modifier les fichiers pom.xml :" -ForegroundColor Cyan
Write-Host ""
Write-Host "1️⃣  Éditer: $PARENT_POM" -ForegroundColor White
Write-Host "   Ajouter dans <properties>:" -ForegroundColor Gray
Write-Host "   <primefaces-freya-extension.version>1.0.0-SNAPSHOT</primefaces-freya-extension.version>" -ForegroundColor DarkGray
Write-Host ""
Write-Host "   Ajouter dans <dependencyManagement><dependencies>:" -ForegroundColor Gray
Write-Host "   <dependency>" -ForegroundColor DarkGray
Write-Host "       <groupId>dev.lions</groupId>" -ForegroundColor DarkGray
Write-Host "       <artifactId>primefaces-freya-extension-runtime</artifactId>" -ForegroundColor DarkGray
Write-Host "       <version>`${primefaces-freya-extension.version}</version>" -ForegroundColor DarkGray
Write-Host "   </dependency>" -ForegroundColor DarkGray
Write-Host ""
Write-Host "2️⃣  Éditer: $CLIENT_POM" -ForegroundColor White
Write-Host "   Ajouter dans <dependencies>:" -ForegroundColor Gray
Write-Host "   <dependency>" -ForegroundColor DarkGray
Write-Host "       <groupId>dev.lions</groupId>" -ForegroundColor DarkGray
Write-Host "       <artifactId>primefaces-freya-extension-runtime</artifactId>" -ForegroundColor DarkGray
Write-Host "   </dependency>" -ForegroundColor DarkGray
Write-Host ""
Write-Host "3️⃣  Dans vos fichiers .xhtml, ajouter le namespace:" -ForegroundColor White
Write-Host "   xmlns:fr=`"http://primefaces.org/freya`"" -ForegroundColor DarkGray
Write-Host ""
Write-Host "4️⃣  Remplacer les composants PrimeFaces par les composants Freya:" -ForegroundColor White
Write-Host "   Avant: <div class=`"field`"><p:outputLabel>...</p:outputLabel><p:inputText>...</p:inputText></div>" -ForegroundColor DarkGray
Write-Host "   Après: <fr:fieldInput label=`"...`" value=`"#{...}`" />" -ForegroundColor DarkGray
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Intégration terminée !" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "📚 Documentation complète: $USER_MANAGER_PATH\INTEGRATION_FREYA_EXTENSION.md" -ForegroundColor Green
Write-Host ""
Write-Host "🚀 Pour tester:" -ForegroundColor Yellow
Write-Host "   cd $USER_MANAGER_PATH" -ForegroundColor Gray
Write-Host "   mvn clean install" -ForegroundColor Gray
Write-Host "   cd lions-user-manager-client-quarkus-primefaces-freya" -ForegroundColor Gray
Write-Host "   mvn quarkus:dev" -ForegroundColor Gray
Write-Host ""

