{{- if .Values.ingress.enabled }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "wordpress-nginx.fullname" . }}
  labels:
    {{- include "wordpress-nginx.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.className }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  {{- if or .Values.ingress.tls .Values.ingress.extraTls }}
  tls:
    {{- if .Values.ingress.tls }}
    - hosts:
        - {{ .Values.ingress.hostname | quote }}
        {{- if .Values.wordpress.multisite.enabled }}
        {{- if .Values.wordpress.multisite.subdomains }}
        - "*.{{ .Values.wordpress.multisite.domain }}"
        {{- end }}
        {{- end }}
      secretName: {{ .Values.ingress.hostname | replace "." "-" }}-tls
    {{- end }}
    {{- if .Values.ingress.extraTls }}
    {{- range .Values.ingress.extraTls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
    {{- end }}
  {{- end }}
  rules:
    - host: {{ .Values.ingress.hostname | quote }}
      http:
        paths:
          - path: {{ .Values.ingress.path }}
            pathType: {{ .Values.ingress.pathType }}
            backend:
              service:
                name: {{ include "wordpress-nginx.fullname" . }}
                port:
                  number: {{ $.Values.service.port }}
    {{- if .Values.wordpress.multisite.enabled }}
    {{- if .Values.wordpress.multisite.subdomains }}
    - host: "*.{{ .Values.wordpress.multisite.domain }}"
      http:
        paths:
          - path: {{ .Values.ingress.path }}
            pathType: {{ .Values.ingress.pathType }}
            backend:
              service:
                name: {{ include "wordpress-nginx.fullname" . }}
                port:
                  number: {{ $.Values.service.port }}
    {{- end }}
    {{- end }}
    {{- range .Values.ingress.extraHosts }}
    - host: {{ .name | quote }}
      http:
        paths:
          - path: {{ .path }}
            pathType: Prefix
            backend:
              service:
                name: {{ include "wordpress-nginx.fullname" $ }}
                port:
                  number: {{ $.Values.service.port }}
    {{- end }}
{{- end }}