server {
    listen       7860;
    listen       [::]:7860;
    server_name  _;

    root   /usr/share/nginx/html;
    index  index.html;

    # gzip
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss
        application/wasm
        image/svg+xml
        font/ttf
        font/otf
        font/woff
        font/woff2;

    # Long-lived cache for hashed Vite assets
    location /assets/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        add_header X-Frame-Options "DENY" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;
        add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
        try_files $uri =404;
    }

    # Never cache index.html (so deploys take effect immediately)
    location = /index.html {
        add_header Cache-Control "no-store, no-cache, must-revalidate" always;
        add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' data: https://fonts.gstatic.com; img-src 'self' data: blob: http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir https://*.arvanstorage.ir https://*.storage.iran.liara.site https://www.msc.ir https://*.basemaps.cartocdn.com https://www.google.com; media-src 'self' blob: http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir https://*.arvanstorage.ir https://*.storage.iran.liara.site; connect-src 'self' http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir; frame-src https://maps.google.com; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self';" always;
        add_header X-Frame-Options "DENY" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "strict-origin-when-cross-origin" always;
        add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
        try_files $uri =404;
    }

    # Never let the SPA fallback turn hidden files such as /.env into a 200.
    location ~ /\.(?!well-known(?:/|$)) {
        return 404;
    }

    # Same-origin proxy for CMS API and audio/video files
    location /api/ {
        proxy_pass http://steelforesight-cms:3001/api/;
        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 /media/ {
        proxy_pass http://steelforesight-cms:3001/media/;
        proxy_set_header Host $host;
        proxy_set_header Range $http_range;
        proxy_set_header If-Range $http_if_range;
        proxy_buffering off;
    }

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

    # Security headers
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' data: https://fonts.gstatic.com; img-src 'self' data: blob: http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir https://*.arvanstorage.ir https://*.storage.iran.liara.site https://www.msc.ir https://*.basemaps.cartocdn.com https://www.google.com; media-src 'self' blob: http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir https://*.arvanstorage.ir https://*.storage.iran.liara.site; connect-src 'self' http://* https://* https://cms-steelforesight.liara.run https://api.steelforesight.ir; frame-src https://maps.google.com; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self';" always;
    add_header X-Frame-Options        "DENY" always;
    add_header X-Content-Type-Options "nosniff"    always;
    add_header Referrer-Policy        "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy     "camera=(), microphone=(), geolocation=()" always;
}
