{{- if and .Values.observability.metrics.enabled .Values.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ include "nextcloud-mcp-server.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "nextcloud-mcp-server.labels" . | nindent 4 }}
    {{- with .Values.serviceMonitor.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  selector:
    matchLabels:
      {{- include "nextcloud-mcp-server.selectorLabels" . | nindent 6 }}
  endpoints:
    - port: metrics
      path: {{ .Values.observability.metrics.path }}
      interval: {{ .Values.serviceMonitor.interval }}
      scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
      scheme: http
      relabelings:
        # Add namespace label
        - sourceLabels: [__meta_kubernetes_namespace]
          targetLabel: namespace
        # Add pod label
        - sourceLabels: [__meta_kubernetes_pod_name]
          targetLabel: pod
        # Add service label
        - sourceLabels: [__meta_kubernetes_service_name]
          targetLabel: service
{{- end }}
