server {
    listen 80;
    server_name clube67.com www.clube67.com newwhats.clube67.com;

    # Limite de upload para mídias do WhatsApp
    client_max_body_size 100M;

    # Resolver DNS interno do Docker para resolução dinâmica
    resolver 127.0.0.11 valid=30s;

    # Proxy para o Backend (Express API)
    location /api/ {
        set $backend_upstream http://newwhats-backend:8008;
        rewrite ^/api/api/(.*)$ /api/$1 break;
        proxy_pass $backend_upstream;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        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 para o Socket.io (Realtime Events)
    location /socket.io/ {
        set $backend_upstream http://newwhats-backend:8008;
        proxy_pass $backend_upstream;
        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 para o Frontend (Next.js SSR)
    location / {
        set $frontend_upstream http://newwhats-frontend:3003;
        proxy_pass $frontend_upstream;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        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;
    }
}
