# ==============================================================================
# Ring2All BSS — Customer Self-Care Store & Billing Portal Nginx Configuration
# Dedicated TLS listener on port 8443 so it never hijacks port 443 from softswitch-bss-web
# ==============================================================================

server {
    listen 8443 ssl;
    listen [::]:8443 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_client_access.log;
    error_log /var/log/nginx/softswitch_bss_client_error.log;

    # ── CARRIER PERIMETER FILTER: WHATEVER IS NOT CLIENT SCOPED IS FORBIDDEN ──
    # Block back-office administration routes from being called through the public portal
    location ~* ^/api/(?!(?:v1/)?(?:client(?:/|$)|lnp/client(?:/|$)|branding(?:/|$)|languages(?:/|$)|health(?:/|$)|payments/webhooks/stripe(?:/|$))).+ {
        default_type application/json;
        return 403 '{"error":"Forbidden: Administrative APIs are not accessible via customer perimeter"}';
    }

    # ── 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."}';
    }

    # Allowed Customer Self-Care API Proxy
    # Note: If API is on a remote server in multi-server topology, replace 127.0.0.1:3002 with http://<bss-api-ip>: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_read_timeout 300s;
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        client_max_body_size 50M;
    }

    # Custom Branding Assets Proxy (dynamically updated by BSS_API_UPSTREAM in multi-server topology)
    location ^~ /assets/branding/ {
        proxy_pass http://127.0.0.1:3002/assets/branding/;
        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;
    }

    # PWA Service Worker for Customer Portal (/portal/ and root /)
    location = /portal/sw.js {
        alias /var/www/softswitch/bss/client/sw.js;
        expires -1;
        add_header Cache-Control "no-store, no-cache, must-revalidate, max-age=0" always;
        add_header Service-Worker-Allowed "/portal/" always;
        add_header X-Robots-Tag "none" always;
    }
    location = /sw.js {
        root /var/www/softswitch/bss/client;
        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;
    }

    # Customer Store & Self-Care SPA Static Assets (/portal/assets/ avoids Nginx alias+try_files fallback bug)
    location ^~ /portal/assets/ {
        alias /var/www/softswitch/bss/client/assets/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # Customer Store & Self-Care SPA (/portal/ base path)
    location ^~ /portal/ {
        alias /var/www/softswitch/bss/client/;
        index index.html;
        try_files $uri $uri/ /portal/index.html;
    }

    # Customer Store & Self-Care SPA
    location / {
        root /var/www/softswitch/bss/client;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}
