{{- if and .Values.enabled .Values.apiGatewayApplication.enabled }}
{{- $namespace := .Release.Namespace -}}
{{- $TenantEnvironment := .Values.global.domain.TenantEnvironment -}}
{{- $DeployDomainName := ternary (first (regexSplit ":" .Values.global.domain.DeployDomainName -1)) ( .Values.global.domain.DeployDomainName ) (contains ":" .Values.global.domain.DeployDomainName) -}}
{{- $IsPrivateDeployment := .Values.global.domain.IsPrivateDeployment -}}
{{- $IsTlsEnables := .Values.global.ingress.tls_enabled -}}
{{- $scope := $ -}}
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: all-gateways-ingress
  namespace: {{ $namespace }}
  labels:
    type: {{ $.Values.apiGatewayApplication.type }}
    accessmode: {{ $.Values.apiGatewayApplication.accessmode }}
    helm.sh/chart: {{ include "all-ingress.name" $scope }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
    {{- if $.Values.global.image.tag }}
    app.kubernetes.io/version: {{ $.Values.global.image.tag | quote }}
    {{- end }}
  annotations:
    {{- include "all-ingress.frontend.commom.annotations" $scope | nindent 4 }}
    {{- if $IsTlsEnables }}
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/permanent-redirect-code: '301'
    {{- end }}
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
spec:
  rules:
    - host: {{ $DeployDomainName }}
      http:
        paths:
          {{- if $IsPrivateDeployment }}
          {{- range $key,$value := .Values.apiGatewayApplication.manifest }}
          {{- if eq $value false }}
          {{- $applicationName := ternary (trimPrefix "cmii-uav-" $key) (trimPrefix "cmii-" $key) (contains "cmii-uav" $key) -}}
          {{- if eq $applicationName "material-warehouse" }}
          - path: /api/warehouses/?(.*)
          {{- else if eq $applicationName "gateway" }}
          - path: /api/?(.*)
          {{- else if eq $applicationName "admin-gateway" }}
          - path: /oms/api/?(.*)
{{- /*          {{- else if eq $applicationName "project-minio" }}*/}}
{{- /*          - path: /?(.*)/api/minios/?(.*)*/}}
          {{- else if eq $applicationName "open-gateway" }}
          - path: /open/api/?(.*)
          {{- else }}
          - path: /{{ $applicationName }}/?(.*)
            {{- end }}
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 8080
          {{- end }}
          {{- end }}
          {{- else }}
          {{- range $key,$value := .Values.apiGatewayApplication.manifest }}
          {{- if eq $value false }}
          {{- $applicationName := ternary (trimPrefix "cmii-uav-" $key) (trimPrefix "cmii-" $key) (contains "cmii-uav" $key) -}}
          {{- if eq $applicationName "material-warehouse" }}
          - path: /{{ $TenantEnvironment }}/api/warehouses/?(.*)
          {{- else if eq $applicationName "gateway" }}
          - path: /{{ $TenantEnvironment }}/api/?(.*)
          {{- else if eq $applicationName "admin-gateway" }}
          - path: /{{ $TenantEnvironment }}/oms/api/?(.*)
{{- /*          {{- else if eq $applicationName "project-minio" }}*/}}
{{- /*          - path: /{{ $TenantEnvironment }}/?(.*)/api/minios/?(.*)*/}}
          {{- else if eq $applicationName "open-gateway" }}
          - path: /{{ $TenantEnvironment }}/open/api/?(.*)
          {{- else }}
          - path: /{{ $applicationName }}/?(.*)
            {{- end }}
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 8080
          {{- end }}
          {{- end }}
  {{- end }}
  {{- if $IsTlsEnables }}
  tls:
    - hosts:
        - {{ $DeployDomainName }}
    - secretName: {{ $DeployDomainName | quote }}
  {{- end }}

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: all-apis-ingress
  namespace: {{ $namespace }}
  labels:
    type: {{ $.Values.apiGatewayApplication.type }}
    accessmode: {{ $.Values.apiGatewayApplication.accessmode }}
    helm.sh/chart: {{ include "all-ingress.name" $scope }}
    app.kubernetes.io/managed-by: {{ $.Release.Service }}
    {{- if $.Values.global.image.tag }}
    app.kubernetes.io/version: {{ $.Values.global.image.tag | quote }}
    {{- end }}
  annotations:
    {{- include "all-ingress.all-apis.commom.annotations" $scope | nindent 4 }}
    {{- if $IsTlsEnables }}
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/permanent-redirect-code: '301'
    {{- end }}
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
spec:
  rules:
    - host: {{ $DeployDomainName }}
      http:
        paths:
          {{- if $IsPrivateDeployment }}
          {{- range $key,$value := .Values.apiGatewayApplication.manifest }}
          {{- $applicationName := ternary (trimPrefix "cmii-uav-" $key) (trimPrefix "cmii-" $key) (contains "cmii-uav" $key) -}}
          {{- if $value }}
          {{- if eq $applicationName "material-warehouse" }}
          - path: /?(.*)/api/warehouses/?(.*)
          {{- else if eq $applicationName "gateway" }}
          - path: /api/?(.*)
          {{- else if eq $applicationName "admin-gateway" }}
          - path: /oms/api/?(.*)
{{- /*          {{- else if eq $applicationName "project-minio" }}*/}}
{{- /*          - path: /?(.*)/api/minios/?(.*)*/}}
          {{- else if eq $applicationName "open-gateway" }}
          - path: /open/api/?(.*)
          {{- else }}
          - path: /{{ $applicationName }}/?(.*)
          {{- end }}
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 8080
          {{- end }}
          {{- end }}
          {{- else }}
          {{- range $key,$value := .Values.apiGatewayApplication.manifest }}
          {{- if $value }}
          {{- $applicationName := ternary (trimPrefix "cmii-uav-" $key) (trimPrefix "cmii-" $key) (contains "cmii-uav" $key) -}}
          {{- if eq $applicationName "material-warehouse" }}
          - path: /{{ $TenantEnvironment }}/?(.*)/api/warehouses/?(.*)
          {{- else if eq $applicationName "gateway" }}
          - path: /{{ $TenantEnvironment }}/api/?(.*)
          {{- else if eq $applicationName "admin-gateway" }}
          - path: /{{ $TenantEnvironment }}/oms/api/?(.*)
{{- /*          {{- else if eq $applicationName "project-minio" }}*/}}
{{- /*          - path: /{{ $TenantEnvironment }}/?(.*)/api/minios/?(.*)*/}}
          {{- else if eq $applicationName "open-gateway" }}
          - path: /{{ $TenantEnvironment }}/open/api/?(.*)
          {{- else }}
          - path: /{{ $applicationName }}/?(.*)
            {{- end }}
            pathType: ImplementationSpecific
            backend:
              serviceName: {{ $key }}
              servicePort: 8080
          {{- end }}
          {{- end }}
  {{- end }}
  {{- if $IsTlsEnables }}
  tls:
    - hosts:
        - {{ $DeployDomainName }}
    - secretName: {{ $DeployDomainName | quote }}
  {{- end }}
---
{{- end }}
{{/*apiVersion: networking.k8s.io/v1beta1*/}}
{{/*kind: Ingress*/}}
{{/*metadata:*/}}
{{/*  name: project-minio-ingress*/}}
{{/*  namespace: {{ $namespace }}*/}}
{{/*  labels:*/}}
{{/*    type: {{ $.Values.apiGatewayApplication.type }}*/}}
{{/*    accessmode: {{ $.Values.apiGatewayApplication.accessmode }}*/}}
{{/*    helm.sh/chart: {{ include "uavcloud-ingress.name" $scope }}*/}}
{{/*    app.kubernetes.io/managed-by: {{ $.Release.Service }}*/}}
{{/*    {{- if $.Values.global.image.tag }}*/}}
{{/*    app.kubernetes.io/version: {{ $.Values.global.image.tag | quote }}*/}}
{{/*    {{- end }}*/}}
{{/*  annotations:*/}}
{{/*    {{- include "uavcloud-ingress.apiGateway.project-minio.annotations" $scope | nindent 4 }}*/}}
{{/*    {{- if $IsTlsEnables }}*/}}
{{/*    nginx.ingress.kubernetes.io/ssl-redirect: "true"*/}}
{{/*    nginx.ingress.kubernetes.io/permanent-redirect-code: '301'*/}}
{{/*    {{- end }}*/}}
{{/*    nginx.ingress.kubernetes.io/configuration-snippet: |*/}}
{{/*      proxy_set_header Upgrade $http_upgrade;*/}}
{{/*      proxy_set_header Connection "Upgrade";*/}}
{{/*spec:*/}}
{{/*  rules:*/}}
{{/*    - host: {{ $DeployDomainName }}*/}}
{{/*      http:*/}}
{{/*        paths:*/}}
{{/*          {{- if $IsPrivateDeployment }}*/}}
{{/*          - path: /?(.*)/api/minio/?(.*)*/}}
{{/*          {{- else }}*/}}
{{/*          - path: /{{ $TenantEnvironment }}/?(.*)/api/minio/?(.*)*/}}
{{/*          {{- end }}*/}}
{{/*            pathType: ImplementationSpecific*/}}
{{/*            backend:*/}}
{{/*              serviceName: cmii-project-minio*/}}
{{/*              servicePort: 8080*/}}
{{/*  {{- if $IsTlsEnables }}*/}}
{{/*  tls:*/}}
{{/*    - hosts:*/}}
{{/*        - {{ $DeployDomainName }}*/}}
{{/*    - secretName: {{ $DeployDomainName | quote }}*/}}
{{/*  {{- end }}*/}}
{{/*---*/}}