#!/bin/bash

# Script completo per setup ambiente di sviluppo Linux
echo "🐧 SETUP COMPLETO AMBIENTE SVILUPPO LINUX"
echo "========================================="

# Colori
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_step() { echo -e "${BLUE}[STEP]${NC} $1"; }

log_step "1️⃣ Pulizia configurazioni conflittuali Microsoft..."
sudo rm -f /etc/apt/sources.list.d/vscode.list
sudo rm -f /etc/apt/trusted.gpg.d/packages.microsoft.gpg
sudo rm -f /usr/share/keyrings/microsoft.gpg

log_step "2️⃣ Installazione prerequisiti..."
sudo apt update
sudo apt install -y curl wget gpg software-properties-common apt-transport-https git

log_step "3️⃣ Configurazione repository Microsoft..."
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo gpg --dearmor -o /usr/share/keyrings/microsoft.gpg
echo "deb [arch=amd64,arm64,armhf signed-by=/usr/share/keyrings/microsoft.gpg] https://packages.microsoft.com/repos/code stable main" | sudo tee /etc/apt/sources.list.d/vscode.list

log_step "4️⃣ Installazione VS Code..."
sudo apt update
sudo apt install -y code

log_step "5️⃣ Installazione estensioni VS Code essenziali..."
# Estensioni per sviluppo Laravel/PHP
code --install-extension ms-vscode.vscode-json
code --install-extension ms-python.python
code --install-extension bmewburn.vscode-intelephense-client
code --install-extension bradlc.vscode-tailwindcss
code --install-extension onecentlin.laravel-blade
code --install-extension ryannaddy.laravel-artisan
code --install-extension codingyu.laravel-goto-view

# GitHub Copilot
code --install-extension GitHub.copilot
code --install-extension GitHub.copilot-chat

log_step "6️⃣ Configurazione VS Code per Laravel..."
mkdir -p ~/.config/Code/User
cat > ~/.config/Code/User/settings.json << 'EOF'
{
    "workbench.colorTheme": "Default Dark+",
    "editor.fontSize": 14,
    "editor.fontFamily": "'Fira Code', 'Courier New', monospace",
    "editor.fontLigatures": true,
    "editor.tabSize": 4,
    "editor.insertSpaces": true,
    "files.autoSave": "afterDelay",
    "files.autoSaveDelay": 1000,
    "editor.formatOnSave": true,
    "editor.minimap.enabled": true,
    "editor.wordWrap": "on",
    "php.suggest.basic": false,
    "php.validate.enable": true,
    "php.validate.executablePath": "/usr/bin/php",
    "intelephense.files.maxSize": 5000000,
    "intelephense.completion.triggerParameterHints": true,
    "git.enableSmartCommit": true,
    "git.autofetch": true,
    "terminal.integrated.defaultProfile.linux": "bash",
    "terminal.integrated.fontSize": 14,
    "github.copilot.enable": {
        "*": true,
        "yaml": false,
        "plaintext": false,
        "markdown": true,
        "php": true,
        "javascript": true,
        "typescript": true,
        "css": true,
        "scss": true,
        "html": true,
        "blade": true
    },
    "laravel.artisan.location": "./artisan",
    "workbench.startupEditor": "welcomePage",
    "explorer.confirmDelete": false,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue"
}
EOF

log_step "7️⃣ Configurazione workspace Netgescon..."
mkdir -p /var/www/netgescon/.vscode
cat > /var/www/netgescon/.vscode/settings.json << 'EOF'
{
    "php.validate.executablePath": "/usr/bin/php",
    "php.executablePath": "/usr/bin/php",
    "intelephense.environment.phpVersion": "8.3.0",
    "files.associations": {
        "*.blade.php": "blade"
    },
    "emmet.includeLanguages": {
        "blade": "html"
    },
    "terminal.integrated.cwd": "/var/www/netgescon"
}
EOF

cat > /var/www/netgescon/.vscode/extensions.json << 'EOF'
{
    "recommendations": [
        "bmewburn.vscode-intelephense-client",
        "onecentlin.laravel-blade",
        "ryannaddy.laravel-artisan",
        "bradlc.vscode-tailwindcss",
        "GitHub.copilot",
        "GitHub.copilot-chat"
    ]
}
EOF

log_step "8️⃣ Installazione font Fira Code..."
sudo apt install -y fonts-firacode

log_step "9️⃣ Configurazione environment desktop..."
if ! command -v gnome-session &> /dev/null; then
    log_warn "Installazione ambiente desktop minimale..."
    sudo apt install -y ubuntu-desktop-minimal
    log_warn "⚠️ RIAVVIO NECESSARIO per l'interfaccia grafica"
fi

log_step "🔟 Configurazione accesso remoto..."
read -p "Vuoi configurare il desktop remoto (RDP)? (y/n): " setup_rdp
if [[ $setup_rdp =~ ^[Yy]$ ]]; then
    sudo apt install -y xrdp
    sudo systemctl enable xrdp
    sudo systemctl start xrdp
    sudo ufw allow 3389 2>/dev/null || true
    log_info "✅ RDP configurato su porta 3389"
fi

log_step "🎯 Test finale..."
if command -v code &> /dev/null; then
    log_info "✅ VS Code installato: $(code --version | head -1)"
else
    log_warn "❌ Problema con VS Code"
fi

echo ""
echo "🎉 SETUP COMPLETATO!"
echo "==================="
echo ""
echo "📋 Prossimi passi:"
echo "1. Se necessario: sudo reboot"
echo "2. Avvia VS Code: code /var/www/netgescon"
echo "3. Login GitHub Copilot: Ctrl+Shift+P → 'GitHub Copilot: Sign In'"
echo "4. RDP (se installato): 192.168.0.200:3389"
echo ""
echo "🛠️ Comandi di sviluppo:"
echo "- Apri progetto: code /var/www/netgescon"
echo "- Server Laravel: php artisan serve --host=0.0.0.0"
echo "- Logs Laravel: tail -f storage/logs/laravel.log"
echo "- Git status: git status"
echo ""
echo "🔧 Shortcuts VS Code utili:"
echo "- Copilot Chat: Ctrl+Shift+I"
echo "- Command Palette: Ctrl+Shift+P"
echo "- Terminal: Ctrl+`"
echo "- File Explorer: Ctrl+Shift+E"
