# Nginx config for ai.glocalis.com
# Place in /etc/nginx/sites-available/ai.glocalis.com
# Then: ln -s /etc/nginx/sites-available/ai.glocalis.com /etc/nginx/sites-enabled/

server {
    listen 80;
    listen [::]:80;
    server_name ai.glocalis.com;

    # Redirect to HTTPS (Certbot will modify this)
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name ai.glocalis.com;

    # SSL certificates (Certbot will update these paths)
    ssl_certificate /etc/letsencrypt/live/ai.glocalis.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ai.glocalis.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # Logging
    access_log /var/log/nginx/ai.glocalis.com.access.log;
    error_log /var/log/nginx/ai.glocalis.com.error.log;

    # File upload limit (matches API spec)
    client_max_body_size 15M;

    # API proxy
    location / {
        proxy_pass http://127.0.0.1:8002;
        proxy_http_version 1.1;

        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;

        # Timeouts for long-running requests
        proxy_connect_timeout 60s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;

        # Disable buffering for streaming responses
        proxy_buffering off;
    }

    # Health check endpoint (no auth required)
    location /health {
        proxy_pass http://127.0.0.1:8002/health;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    # API docs
    location /docs {
        proxy_pass http://127.0.0.1:8002/docs;
        proxy_http_version 1.1;
        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;
    }

    location /openapi.json {
        proxy_pass http://127.0.0.1:8002/openapi.json;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }
}
