---
# ConfigMap for SQL Gateway Configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: sql-gateway-config
  namespace: {{ include "flink-sql-gateway.namespace" . }}
  labels:
    {{- include "flink-sql-gateway.labels" . | nindent 4 }}
  {{- with .Values.global.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
data:
  sql-gateway-defaults.yaml: |
    # SQL Gateway Configuration
    sql-gateway:
      endpoint:
        rest:
          address: {{ .Values.sqlGateway.config.endpoint.rest.address }}
          port: {{ .Values.sqlGateway.config.endpoint.rest.port }}
          bind-address: {{ .Values.sqlGateway.config.endpoint.rest.bindAddress }}
      session:
        max-num: {{ .Values.sqlGateway.config.session.maxNum }}
        idle-timeout: {{ .Values.sqlGateway.config.session.idleTimeout }}
        check-interval: {{ .Values.sqlGateway.config.session.checkInterval }}
        plan-cache:
          enabled: {{ .Values.sqlGateway.config.session.planCache.enabled }}
          max-size: {{ .Values.sqlGateway.config.session.planCache.maxSize }}
          ttl: {{ .Values.sqlGateway.config.session.planCache.ttl }}
{{- if .Values.sqlClient.enabled }}
---
# ConfigMap with sample SQL scripts
apiVersion: v1
kind: ConfigMap
metadata:
  name: sql-test-scripts
  namespace: {{ include "flink-sql-gateway.namespace" . }}
  labels:
    {{- include "flink-sql-gateway.labels" . | nindent 4 }}
  {{- with .Values.global.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
data:
  01-create-tables.sql: |
    -- Example SQL script for creating tables
    -- This is a template - modify according to your data sources
    
    -- Example: Create source table (replace with your connector)
    CREATE TABLE source_table (
        id STRING,
        message STRING,
        timestamp_field TIMESTAMP(3),
        WATERMARK FOR timestamp_field AS timestamp_field - INTERVAL '5' SECOND
    ) WITH (
        'connector' = 'your-connector',  -- e.g., 'kafka', 'filesystem', etc.
        -- Add your connector-specific properties here
        'format' = 'json'
    );

    -- Example: Create sink table (replace with your connector)
    CREATE TABLE sink_table (
        id STRING,
        processed_message STRING,
        original_timestamp TIMESTAMP(3),
        processing_time TIMESTAMP(3)
    ) WITH (
        'connector' = 'your-connector',  -- e.g., 'kafka', 'filesystem', etc.
        -- Add your connector-specific properties here
        'format' = 'json'
    );

  02-simple-query.sql: |
    -- Simple filtering and transformation query
    INSERT INTO sink_table
    SELECT
        id,
        CONCAT('Processed: ', message) as processed_message,
        timestamp_field as original_timestamp,
        CURRENT_TIMESTAMP as processing_time
    FROM source_table
    WHERE LENGTH(message) > 5;

  03-windowed-aggregation.sql: |
    -- Windowed aggregation example
    SELECT
        TUMBLE_START(timestamp_field, INTERVAL '1' MINUTE) as window_start,
        TUMBLE_END(timestamp_field, INTERVAL '1' MINUTE) as window_end,
        COUNT(*) as message_count,
        COUNT(DISTINCT id) as unique_ids
    FROM source_table
    GROUP BY TUMBLE(timestamp_field, INTERVAL '1' MINUTE);

  test-data-generator.sql: |
    -- Generate test data (for manual testing)
    -- This would typically be done by external producer
    -- Example JSON format for input data:
    -- {"id": "msg001", "message": "Hello World", "timestamp_field": "2024-12-10T10:00:00.000Z"}
    -- {"id": "msg002", "message": "Test Message", "timestamp_field": "2024-12-10T10:01:00.000Z"}

  README.md: |
    # Flink SQL Client Usage

    ## Connect to SQL Gateway with Remote Cluster
    ```bash
    # Connect to SQL Gateway and specify remote cluster
    kubectl exec -it {{ .Values.sqlClient.name }} -n {{ include "flink-sql-gateway.namespace" . }} -- /opt/flink/bin/sql-client.sh gateway \
      --endpoint http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }} \
      -Dexecution.target=remote \
      -Drest.address={{ .Values.sessionCluster.name }}-rest \
      -Drest.port=8081
    ```

    ## Alternative: Set execution config in SQL
    ```bash
    # Connect to SQL Gateway
    kubectl exec -it {{ .Values.sqlClient.name }} -n {{ include "flink-sql-gateway.namespace" . }} -- /opt/flink/bin/sql-client.sh gateway --endpoint http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }}

    # Then in SQL Client, set execution config:
    SET 'execution.target' = 'remote';
    SET 'rest.address' = '{{ .Values.sessionCluster.name }}-rest';
    SET 'rest.port' = '8081';
    ```

    ## Run SQL Scripts
    ```bash
    # Inside the SQL Client container
    kubectl exec -it {{ .Values.sqlClient.name }} -n {{ include "flink-sql-gateway.namespace" . }} -- bash

    # View available scripts
    ls /opt/sql-scripts/

    # Execute SQL file with remote cluster config
    /opt/flink/bin/sql-client.sh gateway \
      --endpoint http://{{ .Values.sqlGateway.name }}:{{ .Values.sqlGateway.port }} \
      -Dexecution.target=remote \
      -Drest.address={{ .Values.sessionCluster.name }}-rest \
      -Drest.port=8081 \
      -f /opt/sql-scripts/01-create-tables.sql
    ```

    ## Test Data
    Send JSON messages to your input source:
    ```json
    {"id": "msg001", "message": "Hello World", "timestamp_field": "2024-12-10T10:00:00.000Z"}
    ```
{{- end }}