# Script PowerShell per convertire tutte le viste da x-app-layout a @extends('layouts.app-universal')
# Autore: NetGesCon Team
# Data: 10 Luglio 2025

Write-Host "🔧 Convertitore Layout NetGesCon" -ForegroundColor Cyan
Write-Host "=================================" -ForegroundColor Cyan
Write-Host ""

# Percorso base delle viste
$basePath = "resources/views/admin"

# Trova tutti i file .blade.php che contengono x-app-layout
$files = Get-ChildItem -Path $basePath -Recurse -Filter "*.blade.php" | Where-Object {
    $content = Get-Content $_.FullName -Raw
    $content -match "<x-app-layout>"
}

Write-Host "📋 Trovati $($files.Count) file da convertire:" -ForegroundColor Yellow
Write-Host ""

foreach ($file in $files) {
    Write-Host "  • $($file.Name)" -ForegroundColor White
}

Write-Host ""
$confirm = Read-Host "⚡ Vuoi procedere con la conversione? (y/N)"

if ($confirm -eq "y" -or $confirm -eq "Y") {
    Write-Host ""
    Write-Host "🚀 Avvio conversione..." -ForegroundColor Green
    Write-Host ""
    
    $converted = 0
    
    foreach ($file in $files) {
        try {
            Write-Host "📝 Convertendo: $($file.Name)..." -ForegroundColor Blue
            
            # Leggi il contenuto
            $content = Get-Content $file.FullName -Raw
            
            # Pattern di sostituzione
            $patterns = @{
                # Sostituisci <x-app-layout> con @extends
                '<x-app-layout>' = '@extends(''layouts.app-universal'')'
                
                # Sostituisci header slot con section content
                '<x-slot name="header">' = '@section(''content'')'
                '</x-slot>' = ''
                
                # Sostituisci chiusura layout
                '</x-app-layout>' = '@endsection'
                
                # Header generico (se presente)
                '<h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">' = '<h4 class="mb-0">'
                '</h2>' = '</h4>'
            }
            
            # Applica le sostituzioni
            foreach ($pattern in $patterns.GetEnumerator()) {
                $content = $content -replace [regex]::Escape($pattern.Key), $pattern.Value
            }
            
            # Aggiungi container Bootstrap se necessario
            if ($content -notmatch '@section\(''content''\)') {
                $content = $content -replace '@extends\(''layouts\.app-universal''\)', "@extends('layouts.app-universal')`n`n@section('content')"
                $content = $content -replace '@endsection$', "@endsection`n@endsection"
            }
            
            # Salva il file
            Set-Content -Path $file.FullName -Value $content -Encoding UTF8
            
            Write-Host "  ✅ Convertito con successo!" -ForegroundColor Green
            $converted++
            
        } catch {
            Write-Host "  ❌ Errore durante la conversione: $($_.Exception.Message)" -ForegroundColor Red
        }
    }
    
    Write-Host ""
    Write-Host "🎉 Conversione completata!" -ForegroundColor Green
    Write-Host "📊 File convertiti: $converted su $($files.Count)" -ForegroundColor Cyan
    Write-Host ""
    Write-Host "🔄 Prossimi passi:" -ForegroundColor Yellow
    Write-Host "  1. Verifica manualmente i file convertiti" -ForegroundColor White
    Write-Host "  2. Testa la navigazione nel browser" -ForegroundColor White
    Write-Host "  3. Adatta eventuali classi Tailwind a Bootstrap" -ForegroundColor White
    
} else {
    Write-Host ""
    Write-Host "❌ Conversione annullata dall'utente." -ForegroundColor Red
}

Write-Host ""
Write-Host "👋 Script terminato." -ForegroundColor Cyan
