# My Truck — nginx config (compatible /etc/nginx/conf.d/default.conf)
# This contains ONLY the server block. Worker/events/http directives stay
# in the default nginx.conf shipped with the image.

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

upstream mytruck_backend {
    server cariste-backend:8002;
}

server {
    listen 8080 default_server;
    server_name _;

    client_max_body_size 100m;

    # WebSocket endpoint — BEFORE generic /api/
    location /api/ws/ {
        proxy_pass http://mytruck_backend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        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;
        proxy_read_timeout 86400;
        proxy_send_timeout 86400;
    }

    # REST API
    location /api/ {
        proxy_pass http://mytruck_backend;
        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;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
    }

    # ─────────────────────────────────────────────────────────────────────────
    # Compat extension XDock+ legacy : `/xdock/scripts_get.php` était hébergé
    # sur alwaysdata avant. Maintenant le NAS proxie directement vers l'API
    # MyTruck (FastAPI). Aucune modif de l'extension n'est nécessaire — elle
    # continue de POST/GET la même URL qu'avant.
    # ─────────────────────────────────────────────────────────────────────────
    location = /xdock/scripts_get.php {
        proxy_pass http://mytruck_backend/api/xdock-scripts/by-email$is_args$args;
        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;
        # On forward l'en-tête X-API-Key fourni par l'extension
        proxy_set_header X-API-Key $http_x_api_key;
        proxy_pass_request_headers on;
    }

    # Static React build
    root /usr/share/nginx/html;
    index index.html;

    location /static/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }
    location ~* \.(?:ico|gif|jpe?g|png|webp|svg|woff2?|ttf|eot)$ {
        expires 30d;
        add_header Cache-Control "public";
        access_log off;
    }

    # SPA fallback
    location / {
        try_files $uri $uri/ /index.html;
    }
}
