#!/bin/bash

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

echo "🔧 NETGESCON - UNIFICAZIONE DOCUMENTAZIONE (Linux)"
echo "=================================================="

BASE_DIR="$HOME/netgescon"
UNIFIED_DIR="$BASE_DIR/DOCS-UNIFIED"
DOCS_MAIN="$BASE_DIR/docs"
DOCS_LARAVEL="$BASE_DIR/netgescon-laravel/docs"

echo ""
echo "📁 Verifica esistenza cartelle..."

if [ ! -d "$DOCS_MAIN" ]; then
    echo "❌ Cartella $DOCS_MAIN non trovata"
    exit 1
fi

if [ ! -d "$DOCS_LARAVEL" ]; then
    echo "❌ Cartella $DOCS_LARAVEL non trovata"
    exit 1
fi

echo "✅ Cartelle sorgente verificate"

# Crea o pulisce struttura unificata
if [ -d "$UNIFIED_DIR" ]; then
    echo "🗑️ Pulizia cartella esistente..."
    rm -rf "$UNIFIED_DIR"
fi

mkdir -p "$UNIFIED_DIR"
echo "✅ Creata cartella unificata: $UNIFIED_DIR"

echo ""
echo "📋 FASE 1: Copia da docs principale"

# 00-NAVIGAZIONE
NAV_DIR="$UNIFIED_DIR/00-NAVIGAZIONE"
mkdir -p "$NAV_DIR"
echo "   📂 00-NAVIGAZIONE..."

if [ -f "$BASE_DIR/00-INDICE-MASTER-NETGESCON.md" ]; then
    cp "$BASE_DIR/00-INDICE-MASTER-NETGESCON.md" "$NAV_DIR/"
    echo "     ✅ 00-INDICE-MASTER-NETGESCON.md"
fi

# 01-MANUALI-OPERATIVI
MANUAL_DIR="$UNIFIED_DIR/01-MANUALI-OPERATIVI"
mkdir -p "$MANUAL_DIR"
echo "   📂 01-MANUALI-OPERATIVI..."

for file in "00-MANUALE-COMPLETO-NETGESCON-UNIFICATO.md" "RIEPILOGO-FINALE-SESSIONE-17-07-2025.md"; do
    if [ -f "$DOCS_MAIN/$file" ]; then
        cp "$DOCS_MAIN/$file" "$MANUAL_DIR/"
        echo "     ✅ $file"
    fi
done

if [ -d "$DOCS_MAIN/manuals" ]; then
    cp -r "$DOCS_MAIN/manuals"/* "$MANUAL_DIR/" 2>/dev/null || true
    echo "     ✅ Cartella manuals/*"
fi

# 02-DOCUMENTAZIONE-TECNICA
TECH_DIR="$UNIFIED_DIR/02-DOCUMENTAZIONE-TECNICA"
mkdir -p "$TECH_DIR"
echo "   📂 02-DOCUMENTAZIONE-TECNICA..."

for file in "04-DATABASE-STRUTTURE.md" "05-INTERFACCIA-UNIVERSALE.md" "06-SISTEMA-MULTI-RUOLO.md" "08-FRONTEND-UX.md"; do
    if [ -f "$DOCS_MAIN/$file" ]; then
        cp "$DOCS_MAIN/$file" "$TECH_DIR/"
        echo "     ✅ $file"
    fi
done

# 04-LOGS-SVILUPPO
LOGS_DIR="$UNIFIED_DIR/04-LOGS-SVILUPPO"
mkdir -p "$LOGS_DIR"
echo "   📂 04-LOGS-SVILUPPO..."

for file in "RIEPILOGO-MODULARIZZAZIONE.md" "LISTA-FILE-BACKUP.md"; do
    if [ -f "$DOCS_MAIN/$file" ]; then
        cp "$DOCS_MAIN/$file" "$LOGS_DIR/"
        echo "     ✅ $file"
    fi
done

if [ -d "$DOCS_MAIN/logs" ]; then
    cp -r "$DOCS_MAIN/logs"/* "$LOGS_DIR/" 2>/dev/null || true
    echo "     ✅ Cartella logs/*"
fi

# 07-API-SPECS
API_DIR="$UNIFIED_DIR/07-API-SPECS"
mkdir -p "$API_DIR"
echo "   📂 07-API-SPECS..."

for file in "07-API-INTEGRAZIONI.md" "05-SINCRONIZZAZIONE-AMBIENTE.md"; do
    if [ -f "$DOCS_MAIN/$file" ]; then
        cp "$DOCS_MAIN/$file" "$API_DIR/"
        echo "     ✅ $file"
    fi
done

if [ -d "$DOCS_MAIN/api" ]; then
    cp -r "$DOCS_MAIN/api"/* "$API_DIR/" 2>/dev/null || true
    echo "     ✅ Cartella api/*"
fi

# 08-CHECKLISTS-TODO
CHECK_DIR="$UNIFIED_DIR/08-CHECKLISTS-TODO"
mkdir -p "$CHECK_DIR"
echo "   📂 08-CHECKLISTS-TODO..."

if [ -d "$DOCS_MAIN/checklists" ]; then
    cp -r "$DOCS_MAIN/checklists"/* "$CHECK_DIR/" 2>/dev/null || true
    echo "     ✅ Cartella checklists/*"
fi

echo ""
echo "📋 FASE 2: Copia da docs Laravel"

# 03-ARCHITETTURA-SISTEMA
ARCH_DIR="$UNIFIED_DIR/03-ARCHITETTURA-SISTEMA"
mkdir -p "$ARCH_DIR"
echo "   📂 03-ARCHITETTURA-SISTEMA..."

for file in "ARCHITETTURA_MODULARE_COMPLETATA.md" "RIEPILOGO_ARCHITETTURA_COMPLETATA.md" "PROTOCOLLO_COMUNICAZIONE.md" "sidebar-modulare.md"; do
    if [ -f "$DOCS_LARAVEL/$file" ]; then
        cp "$DOCS_LARAVEL/$file" "$ARCH_DIR/"
        echo "     ✅ $file"
    fi
done

# Aggiungi manuali da Laravel
for file in "PROCEDURA_OPERATIVA.md" "personalizzazione-tema.md" "miki.md"; do
    if [ -f "$DOCS_LARAVEL/$file" ]; then
        cp "$DOCS_LARAVEL/$file" "$MANUAL_DIR/"
        echo "     ✅ $file (da Laravel)"
    fi
done

if [ -f "$DOCS_LARAVEL/QUICK_REFERENCE.md" ]; then
    cp "$DOCS_LARAVEL/QUICK_REFERENCE.md" "$NAV_DIR/QUICK_REFERENCE_LARAVEL.md"
    echo "     ✅ QUICK_REFERENCE_LARAVEL.md"
fi

# Copia guide e specifiche
if [ -d "$DOCS_LARAVEL/guide" ]; then
    mkdir -p "$TECH_DIR/guide-laravel"
    cp -r "$DOCS_LARAVEL/guide"/* "$TECH_DIR/guide-laravel/" 2>/dev/null || true
    echo "     ✅ Cartella guide/*"
fi

if [ -d "$DOCS_LARAVEL/specifiche" ]; then
    mkdir -p "$TECH_DIR/specifiche-laravel"
    cp -r "$DOCS_LARAVEL/specifiche"/* "$TECH_DIR/specifiche-laravel/" 2>/dev/null || true
    echo "     ✅ Cartella specifiche/*"
fi

echo ""
echo "📋 FASE 3: Script e materiali aggiuntivi"

# 05-MIGRAZIONE-LINUX
MIGRATION_DIR="$UNIFIED_DIR/05-MIGRAZIONE-LINUX"
mkdir -p "$MIGRATION_DIR"
echo "   📂 05-MIGRAZIONE-LINUX..."

if [ -f "$BASE_DIR/GUIDA-UNIFICAZIONE-DOCS.md" ]; then
    cp "$BASE_DIR/GUIDA-UNIFICAZIONE-DOCS.md" "$MIGRATION_DIR/"
    echo "     ✅ GUIDA-UNIFICAZIONE-DOCS.md"
fi

# 06-SCRIPTS-TOOLS
SCRIPTS_DIR="$UNIFIED_DIR/06-SCRIPTS-TOOLS"
mkdir -p "$SCRIPTS_DIR"
echo "   📂 06-SCRIPTS-TOOLS..."

if ls "$BASE_DIR/netgescon-laravel"/*.sh 1> /dev/null 2>&1; then
    cp "$BASE_DIR/netgescon-laravel"/*.sh "$SCRIPTS_DIR/" 2>/dev/null || true
    echo "     ✅ Script da netgescon-laravel/*"
fi

if [ -d "$BASE_DIR/scripts" ]; then
    cp -r "$BASE_DIR/scripts"/* "$SCRIPTS_DIR/" 2>/dev/null || true
    echo "     ✅ Cartella scripts/*"
fi

echo ""
echo "📋 FASE 4: Creazione indice e inventario"

# Indice master
cat > "$UNIFIED_DIR/00-INDICE-DOCUMENTAZIONE-UNIFICATA.md" << 'EOF'
# 📚 NETGESCON - DOCUMENTAZIONE UNIFICATA
## 🧭 Indice Master della Documentazione Completa

> **🎯 ENTRY POINT UNICO** per tutta la documentazione NetGescon  

## 🗂️ STRUTTURA ORGANIZZATA

### 📋 00-NAVIGAZIONE - Punti di accesso e guide rapide
### 🛠️ 01-MANUALI-OPERATIVI - Guide pratiche per l'uso
### 🔧 02-DOCUMENTAZIONE-TECNICA - Specifiche e implementazione
### 🏗️ 03-ARCHITETTURA-SISTEMA - Architettura e design
### 📝 04-LOGS-SVILUPPO - Storia e tracking sviluppo
### 🐧 05-MIGRAZIONE-LINUX - Guide migrazione e setup Linux
### ⚙️ 06-SCRIPTS-TOOLS - Script e strumenti automazione
### 📡 07-API-SPECS - Specifiche API e integrazioni
### ✅ 08-CHECKLISTS-TODO - Task e checklist

---
*📝 Documentazione unificata NetGescon*
EOF

echo "     ✅ Indice master creato"

# Inventario
INVENTORY_FILE="$UNIFIED_DIR/INVENTARIO-UNIFICAZIONE.md"
cat > "$INVENTORY_FILE" << EOF
# 📋 INVENTARIO UNIFICAZIONE DOCUMENTAZIONE
**Data:** $(date '+%d/%m/%Y %H:%M')
**Script:** unify-docs-netgescon-linux.sh

## 📁 CARTELLE UNIFICATE:
- \`$DOCS_MAIN\`
- \`$DOCS_LARAVEL\`

## 📊 STATISTICHE:
EOF

for dir in "$UNIFIED_DIR"/*; do
    if [ -d "$dir" ]; then
        DIR_NAME=$(basename "$dir")
        FILE_COUNT=$(find "$dir" -type f 2>/dev/null | wc -l)
        echo "- **$DIR_NAME**: $FILE_COUNT file" >> "$INVENTORY_FILE"
    fi
done

echo "     ✅ Inventario creato"

echo ""
echo "✅ UNIFICAZIONE COMPLETATA!"
echo "📂 Documentazione unificata in: $UNIFIED_DIR"

TOTAL_FILES=$(find "$UNIFIED_DIR" -type f 2>/dev/null | wc -l)
TOTAL_SIZE=$(du -sh "$UNIFIED_DIR" 2>/dev/null | cut -f1)

echo ""
echo "📊 STATISTICHE FINALI:"
echo "📄 File totali: $TOTAL_FILES"
echo "💾 Dimensione: $TOTAL_SIZE"

echo ""
echo "🎯 DOCUMENTAZIONE PRONTA PER VS CODE E COPILOT!"
