# Deploy Checklist

## Pre-Deploy

- [ ] Dashboard built: `cd dashboard && npm run build`
- [ ] Backend `.env` configured with correct PORT and API keys
- [ ] `data/` directory exists (for SQLite)
- [ ] Test locally: `cd backend && npm start` → open `http://localhost:PORT`
- [ ] Scan QR locally, send test messages, verify full flow

## Copy to VPS

```bash
# 1. Build dashboard
cd dashboard && npm run build

# 2. SCP to VPS (adapt paths)
scp -r backend/ root@IP:/var/www/apps/APP_NAME/backend/
scp -r dashboard/dist/ root@IP:/var/www/apps/APP_NAME/dashboard/dist/

# 3. Install dependencies on VPS
ssh root@IP "cd /var/www/apps/APP_NAME/backend && npm install --production"

# 4. Create .env on VPS
ssh root@IP "cat > /var/www/apps/APP_NAME/backend/.env << 'EOF'
PORT=XXXX
GEMINI_API_KEY=AIza...
EOF"
```

## PM2 Setup

```bash
# Start
cd /var/www/apps/APP_NAME/backend
pm2 start src/index.js --name APP_NAME

# Save for auto-restart
pm2 save

# Useful commands
pm2 logs APP_NAME        # View logs
pm2 restart APP_NAME     # Restart
pm2 stop APP_NAME        # Stop
pm2 delete APP_NAME      # Remove
pm2 monit                # Live monitoring
```

## Nginx Config

```nginx
server {
    listen 443 ssl;
    server_name subdomain.domain.com;

    ssl_certificate /etc/letsencrypt/live/subdomain.domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/subdomain.domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:PORT;
        proxy_http_version 1.1;

        # CRITICAL for Socket.IO WebSocket:
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Timeout for long AI operations:
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }
}

# HTTP → HTTPS redirect
server {
    listen 80;
    server_name subdomain.domain.com;
    return 301 https://$host$request_uri;
}
```

```bash
# Enable site
ln -s /etc/nginx/sites-available/APP_NAME /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
```

## SSL (Certbot)

```bash
certbot --nginx -d subdomain.domain.com
```

## Post-Deploy Verification

- [ ] `curl https://subdomain.domain.com/api/status` → returns JSON
- [ ] Dashboard loads at `https://subdomain.domain.com`
- [ ] QR code appears on Connect page
- [ ] After scanning: status shows connected + phone number
- [ ] Send message on WhatsApp → bot responds
- [ ] Socket.IO works (dashboard updates in real-time)
- [ ] Check PM2 logs for errors: `pm2 logs APP_NAME --lines 50`

## Common Issues

| Problem | Solution |
|---------|----------|
| QR code doesn't appear | Check PM2 logs, Puppeteer may need `--no-sandbox` |
| Socket.IO connection fails | Nginx missing `Upgrade`/`Connection` headers |
| Images not sending | `express.json({ limit: '50mb' })` missing |
| Bot doesn't respond | Check `message` event handler, ensure groups are filtered |
| Session lost after restart | Verify `.wwebjs_auth/` persists (not in .gitignore on server) |
| Puppeteer crash on VPS | Install deps: `apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libxdamage1 libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2` |
| SQLite locked | Ensure WAL mode: `db.pragma('journal_mode = WAL')` |
| High memory usage | whatsapp-web.js uses ~300-500MB (Chromium), normal |
| 502 Bad Gateway | PM2 not running or wrong port in Nginx |
| AI timeout | Increase `proxy_read_timeout` in Nginx to 300s+ |

## Update Workflow

```bash
# On local machine
cd dashboard && npm run build

# Copy updated files
scp -r backend/src/ root@IP:/var/www/apps/APP_NAME/backend/src/
scp -r dashboard/dist/ root@IP:/var/www/apps/APP_NAME/dashboard/dist/

# Restart on VPS
ssh root@IP "pm2 restart APP_NAME"
```
