server {
    listen 80;
    server_name _;

    client_max_body_size 50M;
    error_log  /var/log/nginx/error.log warn;
    access_log /var/log/nginx/access.log;

    root /var/www/public;
    index index.php;

    # --------------------------------------------------
    # CORS para API (Flutter Web)
    # --------------------------------------------------
    location /api/ {
        # Preflight CORS
        if ($request_method = OPTIONS) {
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, PATCH, DELETE, OPTIONS' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Accept' always;
            add_header 'Access-Control-Max-Age' 86400;
            return 204;
        }
        try_files $uri $uri/ /index.php?$query_string;
    }

    # --------------------------------------------------
    # Resto do site (inalterado)
    # --------------------------------------------------
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # --------------------------------------------------
    # Server-Sent Events (Realtime)
    # --------------------------------------------------
    location /realtime/ {
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600;
        proxy_send_timeout 3600;
        chunked_transfer_encoding off;
    }
    
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass cliente:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

}