server {
    listen 80;
    server_name _;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name _;
    root /usr/share/nginx/html;
    index index.html;

    ssl_certificate     /etc/nginx/ssl/cert.crt;
    ssl_certificate_key /etc/nginx/ssl/cert.key;
    ssl_protocols       TLSv1.2 TLSv1.3;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    # Upload-Limit (Shares bis 100 MB, base64-kodierte bis ~35 MB original)
    client_max_body_size 110m;

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

    # React Router Support
    location / {
        try_files $uri $uri/ /index.html;
    }

    # SSE – Netzwerk-Monitoring
    location = /api/network-monitor/sse {
        proxy_pass http://backend:5000/api/network-monitor/sse;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 86400s;
        chunked_transfer_encoding on;
        add_header X-Accel-Buffering "no";
    }

    # SSE (Server-Sent Events) – kein Buffering, lange Verbindungen
    location ~ ^/api/tickets/[0-9]+/events {
        proxy_pass http://backend:5000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 86400s;
        chunked_transfer_encoding on;
        add_header X-Accel-Buffering "no";
    }

    # Health Check API Proxy zum Backend (nur für API-Aufrufe, nicht Browser-Navigation)
    location = /api/health {
        proxy_pass http://backend:5000/health;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Uploads (Bilder, Anhänge) vom Backend ausliefern
    location ^~ /uploads/ {
        proxy_pass http://backend:5000/uploads/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        expires 7d;
        add_header Cache-Control "public";
    }

    # Agent-Downloads (Installer, intunewin)
    location ^~ /downloads/ {
        proxy_pass http://backend:5000/downloads/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        add_header Content-Disposition "attachment";
        add_header Cache-Control "no-cache";
    }

    # Crawl-Import – langer Timeout (bis zu 5 Minuten)
    location = /api/ai/knowledge-base/import-crawl {
        proxy_pass http://backend:5000/api/ai/knowledge-base/import-crawl;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }

    # API Proxy zum Backend
    location /api/ {
        proxy_pass http://backend:5000/api/;
        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_cache_bypass $http_upgrade;
    }

    # Cache für statische Assets
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Keine Cache für index.html
    location = /index.html {
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        expires 0;
    }

    # 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;
}
