# ═══════════════════════════════════════════════════════════════
# OpusSuite · Nginx Reverse Proxy Configuration
# Ubicación: /etc/nginx/sites-available/opussuite
# Activar: ln -s /etc/nginx/sites-available/opussuite /etc/nginx/sites-enabled/
# ═══════════════════════════════════════════════════════════════

# Límite de subida de archivos (fotos de alta resolución)
client_max_body_size 100M;

# Cache de upstream para mejorar rendimiento
upstream opussuite_frontend {
    server 127.0.0.1:3000;
    keepalive 32;
}

upstream opussuite_backend {
    server 127.0.0.1:3001;
    keepalive 32;
}

# ── HTTP → Redirigir a HTTPS ───────────────────────────────────
server {
    listen 80;
    listen [::]:80;
    server_name tudominio.com www.tudominio.com;

    # Let's Encrypt challenge
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# ── HTTPS Principal ────────────────────────────────────────────
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name tudominio.com www.tudominio.com;

    # ── SSL (Let's Encrypt) ─────────────────────────────────
    ssl_certificate     /etc/letsencrypt/live/tudominio.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/tudominio.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache   shared:SSL:50m;
    ssl_session_tickets off;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    add_header Strict-Transport-Security "max-age=63072000" always;

    # ── Headers de seguridad ────────────────────────────────
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "no-referrer-when-downgrade" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;

    # ── API Backend (NestJS) ────────────────────────────────
    location /api/ {
        proxy_pass         http://opussuite_backend;
        proxy_http_version 1.1;
        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;
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;

        # Rate limiting para la API
        limit_req zone=api_limit burst=20 nodelay;
    }

    # ── WebSocket (Socket.io) ───────────────────────────────
    location /ws/ {
        proxy_pass         http://opussuite_backend;
        proxy_http_version 1.1;
        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_read_timeout 86400s;  # 24h para conexiones WS
    }

    # ── Archivos estáticos de Next.js (_next/static) ───────
    location /_next/static/ {
        proxy_pass http://opussuite_frontend;
        proxy_cache_valid 200 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # ── Favicon y archivos públicos ─────────────────────────
    location ~* \.(ico|png|jpg|jpeg|gif|svg|woff|woff2|ttf|css|js)$ {
        proxy_pass http://opussuite_frontend;
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }

    # ── Frontend Next.js (todo lo demás) ───────────────────
    location / {
        proxy_pass         http://opussuite_frontend;
        proxy_http_version 1.1;
        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;
        proxy_cache_bypass $http_upgrade;
    }

    # ── Logs ────────────────────────────────────────────────
    access_log /var/log/nginx/opussuite-access.log;
    error_log  /var/log/nginx/opussuite-error.log;
}

# ── Rate limiting zone (en http{} block de nginx.conf) ────────
# Añadir en /etc/nginx/nginx.conf dentro del bloque http{}:
# limit_req_zone $binary_remote_addr zone=api_limit:10m rate=30r/m;
