{{- if .Values.sqlGateway.enabled }}
---
# SQL Gateway Deployment (separate from Flink cluster)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.sqlGateway.name }}
  namespace: {{ include "flink-sql-gateway.namespace" . }}
  labels:
    {{- include "flink-sql-gateway.sqlGateway.labels" . | nindent 4 }}
  {{- with .Values.global.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.sqlGateway.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.sqlGateway.name }}
  template:
    metadata:
      labels:
        app: {{ .Values.sqlGateway.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-gateway
          image: {{ include "flink-sql-gateway.image" . }}
          imagePullPolicy: {{ .Values.global.image.pullPolicy }}
          {{- with .Values.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          command: ["/bin/bash"]
          args:
            - -c
            - |
              # Wait for Flink session cluster to be ready with timeout
              echo "Waiting for Flink session cluster..."
              TIMEOUT=300  # 5 minutes
              ELAPSED=0
              INTERVAL=5
              
              while [ $ELAPSED -lt $TIMEOUT ]; do
                if curl -f --connect-timeout 5 --max-time 10 http://{{ .Values.sessionCluster.name }}-rest:8081/v1/overview >/dev/null 2>&1; then
                  echo "Flink session cluster is ready!"
                  break
                fi
                echo "Waiting for Flink cluster... (${ELAPSED}s/${TIMEOUT}s)"
                sleep $INTERVAL
                ELAPSED=$((ELAPSED + INTERVAL))
              done
              
              if [ $ELAPSED -ge $TIMEOUT ]; then
                echo "ERROR: Timeout waiting for Flink session cluster"
                echo "Checking cluster status..."
                kubectl get pods -n {{ include "flink-sql-gateway.namespace" . }} -l app={{ .Values.sessionCluster.name }} || true
                exit 1
              fi
              
              echo "Flink cluster is ready. Starting SQL Gateway..."
              
              # Create flink configuration with environment variable substitution
              cat > /opt/flink/conf/config.yaml << EOF
              # Remote Flink Cluster Configuration
              rest.bind-address: 0.0.0.0
              rest.address: {{ .Values.sessionCluster.name }}-rest
              rest.port: 8081
              jobmanager.rpc.address: {{ .Values.sessionCluster.name }}
              jobmanager.rpc.port: 6123
              
              # Session Cluster at Kubernetes
              execution.target: kubernetes-session
              kubernetes.namespace: {{ .Release.Namespace }}
              kubernetes.cluster-id: {{ .Values.sessionCluster.name }}
              
              # SQL Gateway Configuration
              sql-gateway.endpoint.rest.address: 0.0.0.0
              sql-gateway.endpoint.rest.port: {{ .Values.sqlGateway.port }}
              
              # Table/SQL Configuration
              table.exec.source.idle-timeout: 30s
              table.exec.resource.default-parallelism: 1
              
              # Additional Flink Configuration from values
              {{- with .Values.sqlGateway.flinkConfiguration }}
              {{- range $key, $value := . }}
              {{ $key }}: {{ $value }}
              {{- end }}
              {{- end }}
              EOF
              
              # Start SQL Gateway
              /opt/flink/bin/sql-gateway.sh start
              
              echo "SQL Gateway started successfully"
              
              # Wait for log file to be created and then tail it
              LOG_FILE=""
              for i in {1..30}; do
                LOG_FILE=$(find /opt/flink/log -name "flink--sql-gateway-*.log" 2>/dev/null | head -1)
                if [ -n "$LOG_FILE" ]; then
                  echo "Found log file: $LOG_FILE"
                  break
                fi
                echo "Waiting for log file to be created... ($i/30)"
                sleep 2
              done
              
              if [ -n "$LOG_FILE" ]; then
                echo "Tailing SQL Gateway log file..."
                tail -f "$LOG_FILE"
              else
                echo "Log file not found, keeping container alive..."
                # Keep container running without tailing logs
                while true; do
                  echo "SQL Gateway is running on port {{ .Values.sqlGateway.port }}"
                  sleep 60
                done
              fi
          env:
            - name: FLINK_CONF_DIR
              value: "/opt/flink/conf"
            {{- with .Values.sqlGateway.env }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: rest
              containerPort: {{ .Values.sqlGateway.port }}
              protocol: TCP
          volumeMounts:
            {{- with .Values.sqlGateway.volumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
            - name: sql-gateway-config
              mountPath: /opt/flink/conf/sql-gateway-defaults.yaml
              subPath: sql-gateway-defaults.yaml
              readOnly: true
          resources:
            {{- toYaml .Values.sqlGateway.resources | nindent 12 }}
          {{- with .Values.sqlGateway.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.sqlGateway.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      volumes:
        {{- with .Values.sqlGateway.volumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: sql-gateway-config
          configMap:
            name: sql-gateway-config
{{- end }}