{{- $edemoUrl := .Values.edemocracia.hostname -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "edemocracia.fullname" . }}-edemocracia
  labels:
    {{- include "edemocracia.labels" . | nindent 4 }}
    io.kompose.service: {{ include "edemocracia.fullname" . }}-edemocracia
spec:
  selector:
    matchLabels:
      {{- include "edemocracia.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: edemocracia
      io.kompose.service: {{ include "edemocracia.fullname" . }}-edemocracia
  strategy:
      type: Recreate
  template:
    metadata:
      labels:
        {{- include "edemocracia.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: edemocracia
        io.kompose.service: {{ include "edemocracia.fullname" . }}-edemocracia
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      initContainers:
      - image: "{{ .Values.edemocracia.image.repository }}:{{ .Values.edemocracia.image.tag }}"
        name: {{ include "edemocracia.fullname" . }}-edemocracia-cpmedia
        command:
          - "cp"
          - "-au"
          - "/var/labhacker/edemocracia/src/public/media/"
          - "/srcpublic/"
        volumeMounts:
          - mountPath: /srcpublic
            name: {{ include "edemocracia.fullname" . }}-edemocracia
      containers:
        - name: {{ include "edemocracia.fullname" . }}-edemocracia
          image: "{{ .Values.edemocracia.image.repository }}:{{ .Values.edemocracia.image.tag }}"
          imagePullPolicy: {{ .Values.edemocracia.image.pullPolicy }}
          args:
            - ./runserver-production
          env:
            - name: ADMIN_EMAIL
              value: {{ .Values.edemocracia.admin.email | quote }} 
            - name: ADMIN_PASSWORD
              value: {{ .Values.edemocracia.admin.password | quote }}
            - name: ADMIN_USERNAME
              value: admin
            - name: ALLOWED_HOSTS
              value: "{{ $edemoUrl }}"
            - name: AUDIENCIAS_API_KEY
              value: {{ .Values.edemocracia.apikey | quote }}
            - name: AUDIENCIAS_ENABLED
              {{- if .Values.audiencias.enabled }}
              value: "True"
              {{- else }}
              value: "False"
              {{- end }}
            - name: AUDIENCIAS_UPSTREAM
              value: http://audienciasweb:8000/audiencias
            - name: DATABASE_ENGINE
              value: postgresql_psycopg2
            - name: DATABASE_HOST
              value: {{ (include "postgresql.fullname" .) }}
            - name: DATABASE_NAME
              value: edemocracia
            - name: DATABASE_PASSWORD
              value: {{ .Values.postgresql.postgresqlPassword | quote }}
            - name: DATABASE_PORT
              value: "5432"
            - name: DATABASE_USER
              value: {{ .Values.postgresql.postgresqlUsername | quote }}
            - name: DEBUG
              value: "{{ .Values.edemocracia.debug }}"
            - name: DEFAULT_FROM_EMAIL
              value: '"Portal e-Democracia" <{{ .Values.edemocracia.admin.email }}>'
            - name: DISCOURSE_ENABLED
              {{- if .Values.discourse.enabled }}
              value: "True"
              {{- else }}
              value: "False"
              {{- end }}
            - name: DISCOURSE_SSO_SECRET
              value: {{ .Values.discourse.sso.secret | quote }}
            # Workaround for a bug in runserver-production script, which does not check for DISCOURSE_ENABLED variable
            - name: DISCOURSE_UPSTREAM
              {{- if .Values.discourse.enabled }}
              value: http://discourse:8080/expressao
              {{- else }}
              value: "--version"
              {{- end }}
            - name: EMAIL_BACKEND
              value: django.core.mail.backends.smtp.EmailBackend
            - name: EMAIL_HOST
              value: {{ .Values.smtp.host | quote }}
            - name: EMAIL_HOST_PASSWORD
              value: {{ .Values.smtp.password | quote }}
            - name: EMAIL_HOST_USER
              value: {{ .Values.smtp.username | quote }}
            - name: EMAIL_PORT
              value: {{ .Values.smtp.port | quote }}
            - name: EMAIL_USE_TLS
              value: {{ .Values.smtp.tls | quote }}
            - name: GOOGLE_ANALYTICS_ID
              value: {{ .Values.edemocracia.googleAnalyticsId | quote }}
            - name: OLARK_ID
              value: {{ .Values.edemocracia.olarkId | quote }}
            - name: RECAPTCHA_PRIVATE_KEY
              value: {{ .Values.edemocracia.recaptcha.privateKey | quote }}
            - name: RECAPTCHA_SITE_KEY
              value: {{ .Values.edemocracia.recaptcha.siteKey | quote }}
            - name: SECRET_KEY
              value: {{ .Values.edemocracia.apikey | quote }}
            - name: SITE_NAME
              value: {{ .Values.edemocracia.siteName | quote }}
            - name: SITE_URL
              value: "https://{{ $edemoUrl }}"
            - name: SOCIAL_AUTH_FACEBOOK_KEY
              value: {{ .Values.edemocracia.socialAuth.facebook.key | quote }}
            - name: SOCIAL_AUTH_FACEBOOK_SECRET
              value: {{ .Values.edemocracia.socialAuth.facebook.secret | quote }}
            - name: SOCIAL_AUTH_GOOGLE_OAUTH2_KEY
              value: {{ .Values.edemocracia.socialAuth.google.key | quote }}
            - name: SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET
              value: {{ .Values.edemocracia.socialAuth.google.secret | quote }}
            - name: SOCIAL_AUTH_REDIRECT_IS_HTTPS
              value: "True"
            - name: WIKILEGIS_API_KEY
              value: {{ .Values.edemocracia.apikey | quote }}
            - name: WIKILEGIS_API_URL
              value: /api/v1/
            - name: WIKILEGIS_ENABLED
              {{- if .Values.wikilegis.enabled }}
              value: "True"
              {{- else }}
              value: "False"
              {{- end }}
            - name: WIKILEGIS_UPSTREAM
              value: http://wikilegis:8000
          resources:
            {{- toYaml .Values.edemocracia.resources | nindent 12 }}
          volumeMounts:
            - mountPath: /var/labhacker/edemocracia/src/public/
              name: {{ include "edemocracia.fullname" . }}-edemocracia
      restartPolicy: Always
      serviceAccountName: ""
      volumes:
        - name: {{ include "edemocracia.fullname" . }}-edemocracia
          persistentVolumeClaim:
            claimName: {{ include "edemocracia.fullname" . }}-edemocracia
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}