apiVersion: v1
kind: ConfigMap
metadata:
  name: {{.Release.Name}}-nginx-conf-cm
  labels:
    env: {{.Values.env}}
data:
  default.conf: |
    server {
        listen 8080;
        server_name ~^(?<subdomain>[^.]+)\.jb-guide-dev\.labs\.jb\.gg$;

        resolver 8.8.8.8;  # Use Google's public DNS server
        resolver_timeout 5s;  # Timeout for DNS resolution

        location = /info/health-check/ {
          add_header Content-Type text/plain;
          return 200 "OK";
        }

        location / {

          # Handle /guide/ specifically
            location = /guide/ {
            rewrite ^ /$subdomain/guide/index.html last;
          }

          # Handle /guide without trailing slash
            location = /guide {
            rewrite ^ /$subdomain/guide/index.html last;
          }

            # Match URLs with a trailing slash
            if ($request_uri ~ ^/guide/(.+)/$) {
                rewrite ^/guide/(.+)/$ /$subdomain/guide/$1/index.html last;
            }
            # Match URLs without a trailing slash
            if ($request_uri ~ ^/guide/(.+)$) {
                rewrite ^/guide/(.+)$ /$subdomain/guide/$1/index.html last;
            }

            # Handle requests for files (including any file extension)
            location ~ ^/guide/(.+\..+)$ {
                rewrite ^/guide/(.+\..+)$ /$subdomain/guide/$1 last;
            }

            proxy_pass https://jb-guide-prod-guide-static-atsaxr.storage.googleapis.com;
            proxy_set_header Host jb-guide-prod-guide-static-atsaxr.storage.googleapis.com;
            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;
        }

    }

