# Voice AI - Nginx Configuration for app3.syntheon.in
#
# Routes:
# - https://app3.syntheon.in/           -> frontend (Vite preview in container on :3000)
# - https://app3.syntheon.in/master/    -> masterpanel (Vite preview in container on :4174)
# - https://app3.syntheon.in/api/mcube/ -> MCube proxy (:8088) (longest-prefix before /api/)
# - https://app3.syntheon.in/api/       -> Django backend (:8000)
# - https://app3.syntheon.in/webhooks/  -> MCube proxy (:8088) -> webhook_server (:8002)
# - https://app3.syntheon.in/ws/        -> MCube proxy (:8088) -> ws_bridge (:9001)  (WebSocket)
# - https://app3.syntheon.in/bid/websocket/ -> MCube proxy (:8088) -> ws_bridge (:9001) (WebSocket)
#
# INSTALLATION (Ubuntu/Debian):
# 1) sudo cp nginx-app3-syntheon.conf /etc/nginx/sites-available/app3-syntheon
# 2) sudo ln -s /etc/nginx/sites-available/app3-syntheon /etc/nginx/sites-enabled/
# 3) sudo nginx -t && sudo systemctl reload nginx
# 4) SSL: sudo certbot --nginx -d app3.syntheon.in

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name app3.syntheon.in;

    client_max_body_size 25M;

    # ----------------------------
    # MCube outbound JSON API -> proxy :8088 (must be more specific than /api/)
    # ----------------------------
    location /api/mcube/ {
        proxy_pass http://127.0.0.1:8088;
        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_read_timeout 300;
        proxy_connect_timeout 60;
        proxy_send_timeout 300;
    }

    # ----------------------------
    # Django API
    # ----------------------------
    location /api/ {
        proxy_pass http://127.0.0.1:8000;
        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;

        # If your API uses websockets anywhere under /api
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_read_timeout 300;
        proxy_connect_timeout 60;
        proxy_send_timeout 300;
    }

    # ----------------------------
    # MCube proxy (one public port: 8088)
    # ----------------------------
    location /webhooks/ {
        proxy_pass http://127.0.0.1:8088;
        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 $connection_upgrade;

        proxy_read_timeout 300;
        proxy_connect_timeout 60;
        proxy_send_timeout 300;
    }

    location /ws/ {
        proxy_pass http://127.0.0.1:8088;
        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 $connection_upgrade;

        proxy_read_timeout 86400;
        proxy_connect_timeout 60;
        proxy_send_timeout 60;
    }

    location /bid/websocket/ {
        proxy_pass http://127.0.0.1:8088;
        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 $connection_upgrade;

        proxy_read_timeout 86400;
        proxy_connect_timeout 60;
        proxy_send_timeout 60;
    }

    # ----------------------------
    # Masterpanel UI
    # ----------------------------
    location /master/ {
        proxy_pass http://127.0.0.1:4174/;
        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 $connection_upgrade;

        proxy_read_timeout 86400;
    }

    # ----------------------------
    # Frontend UI (default)
    # ----------------------------
    location / {
        proxy_pass http://127.0.0.1:3000;
        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 $connection_upgrade;

        proxy_read_timeout 86400;
    }
}

