# Script PowerShell per Unificazione Documentazione NetGescon
# Data: 18/07/2025
# Scopo: Unificare tutte le cartelle docs sparse in una struttura organizzata

Write-Host "🔧 NETGESCON - UNIFICAZIONE DOCUMENTAZIONE" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan

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

# Percorsi delle cartelle docs originali
$DocsMain = "$BaseDir\docs"
$DocsLaravel = "$BaseDir\netgescon-laravel\docs"
$DocsTempWindows = "c:\temp"

Write-Host "`n📁 Verifica esistenza cartelle..." -ForegroundColor Yellow

# Verifica esistenza cartelle
if (!(Test-Path $DocsMain)) {
    Write-Host "❌ Cartella $DocsMain non trovata" -ForegroundColor Red
    exit 1
}

if (!(Test-Path $DocsLaravel)) {
    Write-Host "❌ Cartella $DocsLaravel non trovata" -ForegroundColor Red
    exit 1
}

Write-Host "✅ Cartelle sorgente verificate" -ForegroundColor Green

# Crea struttura di destinazione se non esiste
if (!(Test-Path $UnifiedDir)) {
    New-Item -ItemType Directory -Path $UnifiedDir -Force | Out-Null
    Write-Host "✅ Creata cartella unificata: $UnifiedDir" -ForegroundColor Green
}

Write-Host "`n📋 FASE 1: Copia da docs principale ($DocsMain)" -ForegroundColor Yellow

# 00-NAVIGAZIONE
Write-Host "   📂 00-NAVIGAZIONE..."
$NavDir = "$UnifiedDir\00-NAVIGAZIONE"
if (!(Test-Path $NavDir)) { New-Item -ItemType Directory -Path $NavDir -Force | Out-Null }

# Copia indice master
if (Test-Path "$BaseDir\00-INDICE-MASTER-NETGESCON.md") {
    Copy-Item "$BaseDir\00-INDICE-MASTER-NETGESCON.md" "$NavDir\" -Force
    Write-Host "     ✅ 00-INDICE-MASTER-NETGESCON.md" -ForegroundColor Green
}

# Copia QUICK REFERENCE se esiste
if (Test-Path "$DocsMain\QUICK-REFERENCE-CARD.md") {
    Copy-Item "$DocsMain\QUICK-REFERENCE-CARD.md" "$NavDir\" -Force
    Write-Host "     ✅ QUICK-REFERENCE-CARD.md" -ForegroundColor Green
}

# 01-MANUALI-OPERATIVI
Write-Host "   📂 01-MANUALI-OPERATIVI..."
$ManualDir = "$UnifiedDir\01-MANUALI-OPERATIVI"
if (!(Test-Path $ManualDir)) { New-Item -ItemType Directory -Path $ManualDir -Force | Out-Null }

# Copia manuali
$ManualFiles = @(
    "00-MANUALE-COMPLETO-NETGESCON-UNIFICATO.md",
    "RIEPILOGO-FINALE-SESSIONE-17-07-2025.md"
)

foreach ($file in $ManualFiles) {
    if (Test-Path "$DocsMain\$file") {
        Copy-Item "$DocsMain\$file" "$ManualDir\" -Force
        Write-Host "     ✅ $file" -ForegroundColor Green
    }
}

# Copia cartella manuals se esiste
if (Test-Path "$DocsMain\manuals") {
    Copy-Item "$DocsMain\manuals\*" "$ManualDir\" -Recurse -Force
    Write-Host "     ✅ Cartella manuals/*" -ForegroundColor Green
}

# 02-DOCUMENTAZIONE-TECNICA
Write-Host "   📂 02-DOCUMENTAZIONE-TECNICA..."
$TechDir = "$UnifiedDir\02-DOCUMENTAZIONE-TECNICA"
if (!(Test-Path $TechDir)) { New-Item -ItemType Directory -Path $TechDir -Force | Out-Null }

$TechFiles = @(
    "04-DATABASE-STRUTTURE.md",
    "05-INTERFACCIA-UNIVERSALE.md",
    "06-SISTEMA-MULTI-RUOLO.md",
    "08-FRONTEND-UX.md"
)

foreach ($file in $TechFiles) {
    if (Test-Path "$DocsMain\$file") {
        Copy-Item "$DocsMain\$file" "$TechDir\" -Force
        Write-Host "     ✅ $file" -ForegroundColor Green
    }
}

# 04-LOGS-SVILUPPO
Write-Host "   📂 04-LOGS-SVILUPPO..."
$LogsDir = "$UnifiedDir\04-LOGS-SVILUPPO"
if (!(Test-Path $LogsDir)) { New-Item -ItemType Directory -Path $LogsDir -Force | Out-Null }

$LogFiles = @(
    "RIEPILOGO-MODULARIZZAZIONE.md",
    "LISTA-FILE-BACKUP.md"
)

foreach ($file in $LogFiles) {
    if (Test-Path "$DocsMain\$file") {
        Copy-Item "$DocsMain\$file" "$LogsDir\" -Force
        Write-Host "     ✅ $file" -ForegroundColor Green
    }
}

# Copia cartella logs se esiste
if (Test-Path "$DocsMain\logs") {
    Copy-Item "$DocsMain\logs\*" "$LogsDir\" -Recurse -Force
    Write-Host "     ✅ Cartella logs/*" -ForegroundColor Green
}

# 07-API-SPECS
Write-Host "   📂 07-API-SPECS..."
$ApiDir = "$UnifiedDir\07-API-SPECS"
if (!(Test-Path $ApiDir)) { New-Item -ItemType Directory -Path $ApiDir -Force | Out-Null }

$ApiFiles = @(
    "07-API-INTEGRAZIONI.md",
    "05-SINCRONIZZAZIONE-AMBIENTE.md"
)

foreach ($file in $ApiFiles) {
    if (Test-Path "$DocsMain\$file") {
        Copy-Item "$DocsMain\$file" "$ApiDir\" -Force
        Write-Host "     ✅ $file" -ForegroundColor Green
    }
}

# Copia cartella api se esiste
if (Test-Path "$DocsMain\api") {
    Copy-Item "$DocsMain\api\*" "$ApiDir\" -Recurse -Force
    Write-Host "     ✅ Cartella api/*" -ForegroundColor Green
}

# 08-CHECKLISTS-TODO
Write-Host "   📂 08-CHECKLISTS-TODO..."
$CheckDir = "$UnifiedDir\08-CHECKLISTS-TODO"
if (!(Test-Path $CheckDir)) { New-Item -ItemType Directory -Path $CheckDir -Force | Out-Null }

# Copia cartella checklists se esiste
if (Test-Path "$DocsMain\checklists") {
    Copy-Item "$DocsMain\checklists\*" "$CheckDir\" -Recurse -Force
    Write-Host "     ✅ Cartella checklists/*" -ForegroundColor Green
}

Write-Host "`n📋 FASE 2: Copia da docs Laravel ($DocsLaravel)" -ForegroundColor Yellow

# 03-ARCHITETTURA-SISTEMA
Write-Host "   📂 03-ARCHITETTURA-SISTEMA..."
$ArchDir = "$UnifiedDir\03-ARCHITETTURA-SISTEMA"
if (!(Test-Path $ArchDir)) { New-Item -ItemType Directory -Path $ArchDir -Force | Out-Null }

$ArchFiles = @(
    "ARCHITETTURA_MODULARE_COMPLETATA.md",
    "RIEPILOGO_ARCHITETTURA_COMPLETATA.md",
    "PROTOCOLLO_COMUNICAZIONE.md",
    "sidebar-modulare.md"
)

foreach ($file in $ArchFiles) {
    if (Test-Path "$DocsLaravel\$file") {
        Copy-Item "$DocsLaravel\$file" "$ArchDir\" -Force
        Write-Host "     ✅ $file" -ForegroundColor Green
    }
}

# 01-MANUALI-OPERATIVI (aggiungi da Laravel)
$LaravelManualFiles = @(
    "PROCEDURA_OPERATIVA.md",
    "personalizzazione-tema.md",
    "miki.md"
)

foreach ($file in $LaravelManualFiles) {
    if (Test-Path "$DocsLaravel\$file") {
        Copy-Item "$DocsLaravel\$file" "$ManualDir\" -Force
        Write-Host "     ✅ $file (da Laravel)" -ForegroundColor Green
    }
}

# 00-NAVIGAZIONE (aggiungi da Laravel)
if (Test-Path "$DocsLaravel\QUICK_REFERENCE.md") {
    Copy-Item "$DocsLaravel\QUICK_REFERENCE.md" "$NavDir\QUICK_REFERENCE_LARAVEL.md" -Force
    Write-Host "     ✅ QUICK_REFERENCE_LARAVEL.md" -ForegroundColor Green
}

# Copia guide e specifiche se esistono
if (Test-Path "$DocsLaravel\guide") {
    $GuideDestDir = "$TechDir\guide-laravel"
    if (!(Test-Path $GuideDestDir)) { New-Item -ItemType Directory -Path $GuideDestDir -Force | Out-Null }
    Copy-Item "$DocsLaravel\guide\*" "$GuideDestDir\" -Recurse -Force
    Write-Host "     ✅ Cartella guide/*" -ForegroundColor Green
}

if (Test-Path "$DocsLaravel\specifiche") {
    $SpecDestDir = "$TechDir\specifiche-laravel"
    if (!(Test-Path $SpecDestDir)) { New-Item -ItemType Directory -Path $SpecDestDir -Force | Out-Null }
    Copy-Item "$DocsLaravel\specifiche\*" "$SpecDestDir\" -Recurse -Force
    Write-Host "     ✅ Cartella specifiche/*" -ForegroundColor Green
}

# FASE 3: Copia da materiali Windows temporanei
Write-Host "`n📋 FASE 3: Copia materiali Windows temporanei" -ForegroundColor Yellow

# 00-NAVIGAZIONE
if (Test-Path "$DocsTempWindows\README-TRANSITION-COMPLETE.md") {
    Copy-Item "$DocsTempWindows\README-TRANSITION-COMPLETE.md" "$NavDir\" -Force
    Write-Host "     ✅ README-TRANSITION-COMPLETE.md" -ForegroundColor Green
}

# 02-DOCUMENTAZIONE-TECNICA
if (Test-Path "$DocsTempWindows\FEATURES-INVENTORY-COMPLETE.md") {
    Copy-Item "$DocsTempWindows\FEATURES-INVENTORY-COMPLETE.md" "$TechDir\" -Force
    Write-Host "     ✅ FEATURES-INVENTORY-COMPLETE.md" -ForegroundColor Green
}

# 05-MIGRAZIONE-LINUX
Write-Host "   📂 05-MIGRAZIONE-LINUX..."
$MigrationDir = "$UnifiedDir\05-MIGRAZIONE-LINUX"
if (!(Test-Path $MigrationDir)) { New-Item -ItemType Directory -Path $MigrationDir -Force | Out-Null }

if (Test-Path "$DocsTempWindows\DEPLOYMENT-GUIDE-COMPLETE.md") {
    Copy-Item "$DocsTempWindows\DEPLOYMENT-GUIDE-COMPLETE.md" "$MigrationDir\" -Force
    Write-Host "     ✅ DEPLOYMENT-GUIDE-COMPLETE.md" -ForegroundColor Green
}

# 06-SCRIPTS-TOOLS
Write-Host "   📂 06-SCRIPTS-TOOLS..."
$ScriptsDir = "$UnifiedDir\06-SCRIPTS-TOOLS"
if (!(Test-Path $ScriptsDir)) { New-Item -ItemType Directory -Path $ScriptsDir -Force | Out-Null }

if (Test-Path "$DocsTempWindows\fix-vscode-install.sh") {
    Copy-Item "$DocsTempWindows\fix-vscode-install.sh" "$ScriptsDir\" -Force
    Write-Host "     ✅ fix-vscode-install.sh" -ForegroundColor Green
}

# Copia script da netgescon-laravel se esistono
if (Test-Path "$BaseDir\netgescon-laravel\*.sh") {
    Copy-Item "$BaseDir\netgescon-laravel\*.sh" "$ScriptsDir\" -Force
    Write-Host "     ✅ Script da netgescon-laravel/*" -ForegroundColor Green
}

# Copia script da cartella scripts principale se esiste
if (Test-Path "$BaseDir\scripts") {
    Copy-Item "$BaseDir\scripts\*" "$ScriptsDir\" -Recurse -Force
    Write-Host "     ✅ Cartella scripts/*" -ForegroundColor Green
}

Write-Host "`n📋 FASE 4: Creazione file di inventario" -ForegroundColor Yellow

# Crea inventario dei file copiati
$InventoryFile = "$UnifiedDir\INVENTARIO-UNIFICAZIONE.md"
$Inventory = @"
# 📋 INVENTARIO UNIFICAZIONE DOCUMENTAZIONE
**Data:** $(Get-Date -Format "dd/MM/yyyy HH:mm")
**Script:** unify-docs-netgescon.ps1

## 📁 CARTELLE SORGENTE UNIFICATE:
- ``$DocsMain``
- ``$DocsLaravel``
- ``$DocsTempWindows``

## 📂 STRUTTURA UNIFICATA CREATA:
- 📂 00-NAVIGAZIONE - Indici e guide rapide
- 📂 01-MANUALI-OPERATIVI - Guide pratiche
- 📂 02-DOCUMENTAZIONE-TECNICA - Specifiche tecniche
- 📂 03-ARCHITETTURA-SISTEMA - Design e architettura
- 📂 04-LOGS-SVILUPPO - Tracking sviluppo
- 📂 05-MIGRAZIONE-LINUX - Guide Linux/deployment
- 📂 06-SCRIPTS-TOOLS - Script e automazione
- 📂 07-API-SPECS - Specifiche API
- 📂 08-CHECKLISTS-TODO - Task e checklist

## 📊 STATISTICHE UNIFICAZIONE:
"@

# Conta i file in ogni directory
$Stats = @{}
Get-ChildItem $UnifiedDir -Recurse -File | Group-Object DirectoryName | ForEach-Object {
    $RelPath = $_.Name.Replace($UnifiedDir, "").TrimStart("\")
    $Stats[$RelPath] = $_.Count
}

foreach ($dir in $Stats.Keys | Sort-Object) {
    $Inventory += "`n- **$dir**: $($Stats[$dir]) file"
}

$Inventory += @"

## ✅ OPERAZIONI COMPLETATE:
- [x] Unificazione cartelle docs sparse
- [x] Organizzazione per categoria
- [x] Creazione indice master
- [x] Copia script e tools
- [x] Inventario documentazione

## 🎯 PROSSIMI PASSI:
1. Trasferire DOCS-UNIFIED su Linux
2. Aggiornare riferimenti nei documenti
3. Validare completezza materiale
4. Creare script sync automatico

---
*Generato automaticamente da unify-docs-netgescon.ps1*
"@

$Inventory | Out-File -FilePath $InventoryFile -Encoding UTF8 -Force
Write-Host "     ✅ INVENTARIO-UNIFICAZIONE.md creato" -ForegroundColor Green

Write-Host "`n✅ UNIFICAZIONE COMPLETATA!" -ForegroundColor Green
Write-Host "📂 Documentazione unificata in: $UnifiedDir" -ForegroundColor Cyan
Write-Host "📋 Inventario disponibile in: $InventoryFile" -ForegroundColor Cyan

# Mostra statistiche finali
Write-Host "`n📊 STATISTICHE FINALI:" -ForegroundColor Yellow
$TotalFiles = (Get-ChildItem $UnifiedDir -Recurse -File).Count
$TotalSize = [math]::Round((Get-ChildItem $UnifiedDir -Recurse -File | Measure-Object -Property Length -Sum).Sum / 1MB, 2)

Write-Host "📄 File totali: $TotalFiles" -ForegroundColor White
Write-Host "💾 Dimensione totale: $TotalSize MB" -ForegroundColor White
Write-Host "📁 Cartelle create: 8" -ForegroundColor White

Write-Host "`n🎯 PROSSIMA AZIONE:" -ForegroundColor Magenta
Write-Host "   Trasferire la cartella DOCS-UNIFIED nel server Linux:" -ForegroundColor White
Write-Host "   scp -r $UnifiedDir netgescon@192.168.0.200:/var/www/netgescon-complete/" -ForegroundColor Gray

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