# Script di Verifica Unificazione Documentazione NetGescon
# Data: 18/07/2025
# Scopo: Verificare che tutti i file importanti siano stati unificati correttamente

Write-Host "🔍 NETGESCON - VERIFICA UNIFICAZIONE DOCUMENTAZIONE" -ForegroundColor Cyan
Write-Host "=====================================================" -ForegroundColor Cyan

$BaseDir = "u:\home\michele\netgescon"
$UnifiedDir = "$BaseDir\DOCS-UNIFIED"

if (!(Test-Path $UnifiedDir)) {
    Write-Host "❌ Cartella DOCS-UNIFIED non trovata!" -ForegroundColor Red
    Write-Host "   Eseguire prima: .\unify-docs-netgescon.ps1" -ForegroundColor Yellow
    exit 1
}

Write-Host "`n📋 VERIFICA FILE CRITICI..." -ForegroundColor Yellow

# Definisci file critici che DEVONO essere presenti
$CriticalFiles = @{
    "00-NAVIGAZIONE" = @(
        "00-INDICE-MASTER-NETGESCON.md",
        "README-TRANSITION-COMPLETE.md"
    )
    "01-MANUALI-OPERATIVI" = @(
        "00-MANUALE-COMPLETO-NETGESCON-UNIFICATO.md",
        "PROCEDURA_OPERATIVA.md"
    )
    "02-DOCUMENTAZIONE-TECNICA" = @(
        "FEATURES-INVENTORY-COMPLETE.md",
        "04-DATABASE-STRUTTURE.md",
        "05-INTERFACCIA-UNIVERSALE.md"
    )
    "03-ARCHITETTURA-SISTEMA" = @(
        "ARCHITETTURA_MODULARE_COMPLETATA.md",
        "RIEPILOGO_ARCHITETTURA_COMPLETATA.md"
    )
    "05-MIGRAZIONE-LINUX" = @(
        "DEPLOYMENT-GUIDE-COMPLETE.md"
    )
    "06-SCRIPTS-TOOLS" = @(
        "fix-vscode-install.sh"
    )
}

$MissingFiles = @()
$FoundFiles = @()

foreach ($folder in $CriticalFiles.Keys) {
    Write-Host "`n📂 Verifica $folder..." -ForegroundColor White
    
    foreach ($file in $CriticalFiles[$folder]) {
        $FilePath = "$UnifiedDir\$folder\$file"
        
        if (Test-Path $FilePath) {
            Write-Host "   ✅ $file" -ForegroundColor Green
            $FoundFiles += "$folder\$file"
        } else {
            Write-Host "   ❌ $file - MANCANTE!" -ForegroundColor Red
            $MissingFiles += "$folder\$file"
        }
    }
}

Write-Host "`n📊 RISULTATI VERIFICA:" -ForegroundColor Yellow

# Statistiche generali
$TotalFiles = (Get-ChildItem $UnifiedDir -Recurse -File).Count
$TotalSize = [math]::Round((Get-ChildItem $UnifiedDir -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1MB, 2)
$TotalFolders = (Get-ChildItem $UnifiedDir -Directory).Count

Write-Host "📄 File totali unificati: $TotalFiles" -ForegroundColor White
Write-Host "💾 Dimensione totale: $TotalSize MB" -ForegroundColor White
Write-Host "📁 Cartelle principali: $TotalFolders" -ForegroundColor White

Write-Host "`n✅ File critici trovati: $($FoundFiles.Count)" -ForegroundColor Green
if ($MissingFiles.Count -gt 0) {
    Write-Host "❌ File critici mancanti: $($MissingFiles.Count)" -ForegroundColor Red
    Write-Host "`n📝 FILE MANCANTI:" -ForegroundColor Red
    foreach ($file in $MissingFiles) {
        Write-Host "   - $file" -ForegroundColor Red
    }
} else {
    Write-Host "🎉 Tutti i file critici sono presenti!" -ForegroundColor Green
}

Write-Host "`n📂 STRUTTURA CARTELLE CREATA:" -ForegroundColor Yellow
Get-ChildItem $UnifiedDir -Directory | ForEach-Object {
    $FileCount = (Get-ChildItem $_.FullName -Recurse -File).Count
    $FolderSize = [math]::Round((Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1KB, 1)
    Write-Host "   📁 $($_.Name): $FileCount file ($FolderSize KB)" -ForegroundColor White
}

# Verifica presenza di file duplicati
Write-Host "`n🔍 VERIFICA DUPLICATI..." -ForegroundColor Yellow
$AllFiles = Get-ChildItem $UnifiedDir -Recurse -File
$FileHashes = @{}
$Duplicates = @()

foreach ($file in $AllFiles) {
    $hash = Get-FileHash $file.FullName -Algorithm MD5
    if ($FileHashes.ContainsKey($hash.Hash)) {
        $Duplicates += @{
            "Original" = $FileHashes[$hash.Hash]
            "Duplicate" = $file.FullName
        }
    } else {
        $FileHashes[$hash.Hash] = $file.FullName
    }
}

if ($Duplicates.Count -gt 0) {
    Write-Host "⚠️ Trovati $($Duplicates.Count) file duplicati:" -ForegroundColor Yellow
    foreach ($dup in $Duplicates) {
        Write-Host "   - $($dup.Original)" -ForegroundColor Gray
        Write-Host "   - $($dup.Duplicate)" -ForegroundColor Gray
        Write-Host "" -ForegroundColor Gray
    }
} else {
    Write-Host "✅ Nessun duplicato trovato" -ForegroundColor Green
}

# Genera report di verifica
$ReportFile = "$UnifiedDir\REPORT-VERIFICA-UNIFICAZIONE.md"
$Report = @"
# 📋 REPORT VERIFICA UNIFICAZIONE DOCUMENTAZIONE
**Data Verifica:** $(Get-Date -Format "dd/MM/yyyy HH:mm")
**Script:** verify-docs-unification.ps1

## 📊 STATISTICHE GENERALI
- **File Totali:** $TotalFiles
- **Dimensione Totale:** $TotalSize MB
- **Cartelle Principali:** $TotalFolders

## ✅ FILE CRITICI
- **Trovati:** $($FoundFiles.Count)
- **Mancanti:** $($MissingFiles.Count)

### 📄 File Critici Presenti:
"@

foreach ($file in $FoundFiles) {
    $Report += "`n- ✅ $file"
}

if ($MissingFiles.Count -gt 0) {
    $Report += "`n`n### ❌ File Critici Mancanti:"
    foreach ($file in $MissingFiles) {
        $Report += "`n- ❌ $file"
    }
}

$Report += @"

## 📂 STRUTTURA CARTELLE
"@

Get-ChildItem $UnifiedDir -Directory | ForEach-Object {
    $FileCount = (Get-ChildItem $_.FullName -Recurse -File).Count
    $FolderSize = [math]::Round((Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1KB, 1)
    $Report += "`n- **$($_.Name)**: $FileCount file ($FolderSize KB)"
}

if ($Duplicates.Count -gt 0) {
    $Report += "`n`n## ⚠️ FILE DUPLICATI ($($Duplicates.Count) trovati)"
    foreach ($dup in $Duplicates) {
        $Report += "`n- Original: $($dup.Original.Replace($UnifiedDir, ""))"
        $Report += "`n- Duplicate: $($dup.Duplicate.Replace($UnifiedDir, ""))"
        $Report += ""
    }
} else {
    $Report += "`n`n## ✅ DUPLICATI: Nessun duplicato trovato"
}

$Report += @"

## 🎯 VALUTAZIONE FINALE
"@

if ($MissingFiles.Count -eq 0 -and $Duplicates.Count -eq 0) {
    $Report += "`n**✅ ECCELLENTE** - Unificazione completata con successo!"
    $Status = "ECCELLENTE"
} elseif ($MissingFiles.Count -le 2 -and $Duplicates.Count -le 3) {
    $Report += "`n**⚠️ BUONA** - Unificazione riuscita con piccoli problemi da risolvere"
    $Status = "BUONA"
} else {
    $Report += "`n**❌ PROBLEMI** - Necessaria revisione unificazione"
    $Status = "PROBLEMI"
}

$Report += @"

## 🚀 PROSSIMI PASSI
1. [ ] Trasferire DOCS-UNIFIED su server Linux
2. [ ] Aggiornare riferimenti incrociati
3. [ ] Validare completezza contenuti
4. [ ] Configurare sincronizzazione automatica

---
*Report generato automaticamente da verify-docs-unification.ps1*
"@

$Report | Out-File -FilePath $ReportFile -Encoding UTF8 -Force

Write-Host "`n📋 REPORT GENERATO:" -ForegroundColor Cyan
Write-Host "   $ReportFile" -ForegroundColor White

Write-Host "`n🎯 VALUTAZIONE FINALE: $Status" -ForegroundColor $(if($Status -eq "ECCELLENTE") {"Green"} elseif($Status -eq "BUONA") {"Yellow"} else {"Red"})

if ($Status -eq "ECCELLENTE") {
    Write-Host "`n🚀 PRONTO PER IL TRASFERIMENTO!" -ForegroundColor Green
    Write-Host "   Comando per trasferire su Linux:" -ForegroundColor White
    Write-Host "   scp -r $UnifiedDir netgescon@192.168.0.200:/var/www/netgescon-complete/" -ForegroundColor Gray
} elseif ($Status -eq "BUONA") {
    Write-Host "`n⚠️ Rivedere i problemi minori prima del trasferimento" -ForegroundColor Yellow
} else {
    Write-Host "`n❌ Rivedere l'unificazione prima di procedere" -ForegroundColor Red
}

Write-Host "`nPremere Enter per continuare..." -ForegroundColor Yellow
Read-Host
