{{- if and (.Values.secret) (.Values.secret.enabled) }}

{{- $postgresAdminPassword := include "formbricks.postgresAdminPassword" . }}
{{- $postgresUserPassword := include "formbricks.postgresUserPassword" . }}
{{- $redisPassword := include "formbricks.redisPassword" . }}
{{- $webappUrl := required "formbricks.webappUrl is required. Set it to your Formbricks instance URL (e.g., https://formbricks.example.com)" .Values.formbricks.webappUrl }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "formbricks.name" . }}-app-secrets
  labels:
    {{- include "formbricks.labels" . | nindent 4 }}
data:
  # Formbricks application URLs
  WEBAPP_URL: {{ $webappUrl | b64enc }}
  NEXTAUTH_URL: {{ $webappUrl | b64enc }}
  {{- if .Values.formbricks.publicUrl }}
  PUBLIC_URL: {{ .Values.formbricks.publicUrl | b64enc }}
  {{- end }}
  {{- if .Values.redis.enabled }}
  REDIS_URL: {{ printf "redis://:%s@formbricks-redis-master:6379" $redisPassword | b64enc }}
  {{- else }}
  REDIS_URL: {{ .Values.redis.externalRedisUrl | b64enc }}
  {{- end }}
  {{- if .Values.postgresql.enabled }}
  DATABASE_URL: {{ printf "postgresql://formbricks:%s@formbricks-postgresql/formbricks" $postgresUserPassword | b64enc }}
  {{- else }}
  DATABASE_URL: {{ .Values.postgresql.externalDatabaseUrl | b64enc }}
  {{- end }}
  CRON_SECRET: {{ include "formbricks.cronSecret" . | b64enc }}
  ENCRYPTION_KEY: {{ include "formbricks.encryptionKey" . | b64enc }}
  NEXTAUTH_SECRET: {{ include "formbricks.nextAuthSecret" . | b64enc }}
  {{- if and (.Values.enterprise.licenseKey) (ne .Values.enterprise.licenseKey "") }}
  ENTERPRISE_LICENSE_KEY: {{ .Values.enterprise.licenseKey | b64enc }}
  {{- end }}
  {{- if .Values.redis.enabled }}
  REDIS_PASSWORD: {{ $redisPassword | b64enc }}
  {{- end }}
  {{- if .Values.postgresql.enabled }}
  POSTGRES_ADMIN_PASSWORD: {{ $postgresAdminPassword | b64enc }}
  POSTGRES_USER_PASSWORD: {{ $postgresUserPassword | b64enc }}
  {{- end }}
{{- end }}
