{{- define "deputies.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "deputies.fullname" -}}
{{- if .Values.fullnameOverride -}}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- $name := include "deputies.name" . -}}
{{- if contains $name .Release.Name -}}
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
{{- else -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{- define "deputies.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{- define "deputies.labels" -}}
helm.sh/chart: {{ include "deputies.chart" . }}
app.kubernetes.io/name: {{ include "deputies.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{- define "deputies.selectorLabels" -}}
app.kubernetes.io/name: {{ include "deputies.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end -}}

{{- define "deputies.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
{{- default (include "deputies.fullname" .) .Values.serviceAccount.name -}}
{{- else -}}
{{- default "default" .Values.serviceAccount.name -}}
{{- end -}}
{{- end -}}

{{- define "deputies.agentSandboxOrchestratorServiceAccountName" -}}
{{- if .Values.controlPlane.agentSandboxOrchestrator.serviceAccount.create -}}
{{- default (printf "%s-agent-sandbox-orchestrator" (include "deputies.fullname" .)) .Values.controlPlane.agentSandboxOrchestrator.serviceAccount.name -}}
{{- else -}}
{{- default (include "deputies.serviceAccountName" .) .Values.controlPlane.agentSandboxOrchestrator.serviceAccount.name -}}
{{- end -}}
{{- end -}}

{{- define "deputies.agentSandboxRbacServiceAccountName" -}}
{{- if .Values.controlPlane.agentSandboxOrchestrator.enabled -}}
{{- include "deputies.agentSandboxOrchestratorServiceAccountName" . -}}
{{- else -}}
{{- include "deputies.serviceAccountName" . -}}
{{- end -}}
{{- end -}}

{{- define "deputies.secretName" -}}
{{- default (printf "%s-app" (include "deputies.fullname" .)) .Values.secrets.name -}}
{{- end -}}

{{- define "deputies.postgresSecretName" -}}
{{- default (printf "%s-postgres" (include "deputies.fullname" .)) .Values.postgres.existingSecret -}}
{{- end -}}

{{- define "deputies.controlPlaneEnv" -}}
{{- $root := .root -}}
{{- $runMode := .runMode -}}
{{- $port := .port -}}
- name: PORT
  value: {{ $port | quote }}
- name: RUN_MODE
  value: {{ $runMode | quote }}
- name: RUNNER
  value: {{ $root.Values.config.runner | quote }}
- name: SANDBOX_PROVIDER
  value: {{ $root.Values.config.sandboxProvider | quote }}
- name: SANDBOX_WORKSPACE_PATH
  value: {{ $root.Values.config.sandboxWorkspacePath | quote }}
- name: APP_DATA_STORE
  value: {{ $root.Values.config.appDataStore | quote }}
- name: RUNNER_STATE_STORE
  value: {{ $root.Values.config.runnerStateStore | quote }}
- name: API_AUTH_MODE
  value: {{ $root.Values.config.apiAuthMode | quote }}
- name: AUTH_PROVIDER
  value: {{ $root.Values.config.authProvider | quote }}
- name: AUTH_COOKIE_SECURE
  value: {{ $root.Values.config.authCookieSecure | quote }}
- name: AUTH_COOKIE_SAME_SITE
  value: {{ $root.Values.config.authCookieSameSite | quote }}
- name: AUTH_GITHUB_ADMIN_USERS
  value: {{ $root.Values.config.authGithubAdminUsers | quote }}
- name: AUTH_GITHUB_ALLOWED_USERS
  value: {{ $root.Values.config.authGithubAllowedUsers | quote }}
- name: AUTH_GITHUB_ALLOWED_ORGANIZATIONS
  value: {{ $root.Values.config.authGithubAllowedOrganizations | quote }}
- name: AUTH_GITHUB_DEFAULT_ROLE
  value: {{ $root.Values.config.authGithubDefaultRole | quote }}
- name: UNSAFE_AUTH_GITHUB_ALLOW_ALL
  value: {{ $root.Values.config.unsafeAuthGithubAllowAll | quote }}
- name: WEB_BASE_URL
  value: {{ $root.Values.config.webBaseUrl | quote }}
- name: SERVICE_BASE_DOMAIN
  value: {{ $root.Values.config.serviceBaseDomain | quote }}
- name: SERVICE_TRUST_FORWARDED_HOSTS
  value: {{ $root.Values.config.serviceTrustForwardedHosts | quote }}
- name: RUNNER_MODEL_DEFAULT
  value: {{ $root.Values.config.runnerModelDefault | quote }}
- name: OPENAI_CODEX_AUTH_STORAGE
  value: postgres
- name: TITLE_GENERATION_ENABLED
  value: {{ $root.Values.config.titleGenerationEnabled | quote }}
- name: PRIVATE_SESSIONS_ENABLED
  value: {{ $root.Values.config.privateSessionsEnabled | quote }}
{{- if $root.Values.config.titleGenerationModel }}
- name: TITLE_GENERATION_MODEL
  value: {{ $root.Values.config.titleGenerationModel | quote }}
{{- end }}
- name: REPOSITORY_SETUP_SCRIPT_ENABLED
  value: {{ $root.Values.config.repositorySetupScriptEnabled | quote }}
- name: REPOSITORY_SETUP_SCRIPT_TIMEOUT_SECONDS
  value: {{ $root.Values.config.repositorySetupScriptTimeoutSeconds | quote }}
- name: DAYTONA_IMAGE
  value: {{ $root.Values.config.daytonaImage | quote }}
- name: AGENT_SANDBOX_ORCHESTRATOR_MODE
  value: {{ ternary "http" $root.Values.config.agentSandboxOrchestratorMode $root.Values.controlPlane.agentSandboxOrchestrator.enabled | quote }}
- name: AGENT_SANDBOX_IMAGE
  value: {{ $root.Values.config.agentSandboxImage | quote }}
- name: AGENT_SANDBOX_STORAGE_SIZE
  value: {{ $root.Values.config.agentSandboxStorageSize | quote }}
- name: HIDE_SETUP_PAGE
  value: {{ $root.Values.config.hideSetupPage | quote }}
{{- if $root.Values.config.daytonaApiUrl }}
- name: DAYTONA_API_URL
  value: {{ $root.Values.config.daytonaApiUrl | quote }}
{{- end }}
{{- if $root.Values.config.daytonaTarget }}
- name: DAYTONA_TARGET
  value: {{ $root.Values.config.daytonaTarget | quote }}
{{- end }}
{{- if $root.Values.config.daytonaSnapshot }}
- name: DAYTONA_SNAPSHOT
  value: {{ $root.Values.config.daytonaSnapshot | quote }}
{{- end }}
{{- if or $root.Values.controlPlane.agentSandboxOrchestrator.enabled $root.Values.config.agentSandboxOrchestratorUrl }}
- name: AGENT_SANDBOX_ORCHESTRATOR_URL
  value: {{ ternary (include "deputies.agentSandboxOrchestratorUrl" $root) $root.Values.config.agentSandboxOrchestratorUrl $root.Values.controlPlane.agentSandboxOrchestrator.enabled | quote }}
{{- end }}
{{- if $root.Values.config.agentSandboxNamespace }}
- name: AGENT_SANDBOX_NAMESPACE
  value: {{ $root.Values.config.agentSandboxNamespace | quote }}
{{- end }}
{{- if $root.Values.config.agentSandboxStorageClassName }}
- name: AGENT_SANDBOX_STORAGE_CLASS_NAME
  value: {{ $root.Values.config.agentSandboxStorageClassName | quote }}
{{- end }}
- name: ARTIFACT_STORAGE_PROVIDER
  value: {{ $root.Values.config.artifactStorageProvider | quote }}
- name: ARTIFACT_STORAGE_S3_ENDPOINT
  value: {{ $root.Values.config.artifactStorageS3Endpoint | quote }}
- name: ARTIFACT_STORAGE_S3_REGION
  value: {{ $root.Values.config.artifactStorageS3Region | quote }}
- name: ARTIFACT_STORAGE_S3_BUCKET
  value: {{ $root.Values.config.artifactStorageS3Bucket | quote }}
- name: ARTIFACT_STORAGE_S3_FORCE_PATH_STYLE
  value: {{ $root.Values.config.artifactStorageS3ForcePathStyle | quote }}
- name: ARTIFACT_STORAGE_S3_CREATE_BUCKET
  value: {{ $root.Values.config.artifactStorageS3CreateBucket | quote }}
- name: POSTGRES_USER
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.username }}
- name: POSTGRES_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.password }}
- name: POSTGRES_DATABASE
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.database }}
- name: POSTGRES_HOST
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.host }}
- name: POSTGRES_PORT
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.port }}
- name: DATABASE_URL
  value: postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DATABASE){{ ternary (printf "?sslmode=%s" $root.Values.postgres.sslMode) "" (ne $root.Values.postgres.sslMode "") }}
{{- range $name, $value := $root.Values.config.extraEnv }}
{{- if has $name (list "OPENAI_CODEX_AUTH_STORAGE" "OPENAI_CODEX_AUTH_BASE64" "INTEGRATION_CREDENTIAL_ACTIVE_KEY_ID" "INTEGRATION_CREDENTIAL_ENCRYPTION_KEYS") }}
{{- fail (printf "config.extraEnv may not override Helm-managed credential variable %s" $name) }}
{{- end }}
- name: {{ $name }}
  value: {{ $value | quote }}
{{- end }}
{{- end -}}

{{- define "deputies.migrationEnv" -}}
{{- $root := . -}}
{{- if $root.Values.postgres.existingSecret }}
- name: POSTGRES_USER
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.username }}
- name: POSTGRES_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.password }}
- name: POSTGRES_DATABASE
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.database }}
- name: POSTGRES_HOST
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.host }}
- name: POSTGRES_PORT
  valueFrom:
    secretKeyRef:
      name: {{ include "deputies.postgresSecretName" $root }}
      key: {{ $root.Values.postgres.secretKeys.port }}
- name: DATABASE_URL
  value: postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DATABASE){{ ternary (printf "?sslmode=%s" $root.Values.postgres.sslMode) "" (ne $root.Values.postgres.sslMode "") }}
{{- else }}
- name: DATABASE_URL
  value: postgres://{{ $root.Values.postgres.username }}:{{ $root.Values.postgres.password }}@{{ $root.Values.postgres.host }}:{{ $root.Values.postgres.port }}/{{ $root.Values.postgres.database }}{{ ternary (printf "?sslmode=%s" $root.Values.postgres.sslMode) "" (ne $root.Values.postgres.sslMode "") }}
{{- end }}
{{- end -}}

{{- define "deputies.apiServiceName" -}}
{{- printf "%s-api" (include "deputies.fullname" .) -}}
{{- end -}}

{{- define "deputies.agentSandboxOrchestratorServiceName" -}}
{{- printf "%s-agent-sandbox-orchestrator" (include "deputies.fullname" .) -}}
{{- end -}}

{{- define "deputies.agentSandboxOrchestratorUrl" -}}
{{- printf "http://%s:%v" (include "deputies.agentSandboxOrchestratorServiceName" .) .Values.controlPlane.agentSandboxOrchestrator.port -}}
{{- end -}}
