# Voice AI - Nginx Configuration
# 
# INSTRUCTIONS:
# 1. Replace 'voiceai.yourdomain.com' with your actual domain
# 2. Replace 'api.voiceai.yourdomain.com' with your API subdomain
# 3. Copy this file: sudo cp nginx-voiceai.conf /etc/nginx/sites-available/voiceai
# 4. Enable site: sudo ln -s /etc/nginx/sites-available/voiceai /etc/nginx/sites-enabled/
# 5. Test config: sudo nginx -t
# 6. Reload Nginx: sudo systemctl reload nginx
# 7. Setup SSL: sudo certbot --nginx -d voiceai.yourdomain.com -d api.voiceai.yourdomain.com

# Frontend - voiceai.yourdomain.com
server {
    listen 80;
    listen [::]:80;
    server_name voiceai.yourdomain.com;

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

    # Client max body size (for file uploads if needed)
    client_max_body_size 10M;

    # Proxy to Next.js frontend
    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 (optional 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/voiceai-frontend-access.log;
    error_log /var/log/nginx/voiceai-frontend-error.log;
}

# Backend API - api.voiceai.yourdomain.com
server {
    listen 80;
    listen [::]:80;
    server_name api.voiceai.yourdomain.com;

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

    # Client max body size
    client_max_body_size 10M;

    # Proxy to Python backend
    location / {
        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 (if needed for API access)
    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;

    # Access logs
    access_log /var/log/nginx/voiceai-backend-access.log;
    error_log /var/log/nginx/voiceai-backend-error.log;
}

# HTTPS Configuration (automatically added by certbot)
# After running certbot, the SSL configuration will be added here
