# Mitai Jinkendo - nginx Production Config
# Place at: /etc/nginx/sites-available/jinkendo

server {
    listen 80;
    server_name mitai.jinkendo.de;

    # Let's Encrypt challenge
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    # Redirect all HTTP to HTTPS
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl http2;
    server_name mitai.jinkendo.de;

    # SSL - managed by Certbot
    ssl_certificate     /etc/letsencrypt/live/mitai.jinkendo.de/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mitai.jinkendo.de/privkey.pem;
    include             /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam         /etc/letsencrypt/ssl-dhparams.pem;

    # Security Headers
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    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;

    # Gzip
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
    gzip_min_length 1000;

    # Rate limiting zones (defined in http block)
    # limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;
    # limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;

    # API - Backend
    location /api/ {
        # limit_req zone=api burst=20 nodelay;
        proxy_pass         http://127.0.0.1:8002;
        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 120s;  # KI-Calls können länger dauern
        client_max_body_size 20M; # CSV + Foto Uploads
    }

    # Frontend - React PWA
    location / {
        proxy_pass         http://127.0.0.1:3002;
        proxy_http_version 1.1;
        proxy_set_header   Host $host;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection 'upgrade';
        proxy_cache_bypass $http_upgrade;
    }
}
