server {
    listen 80;

    # Redirect root to /jitbot/ (relative, preserves host/port)
    location / {
        return 301 /jitbot$request_uri;
    }

    # Redirect /jitbot (no slash) to /jitbot/
    location = /jitbot {
        return 301 /jitbot/;
    }

    # Serve React app at /jitbot/ (with slash)
    location /jitbot/ {
        root /usr/share/nginx/html;
        index index.html;
        try_files /${uri} /index.html =404;  # Explicitly prepend / to uri for correct path resolution
    }

    # Serve static assets under /jitbot/static/
    location /jitbot/static/ {
        root /usr/share/nginx/html;
        try_files $uri =404;  # Serve static files directly
    }

    # Proxy backend API requests
    location /jitbot/api/ {
        proxy_pass http://django-backend:8002/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $http_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;
        client_max_body_size 20m;
    }

    # Serve Django static files (separate from React assets)
    location /static/ {
        alias /usr/share/nginx/html/django-static/;
    }
}