# 🚀 Quick Start - Deployment VigilanzaTurni

Guida rapida per deployment: Replit → GitLab → vt.alfacom.it

## 📝 Checklist Pre-Deployment

- [ ] Server AlmaLinux 9 disponibile (min 2GB RAM)
- [ ] Dominio vt.alfacom.it configurato
- [ ] Account GitLab
- [ ] SSH access al server

---

## ⚡ Setup Iniziale (15 minuti)

### 1️⃣ Setup Server (5 min)

```bash
# SSH nel server
ssh root@vt.alfacom.it

# Clone repository
cd /var/www
git clone https://git.alfacom.it/marco/VigilanzaTurni.git vigilanza-turni
cd vigilanza-turni

# Esegui setup automatico
sudo bash deploy/setup-server.sh
```

Lo script installa automaticamente:
- Node.js 20
- PostgreSQL 15 (password autogenerata)
- PM2
- Nginx
- Certbot (SSL)

⚠️ **Password DB salvata in:** `/root/.vigilanza_db_password`

### 2️⃣ Configura Nginx (2 min)

```bash
# Copia configurazione Nginx
sudo cp deploy/nginx.conf /etc/nginx/conf.d/vigilanza-turni.conf

# Test e reload
sudo nginx -t
sudo systemctl reload nginx

# SSL Certificate
sudo certbot --nginx -d vt.alfacom.it
```

### 3️⃣ Configura .env (2 min)

```bash
cd /var/www/vigilanza-turni

# Recupera password DB da file sicuro
DB_PASS=$(grep PGPASSWORD /root/.vigilanza_db_password | cut -d= -f2)
SESSION_SECRET=$(openssl rand -base64 32)

# Crea .env con valori reali (no shell variables)
cat > .env << EOF
# Database
DATABASE_URL=postgresql://vigilanza_user:${DB_PASS}@localhost:5432/vigilanza_turni
PGHOST=localhost
PGPORT=5432
PGDATABASE=vigilanza_turni
PGUSER=vigilanza_user
PGPASSWORD=${DB_PASS}

# Session
SESSION_SECRET=${SESSION_SECRET}

# Application
NODE_ENV=production
PORT=5000
APP_URL=https://vt.alfacom.it

# Backup
BACKUP_ENABLED=true
BACKUP_DIR=/var/backups/vigilanza-turni
LOG_LEVEL=info
EOF

echo "✅ File .env creato"
```

**Verifica:**
```bash
cat .env | grep DATABASE_URL
# Deve mostrare password reale, non variabili shell
```

### 4️⃣ Primo Deploy (2 min)

```bash
cd /var/www/vigilanza-turni
bash deploy/deploy.sh
```

---

## 🔄 Workflow Quotidiano

### Deploy in 2 Comandi

**Da Replit o locale:**

```bash
# 1. Push a GitLab
./push-to-gitlab.sh

# 2. Sul server: Deploy
ssh root@vt.alfacom.it "cd /var/www/vigilanza-turni && bash deploy/deploy.sh"
```

**Il deploy automaticamente:**
- ✅ Esegue backup database
- ✅ Pull ultime modifiche da GitLab
- ✅ Build frontend Vite
- ✅ Esegue migrations database
- ✅ Restart applicazione PM2
- ✅ Health check

---

## 📊 Comandi Utili

```bash
# Status applicazione
pm2 status

# Logs real-time
pm2 logs vigilanza-turni

# Restart
pm2 restart vigilanza-turni

# Verifica backup
ls -lht /var/backups/vigilanza-turni/

# Ripristina backup (usa password da file)
export $(cat /root/.vigilanza_db_password | xargs)
gunzip -c /var/backups/vigilanza-turni/backup_20250116_143022.sql.gz | \
  psql -h localhost -U vigilanza_user -d vigilanza_turni
```

---

## 🆘 Troubleshooting

**App non risponde:**
```bash
pm2 restart vigilanza-turni
pm2 logs vigilanza-turni --lines 50
sudo systemctl reload nginx
```

**Errore database:**
```bash
# Verifica connessione (usa password da file)
export $(cat /root/.vigilanza_db_password | xargs)
psql -h localhost -U vigilanza_user -d vigilanza_turni -c "SELECT version();"
```

**Build fallito:**
```bash
cd /var/www/vigilanza-turni
rm -rf node_modules dist
npm ci
npm run build
pm2 restart vigilanza-turni
```

---

## 🌐 Accesso

**Applicazione:** https://vt.alfacom.it

**Backup automatici:** 
- Directory: `/var/backups/vigilanza-turni/`
- Retention: 30 giorni
- Formato: `backup_YYYYMMDD_HHMMSS.sql.gz`

---

## 📚 File Importanti

- `deploy/deploy.sh` - Script deployment automatico
- `deploy/setup-server.sh` - Setup iniziale server
- `deploy/nginx.conf` - Configurazione reverse proxy
- `.env` - Variabili ambiente produzione
- `push-to-gitlab.sh` - Helper push GitLab
