# LiveKit — tek alan adı: novazeka.com.tr (ayrı livekit.* alt alanı GEREKMEZ)
#
# Kullanım A) Bu dosyayı server bloğuna dahil edin:
#   include /etc/nginx/snippets/nginx-livekit-novazeka-root.snippet.conf;
# Kullanım B) İçeriği novazeka.com.tr:443 server { } içine kopyalayın.
#
# http { } içinde bir kez (yoksa):
#   map $http_upgrade $connection_upgrade {
#       default upgrade;
#       ''      close;
#   }
#
# CORS: harici map dosyası gerekmez — Access-Control-Allow-Origin = tarayıcı Origin başlığı (localhost dahil).
#
# Sertifika: Mevcut novazeka.com.tr Let's Encrypt fullchain.pem yeterli.
# PHP: LIVEKIT_URL=wss://novazeka.com.tr
#
# ÖNEMLİ: Bu snippet, aynı server { } içinde "location /" ve PHP kurallarından ÖNCE include edilmeli.

large_client_header_buffers 8 64k;
client_header_buffer_size 16k;

# wss://novazeka.com.tr/rtc — Flutter web; GET .../rtc/validate (CORS gerekir)
location ^~ /rtc {
    # CORS: Flutter web (localhost) → https://novazeka.com.tr/rtc/validate preflight
    # Not: Preflight genelde Access-Control-Request-Headers gönderir; mümkünse aynen yansıt.
    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin $http_origin always;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
        add_header Access-Control-Allow-Headers $http_access_control_request_headers always;
        add_header Access-Control-Allow-Credentials 'true' always;
        add_header Vary 'Origin' always;
        add_header Access-Control-Max-Age 86400 always;
        return 204;
    }

    proxy_pass http://127.0.0.1:7880;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    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 86400;
    proxy_buffering off;

    add_header Access-Control-Allow-Origin $http_origin always;
    add_header Access-Control-Allow-Credentials 'true' always;
    add_header Vary 'Origin' always;
    add_header Access-Control-Expose-Headers 'Grpc-Status, Grpc-Message, Grpc-Encoding, Grpc-Accept-Encoding' always;
}

# https://novazeka.com.tr/twirp/... — PHP (RoomService / Egress); tarayıcıdan gerekirse CORS
location ^~ /twirp {
    if ($request_method = OPTIONS) {
        add_header Access-Control-Allow-Origin $http_origin always;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS' always;
        add_header Access-Control-Allow-Headers $http_access_control_request_headers always;
        add_header Access-Control-Allow-Credentials 'true' always;
        add_header Vary 'Origin' always;
        add_header Access-Control-Max-Age 86400 always;
        return 204;
    }

    proxy_pass http://127.0.0.1:7880;
    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;
    # StopEgress / uzun Twirp — 300s yetmezse 408 deadline_exceeded (proxy + PHP curl artırıldı)
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffering off;

    add_header Access-Control-Allow-Origin $http_origin always;
    add_header Access-Control-Allow-Credentials 'true' always;
    add_header Vary 'Origin' always;
    add_header Access-Control-Expose-Headers 'Grpc-Status, Grpc-Message, Grpc-Encoding, Grpc-Accept-Encoding' always;
}
