# ============================================================
# TrackSteel App — Nginx Configuration
# SPA routing + performance + segurança
# ============================================================

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

    # ---- Compressão Gzip ----
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types
        text/plain
        text/css
        text/javascript
        application/javascript
        application/json
        application/xml
        image/svg+xml
        font/woff2;

    # ---- Headers de Segurança ----
    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;

    # ---- Cache de Assets Estáticos ----
    # Arquivos com hash no nome (JS, CSS gerados pelo Vite) — cache longo
    location ~* \.(?:js|css)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        try_files $uri =404;
    }

    # Fontes e imagens — cache médio
    location ~* \.(?:woff2?|ttf|eot|otf|ico|png|jpg|jpeg|gif|svg|webp)$ {
        expires 6M;
        add_header Cache-Control "public";
        try_files $uri =404;
    }

    # ---- SPA Routing ----
    # Qualquer rota que não corresponda a um arquivo redireciona pro index.html
    location / {
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
    }

    # ---- Bloquear acesso a arquivos sensíveis ----
    location ~ /\. {
        deny all;
        return 404;
    }

    # ---- Health Check ----
    location /health {
        access_log off;
        return 200 "OK";
        add_header Content-Type text/plain;
    }
}
