# staging.rebreak.org -- Marketing SPA als Root, Backend-API unter /api/, /webhook, /api/sse
#
# Layout post-marketing-deploy:
#   /                    -> Marketing SPA (statisch, /var/www/marketing-staging)
#   /api/*               -> Backend Nitro (127.0.0.1:3016)
#   /webhook             -> Webhook-Listener (127.0.0.1:9000)
#   /api/sse             -> Backend Nitro SSE (lange Timeouts, kein Buffering)
#
# Deploy Marketing:  scripts/deploy-marketing.sh
# Deploy Backend:    git push -> webhook -> scripts/deploy.sh

server {
    listen 80;
    server_name staging.rebreak.org;

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

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

server {
    listen 443 ssl;
    server_name staging.rebreak.org;

    ssl_certificate /etc/letsencrypt/live/staging.rebreak.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/staging.rebreak.org/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options DENY;
    add_header X-XSS-Protection "1; mode=block";
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

    # --- Backend-Routen (gehen VOR der Marketing-Location, weil spezifischer) ---

    location /webhook {
        proxy_pass http://127.0.0.1:9000/webhook;
        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;
        client_max_body_size 10M;
        proxy_read_timeout 60s;
    }

    location ~* ^/api/sse {
        proxy_pass http://127.0.0.1:3016;
        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 '';
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        chunked_transfer_encoding on;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:3016;
        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 Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 300s;
        proxy_connect_timeout 60s;
        client_max_body_size 50M;
    }

    # --- Marketing SPA (statisch, Nuxt generate output) ---

    root /var/www/marketing-staging;
    index index.html;

    # Long-cache fuer hashed assets (_nuxt/*, fonts, images)
    location /_nuxt/ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    location / {
        try_files $uri $uri/ $uri.html /index.html;
    }
}
