map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 5173;
    server_name _;

    # Local WOPI routes must stay on the frontend app.
    location /wopi/ {
        proxy_pass http://127.0.0.1:5193/wopi/;
        proxy_redirect http://127.0.0.1:5193/ http://172.16.0.59:5173/;
        proxy_redirect http://localhost:5193/ http://172.16.0.59:5173/;

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-Port $server_port;
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    # Collabora shell endpoint.
    location /collabora/ {
        proxy_pass http://127.0.0.1:9980/;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    # Collabora absolute asset and websocket endpoints.
    location /browser/ {
        # Keep the original escaped URI; Collabora websocket/document paths
        # break if nginx normalizes `%2F` inside the encoded WOPI URL.
        proxy_pass http://127.0.0.1:9980;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    location /cool/ {
        # Websocket path contains an encoded WOPI URL in the URI path.
        # Do not append a URI here, otherwise nginx may decode `%2F`.
        proxy_pass http://127.0.0.1:9980;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    location /coolws/ {
        # Child websocket bootstrap path used by Collabora Online.
        proxy_pass http://127.0.0.1:9980;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    location /hosting/ {
        proxy_pass http://127.0.0.1:9980;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    location /loleaflet/ {
        proxy_pass http://127.0.0.1:9980;

        proxy_http_version 1.1;
        proxy_set_header Host 172.16.0.59:5173;
        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_set_header X-Forwarded-Host 172.16.0.59:5173;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;
    }

    # Everything else remains on Next dev server.
    location / {
        proxy_pass http://127.0.0.1:5193;
        proxy_redirect http://127.0.0.1:5193/ http://172.16.0.59:5173/;
        proxy_redirect http://localhost:5193/ http://172.16.0.59:5173/;

        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Forwarded-Port $server_port;
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_connect_timeout 60s;

        proxy_buffer_size 256k;
        proxy_buffers 4 512k;
        proxy_busy_buffers_size 512k;
    }
}
