# =============================================================================
# Nginx Configuration - ISP Billing System
# Domain: isp.ngaredigital.com
# Serves: Frontend apps + API proxy
# =============================================================================

# HTTP → HTTPS redirect
server {
    listen 80;
    server_name isp.ngaredigital.com;

    # Let's Encrypt challenge
    location /.well-known/acme-challenge/ {
        root /var/www/isp.ngaredigital.com/frontend;
    }

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

# HTTPS server
server {
    listen 443 ssl http2;
    server_name isp.ngaredigital.com;

    # SSL certificates (will be installed by certbot)
    ssl_certificate     /etc/letsencrypt/live/isp.ngaredigital.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/isp.ngaredigital.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # Increase body size for file uploads
    client_max_body_size 10M;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/rss+xml font/truetype font/opentype application/font-woff image/svg+xml;

    # ---- STATIC FRONTEND APPS ----

    # Admin Portal (system administrators)
    location /admin {
        alias /var/www/isp.ngaredigital.com/frontend/admin;
        index index.html;
        try_files $uri $uri/ /admin/index.html;
    }

    # Business Dashboard (business owners, managers, etc.)
    location /dashboard {
        alias /var/www/isp.ngaredigital.com/frontend/dashboard;
        index index.html;
        try_files $uri $uri/ /dashboard/index.html;
    }

    # Customer Portal
    location /customer {
        alias /var/www/isp.ngaredigital.com/frontend/customer;
        index index.html;
        try_files $uri $uri/ /customer/index.html;
    }

    # Captive Portal (WiFi login page)
    location /portal {
        alias /var/www/isp.ngaredigital.com/frontend/portal;
        index index.html;
        try_files $uri $uri/ /portal/index.html;
    }

    # Root redirect to admin (or dashboard if preferred)
    location = / {
        return 301 /admin;
    }

    # ---- API PROXY ----
    location /api/ {
        proxy_pass http://127.0.0.1:3000/api/;
        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_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port 443;

        # Timeouts for slow M-Pesa etc.
        proxy_read_timeout 120s;
        proxy_connect_timeout 10s;
        proxy_send_timeout 120s;
    }

    # ---- UPLOADS ----
    location /uploads/ {
        alias /var/www/isp.ngaredigital.com/uploads/;
        expires 30d;
        add_header Cache-Control "public, immutable";
        # Deny direct execution
        location ~* \.(php|pl|py|jsp|asp|sh|cgi)$ {
            deny all;
        }
    }

    # ---- SECURITY RESTRICTIONS ----
    # Deny access to hidden files
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Deny access to sensitive files
    location ~* \.(env|log|sql|md|yml|yaml|json.example)$ {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Remove trailing slash for frontend apps
    rewrite ^/admin/$ /admin permanent;
    rewrite ^/dashboard/$ /dashboard permanent;
    rewrite ^/customer/$ /customer permanent;
    rewrite ^/portal/$ /portal permanent;

    # ---- LOGGING ----
    access_log /var/www/isp.ngaredigital.com/logs/nginx-access.log;
    error_log  /var/www/isp.ngaredigital.com/logs/nginx-error.log warn;
}
