{{- if .Values.admin.enabled -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "admin.fullname" . }}-admin
  namespace: {{ include "admin.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ include "admin.fullname" . }}-admin
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/version: {{ .Chart.AppVersion }}
spec:
  replicas: {{ .Values.admin.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "admin.fullname" . }}-admin
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "admin.fullname" . }}-admin
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- if $.Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range $.Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      terminationGracePeriodSeconds: 30
      containers:
        - name: admin
          image: "{{ include "admin.image" . }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.admin.service.port }}
              protocol: TCP
          env:
            - name: NODE_ENV
              value: {{ .Values.admin.config.nodeEnv | default "production" | quote }}
            {{- if .Values.admin.config.appUrl }}
            - name: NEXT_PUBLIC_APP_URL
              value: {{ .Values.admin.config.appUrl | quote }}
            {{- end }}
            {{- if .Values.admin.config.appDomain }}
            - name: NEXT_PUBLIC_APP_DOMAIN
              value: {{ .Values.admin.config.appDomain | quote }}
            {{- end }}
            {{- if .Values.secrets.enabled }}
            - name: {{ .Values.admin.secretKeys.databaseUrl }}
              valueFrom:
                secretKeyRef:
                  name: {{ include "admin.fullname" . }}-secrets
                  key: {{ .Values.admin.secretKeys.databaseUrl }}
            - name: {{ .Values.admin.secretKeys.redisUrl }}
              valueFrom:
                secretKeyRef:
                  name: {{ include "admin.fullname" . }}-secrets
                  key: {{ .Values.admin.secretKeys.redisUrl }}
            - name: {{ .Values.admin.secretKeys.nextAuthSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ include "admin.fullname" . }}-secrets
                  key: {{ .Values.admin.secretKeys.nextAuthSecret }}
            {{- end }}
            {{- if kindIs "slice" .Values.admin.env }}
            {{- range .Values.admin.env }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}
            {{- else if kindIs "map" .Values.admin.env }}
            {{- range $k, $v := .Values.admin.env }}
            - name: {{ $k }}
              value: {{ $v | quote }}
            {{- end }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.admin.livenessProbe.path }}
              port: {{ .Values.admin.livenessProbe.port }}
            initialDelaySeconds: {{ .Values.admin.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.admin.livenessProbe.periodSeconds }}
          readinessProbe:
            httpGet:
              path: {{ .Values.admin.readinessProbe.path }}
              port: {{ .Values.admin.readinessProbe.port }}
            initialDelaySeconds: {{ .Values.admin.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.admin.readinessProbe.periodSeconds }}
          {{- if .Values.admin.resources }}
          resources:
            {{- toYaml .Values.admin.resources | nindent 12 }}
          {{- end }}
      {{- with .Values.admin.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.admin.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.admin.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "admin.fullname" . }}-admin
  namespace: {{ include "admin.namespace" . }}
  labels:
    app.kubernetes.io/name: {{ include "admin.fullname" . }}-admin
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  type: {{ .Values.admin.service.type }}
  ports:
    - port: {{ .Values.admin.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app.kubernetes.io/name: {{ include "admin.fullname" . }}-admin
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
