# ============================================================
# torbatyar.ir HTTPS — cert: /etc/letsencrypt/live/torbatyar.ir/
# ساب‌دامین tenant: گواهی با provision_ssl.py به‌صورت خودکار expand می‌شود.
# ============================================================

include /etc/nginx/conf.d/torbatyar-tenant-ssl.map;

upstream torbatyar_frontend {
    server 192.168.10.162:3000;
    keepalive 16;
}
upstream torbatyar_core {
    server 192.168.10.162:8000;
    keepalive 16;
}
upstream torbatyar_identity {
    server 192.168.10.162:8001;
    keepalive 16;
}
upstream torbatyar_keycloak {
    server 192.168.10.162:8080;
    keepalive 8;
}
upstream torbatyar_accounting {
    server 192.168.10.162:8002;
    keepalive 16;
}
upstream torbatyar_crm {
    server 192.168.10.162:8003;
    keepalive 16;
}
upstream torbatyar_loyalty {
    server 192.168.10.162:8004;
    keepalive 16;
}
upstream torbatyar_healthcare {
    server 192.168.10.162:8010;
    keepalive 16;
}
upstream torbatyar_beauty {
    server 192.168.10.162:8011;
    keepalive 16;
}
upstream torbatyar_hospitality {
    server 192.168.10.162:8009;
    keepalive 16;
}

# Force HTTPS for platform hosts
server {
    listen 192.168.10.156:80;
    server_name torbatyar.ir www.torbatyar.ir api.torbatyar.ir identity.torbatyar.ir auth.torbatyar.ir accounting.torbatyar.ir crm.torbatyar.ir loyalty.torbatyar.ir healthcare.torbatyar.ir beauty.torbatyar.ir hospitality.torbatyar.ir;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# Tenant subdomains: HTTP تا وقتی SSL آماده شود؛ بعد redirect به HTTPS
server {
    listen 192.168.10.156:80;
    server_name *.torbatyar.ir;

    location /.well-known/acme-challenge/ {
        root /var/www/html;
    }

    location / {
        if ($torbatyar_tenant_ssl = 1) {
            return 301 https://$host$request_uri;
        }
        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;
    }
}

# Frontend + همه ساب‌دامین‌های tenant (SNI؛ گواهی باید SAN داشته باشد)
server {
    listen 192.168.10.156:443 ssl;
    server_name torbatyar.ir www.torbatyar.ir *.torbatyar.ir;

    ssl_certificate     /etc/letsencrypt/live/torbatyar.ir-0001/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir-0001/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;
        proxy_send_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/torbatyar.ir-0001/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir-0001/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/torbatyar.ir-0001/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir-0001/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/torbatyar.ir-0001/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/torbatyar.ir-0001/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;
    }
}

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

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

    location / {
        proxy_pass http://torbatyar_accounting;
        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 crm.torbatyar.ir;

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

    location / {
        proxy_pass http://torbatyar_crm;
        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 loyalty.torbatyar.ir;

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

    location / {
        proxy_pass http://torbatyar_loyalty;
        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 healthcare.torbatyar.ir;

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

    location / {
        proxy_pass http://torbatyar_healthcare;
        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 beauty.torbatyar.ir;

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

    location / {
        proxy_pass http://torbatyar_beauty;
        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 hospitality.torbatyar.ir;

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

    location / {
        proxy_pass http://torbatyar_hospitality;
        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;
    }
}
