# ============================================================
# Spanglish Community - Frontend
# spanglishcommunity.com / www
# ============================================================

server {
    listen 80;
    server_name spanglishcommunity.com www.spanglishcommunity.com;

    # ACME
    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    # Force HTTPS
    location / {
        return 301 https://spanglishcommunity.com$request_uri;
    }
}

server {
    listen 443 ssl;
    http2 on;

    server_name spanglishcommunity.com www.spanglishcommunity.com;

    # Upload size limit (covers same-origin /api uploads via this vhost)
    client_max_body_size 20m;

    # SSL
    ssl_certificate /etc/letsencrypt/live/spanglishcommunity.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/spanglishcommunity.com/privkey.pem;

    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # Security
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    # Logs
    access_log /var/log/nginx/spanglish_frontend_access.log;
    error_log  /var/log/nginx/spanglish_frontend_error.log;

    # LNbits payment SSE stream - must not be buffered or events won't flush in
    # real time. Regex location takes precedence over the /api prefix below.
    location ~ ^/api/lnbits/stream/ {
        proxy_pass http://spanglish_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_set_header Connection '';

        # Disable buffering/caching for Server-Sent Events
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600s;
        proxy_connect_timeout 300s;
    }

    # Proxy /api to backend
    location /api {
        proxy_pass http://spanglish_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 300s;
        proxy_connect_timeout 300s;
    }

    # Proxy /uploads to backend
    location /uploads {
        proxy_pass http://spanglish_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;

        # Cache static files
        proxy_cache_valid 200 1d;
        expires 1d;
        add_header Cache-Control "public, immutable";
    }

    # Frontend App
    location / {
        proxy_pass http://spanglish_frontend;
        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;

        # WebSocket / HMR
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 60s;
        proxy_connect_timeout 60s;
    }
}
