{{- if .Values.ingress.enabled -}}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ include "timetracker.fullname" . }}
  labels:
    {{- include "timetracker.labels" . | nindent 4 }}
  {{- with .Values.ingress.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.ingress.className }}
  ingressClassName: {{ .Values.ingress.className }}
  {{- end }}
  rules:
    {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
          # Backend API routes first (more specific)
          # $2 capture group strips the /api prefix via rewrite-target annotation
          - path: /api(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: {{ include "timetracker.fullname" $ }}-backend
                port:
                  number: {{ $.Values.backend.service.port }}
          # Frontend (catch-all)
          - path: /()(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: {{ include "timetracker.fullname" $ }}-frontend
                port:
                  number: {{ $.Values.frontend.service.port }}
    {{- end }}
  {{- if .Values.ingress.tls.enabled }}
  tls:
    - hosts:
        {{- range .Values.ingress.hosts }}
        - {{ .host | quote }}
        {{- end }}
      secretName: {{ .Values.ingress.tls.secretName }}
  {{- end }}
{{- end }}
