# ==============================================
# Nginx в контейнере (PROD: порт 8084 на хосте).
# Upstream — по имени сервиса: web:8000, front_material:3000 (внутри сети).
# ==============================================

# API Backend (Django) — default_server: сюда попадают запросы на localhost и api.localhost
server {
    listen 80 default_server;
    server_name api.localhost localhost;
    
    charset utf-8;
    client_max_body_size 100M;

    # ==============================================
    # API ENDPOINTS
    # ==============================================
    location /api/ {
        limit_req zone=api_limit burst=20 nodelay;
        
        proxy_pass http://django;
        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_set_header Connection "";
        
        proxy_connect_timeout 300s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }

    # ==============================================
    # ADMIN PANEL
    # ==============================================
    location /admin/ {
        proxy_pass http://django;
        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;
    }

    # ==============================================
    # WEBSOCKET CONNECTIONS
    # ==============================================
    location /ws/ {
        proxy_pass http://django;
        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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }

    # Запрет выполнения скриптов в static и media
    location ~* ^/(static|media)/.*\.(php|pl|py|jsp|asp|sh|cgi)$ {
        deny all;
        access_log off;
    }

    # ==============================================
    # СТАТИЧЕСКИЕ ФАЙЛЫ — содержимое STATIC_ROOT (staticfiles) из volume
    # Web пишет в /app/staticfiles (volume), nginx читает тот же volume как /var/www/static
    # ==============================================
    location /static/ {
        alias /var/www/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
        add_header X-Content-Type-Options "nosniff" always;
        access_log off;
    }

    # ==============================================
    # МЕДИА ФАЙЛЫ — содержимое MEDIA_ROOT (media) из volume
    # ==============================================
    location /media/ {
        alias /var/www/media/;
        expires 7d;
        add_header Cache-Control "public";
        add_header X-Content-Type-Options "nosniff" always;
    }

    # ==============================================
    # LIVEKIT - видеоконференции (официальный Go-сервер)
    # Всё проходит через наш сервис
    # ==============================================
    # SDK livekit-client при ошибке WS делает GET /rtc/v1/validate для понятной ошибки.
    # Официальный сервер LiveKit этот HTTP endpoint не отдаёт (404). Отвечаем 200 сами.
    # location = /livekit/rtc/v1/validate {
    #     add_header Content-Type application/json;
    #     return 200 '{}';
    # }
    # location /livekit {
    #     proxy_pass http://livekit/;
    #     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_set_header Upgrade $http_upgrade;
    #     proxy_set_header Connection "upgrade";
    #     proxy_read_timeout 86400s;
    #     proxy_send_timeout 86400s;
    # }

    # ==============================================
    # HEALTH CHECK
    # ==============================================
    location /health/ {
        proxy_pass http://django;
        access_log off;
    }
}

# Frontend (Next.js) — app.localhost и app.uchill.online
server {
    listen 80;
    server_name app.localhost app.uchill.online;
    
    charset utf-8;

    location / {
        limit_req zone=general_limit burst=50 nodelay;
        
        proxy_pass http://frontend;
        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_set_header Connection "";
        
        # Для Hot Module Replacement (HMR) в development
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # Next.js static files
    location /_next/static/ {
        proxy_pass http://frontend;
        expires 365d;
        add_header Cache-Control "public, immutable";
        access_log off;
    }
}

