# ============================================================
# torbatyar.ir HTTPS — enable after certbot issues certificates
# Certbot (HTTP-01) covers apex + service subdomains.
# Wildcard *.torbatyar.ir needs DNS-01 (Cloudflare) — see deploy notes.
# ============================================================

# ---------- Apex + www → Frontend (HTTPS) ----------
server {
    listen 192.168.10.156:443 ssl;
    server_name torbatyar.ir www.torbatyar.ir;

    ssl_certificate     /etc/letsencrypt/live/torbatyar.ir/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /api/core/ {
        proxy_pass http://torbatyar_core/;
        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 600s;
        client_max_body_size 50m;
    }

    location / {
        proxy_pass http://torbatyar_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 Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
        client_max_body_size 50m;
    }
}

# ---------- Tenant wildcards (requires wildcard cert) ----------
# Uncomment after: certbot certonly --dns-cloudflare -d '*.torbatyar.ir' -d torbatyar.ir
# server {
#     listen 192.168.10.156:443 ssl;
#     server_name *.torbatyar.ir;
#     ssl_certificate     /etc/letsencrypt/live/torbatyar.ir-wildcard/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir-wildcard/privkey.pem;
#     include /etc/letsencrypt/options-ssl-nginx.conf;
#     ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
#     location / {
#         proxy_pass http://torbatyar_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 Upgrade $http_upgrade;
#         proxy_set_header Connection "upgrade";
#         proxy_read_timeout 600s;
#         client_max_body_size 50m;
#     }
# }

server {
    listen 192.168.10.156:443 ssl;
    server_name api.torbatyar.ir;

    ssl_certificate     /etc/letsencrypt/live/api.torbatyar.ir/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/api.torbatyar.ir/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://torbatyar_core;
        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 600s;
        client_max_body_size 50m;
    }
}

server {
    listen 192.168.10.156:443 ssl;
    server_name identity.torbatyar.ir;

    ssl_certificate     /etc/letsencrypt/live/identity.torbatyar.ir/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/identity.torbatyar.ir/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://torbatyar_identity;
        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 600s;
        client_max_body_size 20m;
    }
}

server {
    listen 192.168.10.156:443 ssl;
    server_name auth.torbatyar.ir;

    ssl_certificate     /etc/letsencrypt/live/auth.torbatyar.ir/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/auth.torbatyar.ir/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://torbatyar_keycloak;
        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_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;
        proxy_read_timeout 600s;
        client_max_body_size 20m;
    }
}
