{{- if .Values.sqlClient.enabled }}
---
# SQL Client Deployment for interactive SQL queries
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.sqlClient.name }}
  namespace: {{ include "flink-sql-gateway.namespace" . }}
  labels:
    {{- include "flink-sql-gateway.sqlClient.labels" . | nindent 4 }}
  {{- with .Values.global.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.sqlClient.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.sqlClient.name }}
        {{- include "flink-sql-gateway.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "flink-sql-gateway.serviceAccountName" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: sql-client
          image: {{ include "flink-sql-gateway.image" . }}
          imagePullPolicy: {{ .Values.global.image.pullPolicy }}
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          command: ["/bin/bash"]
          args:
            - -c
            - |
              echo "Flink SQL Client Ready"
              echo "Connect to SQL Gateway: {{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }}"
              echo "Usage: /opt/flink/bin/sql-client.sh gateway --endpoint http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }}"
              
              # Wait for SQL Gateway to be ready
              echo "Waiting for SQL Gateway..."
              until curl -f http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }}/v1/info; do
                echo "Waiting for SQL Gateway..."
                sleep 10
              done
              
              echo "SQL Gateway is ready!"
              echo "You can now connect using:"
              echo "/opt/flink/bin/sql-client.sh gateway --endpoint http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }}"
              
              # Keep container running
              tail -f /dev/null
          {{- with .Values.sqlClient.env }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- with .Values.sqlClient.volumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
            - name: sql-scripts
              mountPath: /opt/sql-scripts
              readOnly: true
          resources:
            {{- toYaml .Values.sqlClient.resources | nindent 12 }}
      volumes:
        {{- with .Values.sqlClient.volumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: sql-scripts
          configMap:
            name: sql-test-scripts
{{- end }}