apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "specwright.fullname" . }}
  labels:
    {{- include "specwright.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "specwright.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "specwright.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "specwright.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          image: {{ include "specwright.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          ports:
            - name: http
              containerPort: {{ .Values.config.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ include "specwright.fullname" . }}
            - secretRef:
                name: {{ include "specwright.githubSecretName" . }}
            - secretRef:
                name: {{ include "specwright.anthropicSecretName" . }}
          {{- if or .Values.secrets.neon.databaseUrl .Values.secrets.neon.existingSecret }}
            - secretRef:
                name: {{ include "specwright.neonSecretName" . }}
          {{- end }}
          {{- if or .Values.secrets.gcp.serviceAccountKey .Values.secrets.gcp.existingSecret }}
            - secretRef:
                name: {{ include "specwright.gcpSecretName" . }}
          {{- end }}
          {{- if or .Values.secrets.jira.host .Values.secrets.jira.existingSecret }}
            - secretRef:
                name: {{ include "specwright.jiraSecretName" . }}
          {{- end }}
          {{- if or .Values.secrets.auth0.domain .Values.secrets.auth0.existingSecret }}
            - secretRef:
                name: {{ include "specwright.auth0SecretName" . }}
          {{- end }}
          {{- if or .Values.secrets.mcp.apiKey .Values.secrets.mcp.existingSecret }}
            - secretRef:
                name: {{ include "specwright.mcpSecretName" . }}
          {{- end }}
          {{- if or .Values.secrets.posthog.apiKey .Values.secrets.posthog.existingSecret }}
            - secretRef:
                name: {{ include "specwright.posthogSecretName" . }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: http
            initialDelaySeconds: 5
            periodSeconds: 15
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /readyz
              port: http
            initialDelaySeconds: 3
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
      volumes:
        - name: tmp
          emptyDir: {}
