# Script de migration automatique de toutes les pages XHTML vers Freya Extension
# Date: 2025-12-26
# Description: Migre tous les composants PrimeFaces vers Freya Extension

$ErrorActionPreference = "Stop"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Migration XHTML vers Freya Extension" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Chemin de base
$basePath = "lions-user-manager-client-quarkus-primefaces-freya\src\main\resources\META-INF\resources"

# Liste des fichiers XHTML à migrer
$xhtmlFiles = @(
    "$basePath\pages\user-manager\users\edit.xhtml",
    "$basePath\pages\user-manager\users\view.xhtml",
    "$basePath\pages\user-manager\users\profile.xhtml",
    "$basePath\pages\user-manager\roles\list.xhtml",
    "$basePath\pages\user-manager\roles\assign.xhtml",
    "$basePath\pages\user-manager\dashboard.xhtml",
    "$basePath\pages\user-manager\settings.xhtml",
    "$basePath\pages\user-manager\audit\logs.xhtml",
    "$basePath\pages\user-manager\sync\dashboard.xhtml",
    "$basePath\pages\user-manager\users.xhtml",
    "$basePath\pages\user-manager\roles.xhtml"
)

# Patterns de remplacement
$replacements = @(
    # Ajout du namespace Freya si absent
    @{
        Pattern = '(<ui:composition[^>]*xmlns:p="http://primefaces.org/ui")'
        Replacement = '$1`n                xmlns:fr="http://primefaces.org/freya"'
        Description = "Ajout namespace Freya"
    },
    
    # Migration des messages vers growl
    @{
        Pattern = '<p:messages\s+id="([^"]+)"\s+showDetail="true"\s+closable="true">\s*<p:autoUpdate\s*/>\s*</p:messages>'
        Replacement = '<fr:growl id="$1" />'
        Description = "Migration p:messages vers fr:growl"
    },
    
    # Migration des commandButton avec styleClass p-button-success
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-success[^"]*)"'
        Replacement = '<fr:commandButton$1severity="success"'
        Description = "Migration commandButton success"
    },
    
    # Migration des commandButton avec styleClass p-button-secondary
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-secondary[^"]*)"'
        Replacement = '<fr:commandButton$1severity="secondary"'
        Description = "Migration commandButton secondary"
    },
    
    # Migration des commandButton avec styleClass p-button-info
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-info[^"]*)"'
        Replacement = '<fr:commandButton$1severity="info"'
        Description = "Migration commandButton info"
    },
    
    # Migration des commandButton avec styleClass p-button-primary
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-primary[^"]*)"'
        Replacement = '<fr:commandButton$1severity="primary"'
        Description = "Migration commandButton primary"
    },
    
    # Migration des commandButton avec styleClass p-button-warning
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-warning[^"]*)"'
        Replacement = '<fr:commandButton$1severity="warning"'
        Description = "Migration commandButton warning"
    },
    
    # Migration des commandButton avec styleClass p-button-danger
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-danger[^"]*)"'
        Replacement = '<fr:commandButton$1severity="danger"'
        Description = "Migration commandButton danger"
    },
    
    # Migration des commandButton avec styleClass p-button-outlined
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-outlined[^"]*)"'
        Replacement = '<fr:commandButton$1outlined="true"'
        Description = "Migration commandButton outlined"
    },
    
    # Migration des commandButton avec styleClass p-button-text
    @{
        Pattern = '<p:commandButton([^>]*)styleClass="([^"]*p-button-text[^"]*)"'
        Replacement = '<fr:commandButton$1text="true"'
        Description = "Migration commandButton text"
    },
    
    # Migration des tags
    @{
        Pattern = '<p:tag\s+'
        Replacement = '<fr:tag '
        Description = "Migration p:tag vers fr:tag"
    },
    
    # Migration des dataTable
    @{
        Pattern = '<p:dataTable\s+'
        Replacement = '<fr:dataTable '
        Description = "Migration p:dataTable vers fr:dataTable"
    },
    
    # Fermeture des dataTable
    @{
        Pattern = '</p:dataTable>'
        Replacement = '</fr:dataTable>'
        Description = "Fermeture fr:dataTable"
    }
)

# Compteurs
$totalFiles = 0
$migratedFiles = 0
$errors = 0

# Migration des fichiers
foreach ($file in $xhtmlFiles) {
    if (Test-Path $file) {
        $totalFiles++
        Write-Host "Migration de: $file" -ForegroundColor Yellow
        
        try {
            # Lecture du contenu
            $content = Get-Content $file -Raw -Encoding UTF8
            $originalContent = $content
            
            # Application des remplacements
            $replacementCount = 0
            foreach ($replacement in $replacements) {
                $matches = [regex]::Matches($content, $replacement.Pattern)
                if ($matches.Count -gt 0) {
                    $content = $content -replace $replacement.Pattern, $replacement.Replacement
                    $replacementCount += $matches.Count
                    Write-Host "  ✓ $($replacement.Description): $($matches.Count) remplacement(s)" -ForegroundColor Green
                }
            }
            
            # Sauvegarde si des modifications ont été faites
            if ($content -ne $originalContent) {
                Set-Content $file -Value $content -Encoding UTF8 -NoNewline
                $migratedFiles++
                Write-Host "  ✓ Fichier migré avec succès ($replacementCount modifications)" -ForegroundColor Green
            } else {
                Write-Host "  ℹ Aucune modification nécessaire" -ForegroundColor Gray
            }
        }
        catch {
            $errors++
            Write-Host "  ✗ Erreur: $_" -ForegroundColor Red
        }
        
        Write-Host ""
    }
}

# Résumé
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Résumé de la migration" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Fichiers traités: $totalFiles" -ForegroundColor White
Write-Host "Fichiers migrés: $migratedFiles" -ForegroundColor Green
Write-Host "Erreurs: $errors" -ForegroundColor $(if ($errors -gt 0) { "Red" } else { "Green" })
Write-Host ""

if ($migratedFiles -gt 0) {
    Write-Host "✓ Migration terminée avec succès !" -ForegroundColor Green
    Write-Host ""
    Write-Host "Prochaines étapes:" -ForegroundColor Cyan
    Write-Host "1. Compiler le projet: mvn clean install -DskipTests" -ForegroundColor White
    Write-Host "2. Tester l'application" -ForegroundColor White
} else {
    Write-Host "ℹ Aucun fichier n'a nécessité de migration" -ForegroundColor Yellow
}

