{{- if .Values.staleCheckCronJob.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ include "specwright.fullname" . }}-stale-check
  labels:
    {{- include "specwright.labels" . | nindent 4 }}
spec:
  schedule: {{ .Values.staleCheckCronJob.schedule | quote }}
  concurrencyPolicy: {{ .Values.staleCheckCronJob.concurrencyPolicy }}
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            {{- include "specwright.selectorLabels" . | nindent 12 }}
        spec:
          {{- with .Values.imagePullSecrets }}
          imagePullSecrets:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          serviceAccountName: {{ include "specwright.serviceAccountName" . }}
          securityContext:
            {{- toYaml .Values.podSecurityContext | nindent 12 }}
          restartPolicy: OnFailure
          containers:
            - name: stale-check
              image: {{ include "specwright.image" . }}
              imagePullPolicy: {{ .Values.image.pullPolicy }}
              command: ["python", "-m", "specwright.cron.stale_check"]
              securityContext:
                {{- toYaml .Values.securityContext | nindent 16 }}
              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 }}
              resources:
                {{- toYaml .Values.resources | nindent 16 }}
              volumeMounts:
                - name: tmp
                  mountPath: /tmp
          volumes:
            - name: tmp
              emptyDir: {}
{{- end }}
