# ==============================================================================
# Ring2All BSS — Back-Office Administration Web UI Nginx Configuration
# ==============================================================================

# Redirect HTTP (80) -> HTTPS (443) so PWA Service Workers & Secure Contexts work reliably
server {
    listen 80;
    listen [::]:80;
    server_name _;

    # ACME HTTP-01 challenge remains accessible over HTTP
    location ^~ /.well-known/acme-challenge/ {
        alias /var/www/softswitch/bss/web/.well-known/acme-challenge/;
        default_type "text/plain";
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name _;
    add_header X-Robots-Tag "none" always;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    access_log /var/log/nginx/softswitch_bss_web_access.log;
    error_log /var/log/nginx/softswitch_bss_web_error.log;

    # ACME HTTP-01 challenge
    location ^~ /.well-known/acme-challenge/ {
        alias /var/www/softswitch/bss/web/.well-known/acme-challenge/;
        default_type "text/plain";
    }

    # ── Structured JSON 429 Rate-Limit Handler (with Retry-After header for Frontend Warning Modal) ──
    error_page 429 = @rate_limited;
    location @rate_limited {
        default_type application/json;
        add_header Retry-After 5 always;
        add_header Cache-Control "no-store" always;
        return 429 '{"success":false,"error":"RATE_LIMIT_EXCEEDED","code":"RATE_LIMIT_EXCEEDED","retryAfter":5,"message":"Too many requests in a short period. Please wait a few seconds before trying again."}';
    }

    # Ring2All BSS Backend API Proxy (Port 3002)
    location /api/ {
        proxy_pass http://127.0.0.1:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header 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_set_header X-SSL-Client-Verify $ssl_client_verify;
        proxy_set_header X-SSL-Client-Cert $ssl_client_escaped_cert;
        proxy_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        client_max_body_size 100M;
    }

    # Static Assets
    location /assets/ {
        root /var/www/softswitch/bss/web;
        expires 7d;
        add_header Cache-Control "public, no-transform";
        add_header X-Robots-Tag "none" always;
    }

    # ── Setup Wizard HTTP Guard (Zero-Flash Server-Side Gate) ──
    location = /_internal/setup-guard {
        internal;
        proxy_pass http://127.0.0.1:3002/api/auth/setup-guard;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header Host $host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Authorization $http_authorization;
        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 @setup_redirect_login {
        return 302 /login;
    }

    location ~ ^/setup(-admin)?/?$ {
        auth_request /_internal/setup-guard;
        error_page 401 403 500 502 = @setup_redirect_login;
        root /var/www/softswitch/bss/web;
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
        add_header X-Robots-Tag "none" always;
        try_files /index.html =404;
    }

    # PWA Service Worker (must revalidate immediately)
    location = /sw.js {
        root /var/www/softswitch/bss/web;
        expires -1;
        add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0" always;
        add_header Service-Worker-Allowed "/" always;
        add_header X-Robots-Tag "none" always;
        try_files $uri =404;
    }

    # Admin Frontend Web SPA
    location / {
        root /var/www/softswitch/bss/web;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
