# Voice AI - Nginx Configuration for app2.syntheon.in
# 
# INSTALLATION:
# 1. sudo cp nginx-app2-syntheon.conf /etc/nginx/sites-available/app2-syntheon
# 2. sudo ln -s /etc/nginx/sites-available/app2-syntheon /etc/nginx/sites-enabled/
# 3. sudo nginx -t
# 4. sudo systemctl reload nginx
# 5. Setup SSL: sudo certbot --nginx -d app2.syntheon.in

server {
    listen 80;
    listen [::]:80;
    server_name app2.syntheon.in;

    # Uncomment after SSL setup to force HTTPS
    # return 301 https://$server_name$request_uri;

    # Client max body size
    client_max_body_size 10M;

    # Backend API - /api/* routes
    location /api/ {
        # Remove /api prefix when proxying to backend
        rewrite ^/api/(.*) /$1 break;
        
        proxy_pass http://localhost:46201;
        proxy_http_version 1.1;
        
        # WebSocket support
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        
        # Standard proxy headers
        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;
        
        # Disable caching
        proxy_cache_bypass $http_upgrade;
        
        # Longer timeout for agent operations
        proxy_read_timeout 300;
        proxy_connect_timeout 60;
        proxy_send_timeout 300;
        
        # CORS headers
        add_header Access-Control-Allow-Origin * always;
        add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
        add_header Access-Control-Allow-Headers "Authorization, Content-Type" always;
    }

    # Frontend - All other routes
    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        
        # WebSocket support (required for LiveKit)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        
        # Standard proxy headers
        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;
        
        # Disable caching for dynamic content
        proxy_cache_bypass $http_upgrade;
        
        # Long timeout for WebSocket connections
        proxy_read_timeout 86400;
        proxy_connect_timeout 60;
        proxy_send_timeout 60;
    }

    # Next.js static files optimization
    location /_next/static {
        proxy_pass http://localhost:3001;
        proxy_cache_valid 200 60m;
        add_header Cache-Control "public, immutable";
    }

    # Access logs
    access_log /var/log/nginx/app2-syntheon-access.log;
    error_log /var/log/nginx/app2-syntheon-error.log;
}

# HTTPS Configuration (automatically added by certbot)
# After running: sudo certbot --nginx -d app2.syntheon.in
