{{- if .Values.mcp.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "statehub.mcpFullname" . }}
  labels: {{- include "statehub.mcpLabels" . | nindent 4 }}
spec:
  replicas: {{ .Values.mcp.replicaCount }}
  selector:
    matchLabels: {{- include "statehub.mcpSelectorLabels" . | nindent 6 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels: {{- include "statehub.mcpLabels" . | nindent 8 }}
    spec:
      securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets: {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: state-hub-mcp
          image: {{ include "statehub.image" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext: {{- toYaml .Values.securityContext | nindent 12 }}
          # The image CMD serves the API; this pod runs the MCP layer instead.
          # Use -m so the WORKDIR (/app) lands on sys.path — invoking the file
          # directly would put /app/mcp_server there and break its own imports.
          command: ["/app/.venv/bin/python", "-m", "mcp_server.server"]
          ports:
            - name: sse
              containerPort: {{ .Values.mcp.service.targetPort }}
              protocol: TCP
          env:
            - name: MCP_TRANSPORT
              value: {{ .Values.mcp.transport | quote }}
            - name: MCP_PORT
              value: {{ .Values.mcp.service.targetPort | quote }}
            # A Service routes to the pod IP, so the default loopback bind
            # would be unreachable.
            - name: MCP_HOST
              value: "0.0.0.0"
            # Stateless HTTP client over the API — no database access.
            - name: API_BASE
              value: {{ include "statehub.mcpApiBase" . | quote }}
          {{- if .Values.mcp.probes.enabled }}
          readinessProbe:
            tcpSocket:
              port: {{ .Values.mcp.service.targetPort }}
            initialDelaySeconds: {{ .Values.mcp.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.mcp.probes.readiness.periodSeconds }}
            timeoutSeconds: {{ .Values.mcp.probes.readiness.timeoutSeconds }}
            failureThreshold: {{ .Values.mcp.probes.readiness.failureThreshold }}
          livenessProbe:
            tcpSocket:
              port: {{ .Values.mcp.service.targetPort }}
            initialDelaySeconds: {{ .Values.mcp.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.mcp.probes.liveness.periodSeconds }}
            timeoutSeconds: {{ .Values.mcp.probes.liveness.timeoutSeconds }}
            failureThreshold: {{ .Values.mcp.probes.liveness.failureThreshold }}
          {{- end }}
          resources: {{- toYaml .Values.mcp.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity: {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations: {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
