---
{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "oxicloud.fullname" . }}
  labels:
    {{- include "oxicloud.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.className }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  {{- if .Values.ingress.tls }}
  tls:
    {{- range .Values.ingress.tls }}
    - hosts:
        {{- range .hosts }}
        - {{ . | quote }}
        {{- end }}
      secretName: {{ .secretName }}
    {{- end }}
  {{- end }}
  rules:
    - host: {{ (index .Values.ingress.hosts 0).host | quote }}
      http:
        paths:
          {{- if .Values.wopi.enabled }}
          # Route Collabora traffic to the WOPI pod
          - path: /browser
            pathType: Prefix
            backend:
              service:
                name: {{ include "oxicloud.fullname" $ }}-wopi
                port:
                  number: {{ .Values.wopi.collabora.service.port }}
          - path: /hosting
            pathType: Prefix
            backend:
              service:
                name: {{ include "oxicloud.fullname" $ }}-wopi
                port:
                  number: {{ .Values.wopi.collabora.service.port }}
          - path: /cool
            pathType: Prefix
            backend:
              service:
                name: {{ include "oxicloud.fullname" $ }}-wopi
                port:
                  number: {{ .Values.wopi.collabora.service.port }}
          {{- end }}

          # Default Catch-All: Route everything else to OxiCloud
          - path: /
            pathType: Prefix
            backend:
              service:
                name: {{ include "oxicloud.fullname" $ }}
                port:
                  number: {{ $.Values.service.port }}
{{- end }}