heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Natural Language Query API handlers
//!
//! Provides REST API endpoints for converting natural language to SQL:
//! - POST /v1/nl/query - Convert NL to SQL
//! - POST /v1/nl/execute - Convert NL to SQL and execute
//! - POST /v1/nl/explain - Explain a natural language query
//! - GET /v1/nl/schema - Get schema context for NL queries
//! - POST /v1/nl/suggest - Get query suggestions

use axum::{
    extract::{Query, State},
    Json,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tracing::{info, error};

use crate::api::models::ApiError;
use crate::api::server::AppState;
use crate::storage::dump::DatabaseInterface;
use crate::ai::nl_query::{
    NlQueryEngine, NlQueryRequest, NlQueryConfig,
    SchemaContext, TableSchema, ColumnSchema, ConversationContext,
    QueryHistoryEntry, SqlDialect,
};

// ============================================================================
// Request/Response Types
// ============================================================================

/// NL Query request body
#[derive(Debug, Deserialize)]
pub struct NlQueryApiRequest {
    /// Natural language question
    pub question: String,
    /// Database/schema to query (optional, uses default)
    pub database: Option<String>,
    /// Schema name (optional)
    pub schema: Option<String>,
    /// Specific tables to include (optional, uses all visible)
    pub tables: Option<Vec<String>>,
    /// Previous conversation context
    pub context: Option<Vec<ConversationEntry>>,
    /// Session ID for conversation tracking
    pub session_id: Option<String>,
    /// Configuration overrides
    pub config: Option<NlQueryConfigOverride>,
}

/// Conversation entry for context
#[derive(Debug, Deserialize)]
pub struct ConversationEntry {
    pub question: String,
    pub sql: String,
    pub success: bool,
}

/// Configuration overrides
#[derive(Debug, Deserialize)]
pub struct NlQueryConfigOverride {
    /// SQL dialect
    pub dialect: Option<String>,
    /// Maximum results
    pub max_results: Option<usize>,
    /// Whether to validate SQL
    pub validate_sql: Option<bool>,
    /// Whether to explain results
    pub explain_results: Option<bool>,
    /// LLM temperature
    pub temperature: Option<f32>,
    /// Model to use
    pub model: Option<String>,
}

/// NL Query API response
#[derive(Debug, Serialize)]
pub struct NlQueryApiResponse {
    /// Generated SQL query
    pub sql: String,
    /// Query explanation
    pub explanation: Option<String>,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,
    /// Detected intent
    pub intent: String,
    /// Tables referenced
    pub tables: Vec<String>,
    /// Whether query is valid
    pub valid: bool,
    /// Validation errors (if any)
    pub validation_errors: Vec<String>,
    /// Warnings
    pub warnings: Vec<String>,
    /// Suggested queries
    pub suggestions: Vec<SuggestionResponse>,
    /// Processing time in ms
    pub processing_time_ms: u64,
    /// Whether result was cached
    pub cached: bool,
}

/// Suggestion response
#[derive(Debug, Serialize)]
pub struct SuggestionResponse {
    pub text: String,
    pub sql: Option<String>,
    pub reason: String,
}

/// NL Execute request (query + execute)
#[derive(Debug, Deserialize)]
pub struct NlExecuteRequest {
    /// Natural language question
    pub question: String,
    /// Branch to execute on
    pub branch: Option<String>,
    /// Database/schema context
    pub database: Option<String>,
    /// Schema name
    pub schema: Option<String>,
    /// Tables to include
    pub tables: Option<Vec<String>>,
    /// Maximum rows to return
    pub limit: Option<usize>,
    /// Conversation context
    pub context: Option<Vec<ConversationEntry>>,
    /// Session ID
    pub session_id: Option<String>,
    /// Configuration
    pub config: Option<NlQueryConfigOverride>,
}

/// NL Execute response
#[derive(Debug, Serialize)]
pub struct NlExecuteResponse {
    /// Generated SQL
    pub sql: String,
    /// Query explanation
    pub explanation: Option<String>,
    /// Confidence score
    pub confidence: f32,
    /// Column names
    pub columns: Vec<String>,
    /// Column types
    pub column_types: Vec<String>,
    /// Result rows
    pub rows: Vec<HashMap<String, serde_json::Value>>,
    /// Row count
    pub row_count: usize,
    /// NL processing time
    pub nl_processing_time_ms: u64,
    /// SQL execution time
    pub sql_execution_time_ms: u64,
    /// Total time
    pub total_time_ms: u64,
    /// Warnings
    pub warnings: Vec<String>,
}

/// Schema context response
#[derive(Debug, Serialize)]
pub struct SchemaContextResponse {
    /// Available tables
    pub tables: Vec<TableSchemaResponse>,
    /// Database name
    pub database: Option<String>,
    /// Schema name
    pub schema: Option<String>,
}

/// Table schema response
#[derive(Debug, Serialize)]
pub struct TableSchemaResponse {
    pub name: String,
    pub description: Option<String>,
    pub columns: Vec<ColumnSchemaResponse>,
    pub primary_key: Option<Vec<String>>,
    pub foreign_keys: Option<Vec<ForeignKeyResponse>>,
    pub row_count: Option<usize>,
}

/// Column schema response
#[derive(Debug, Serialize)]
pub struct ColumnSchemaResponse {
    pub name: String,
    pub data_type: String,
    pub nullable: bool,
    pub description: Option<String>,
    pub is_primary_key: bool,
}

/// Foreign key response
#[derive(Debug, Serialize)]
pub struct ForeignKeyResponse {
    pub columns: Vec<String>,
    pub ref_table: String,
    pub ref_columns: Vec<String>,
}

/// Explain request
#[derive(Debug, Deserialize)]
pub struct NlExplainRequest {
    /// SQL query to explain
    pub sql: String,
    /// Natural language question (optional, for context)
    pub question: Option<String>,
}

/// Explain response
#[derive(Debug, Serialize)]
pub struct NlExplainResponse {
    /// Natural language explanation
    pub explanation: String,
    /// Query breakdown
    pub breakdown: QueryBreakdown,
    /// Suggestions for improvement
    pub suggestions: Vec<String>,
}

/// Query breakdown
#[derive(Debug, Serialize)]
pub struct QueryBreakdown {
    /// Operation type
    pub operation: String,
    /// Tables involved
    pub tables: Vec<String>,
    /// Columns selected
    pub columns: Vec<String>,
    /// Conditions
    pub conditions: Vec<String>,
    /// Joins
    pub joins: Vec<String>,
    /// Aggregations
    pub aggregations: Vec<String>,
    /// Order by
    pub order_by: Option<String>,
    /// Limit
    pub limit: Option<usize>,
}

/// Suggestion request
#[derive(Debug, Deserialize)]
pub struct NlSuggestRequest {
    /// Partial question or context
    pub partial: String,
    /// Database context
    pub database: Option<String>,
    /// Schema context
    pub schema: Option<String>,
    /// Maximum suggestions
    pub limit: Option<usize>,
}

/// Suggestion response list
#[derive(Debug, Serialize)]
pub struct NlSuggestResponse {
    pub suggestions: Vec<QuerySuggestionResponse>,
}

/// Single suggestion
#[derive(Debug, Serialize)]
pub struct QuerySuggestionResponse {
    /// Suggested question
    pub question: String,
    /// Category
    pub category: String,
    /// Complexity
    pub complexity: String,
}

/// Query parameters for schema endpoint
#[derive(Debug, Deserialize)]
pub struct SchemaQueryParams {
    /// Database name
    pub database: Option<String>,
    /// Schema name
    pub schema: Option<String>,
    /// Include only specific tables
    pub tables: Option<String>,
    /// Include sample values
    pub include_samples: Option<bool>,
}

// ============================================================================
// Handler Implementations
// ============================================================================

/// Convert natural language to SQL
///
/// POST /v1/nl/query
pub async fn nl_to_sql(
    State(state): State<AppState>,
    Json(request): Json<NlQueryApiRequest>,
) -> Result<Json<NlQueryApiResponse>, ApiError> {
    let start = Instant::now();
    info!("NL Query request: {}", request.question);

    // Build schema context
    let schema_context = build_schema_context(&state, &request).await?;

    // Build conversation context
    let conversation_context = request.context.map(|entries| {
        ConversationContext {
            history: entries.into_iter().map(|e| QueryHistoryEntry {
                question: e.question,
                sql: e.sql,
                success: e.success,
                timestamp: None,
            }).collect(),
            entities: None,
            session_id: request.session_id.clone(),
        }
    });

    // Build config
    let config = build_config(request.config);

    // Create NL query request
    let nl_request = NlQueryRequest {
        question: request.question.clone(),
        schema: Some(schema_context),
        context: conversation_context,
        config: Some(config),
        user_id: None, // Would come from auth
        tenant_id: None, // Would come from tenant context
        metadata: None,
    };

    // Get or create NL query engine
    let engine = get_nl_engine(&state)?;

    // Translate
    let response = engine.translate(nl_request).await
        .map_err(|e| ApiError::internal(format!("NL translation failed: {}", e)))?;

    // Build API response
    let api_response = NlQueryApiResponse {
        sql: response.sql,
        explanation: response.explanation,
        confidence: response.confidence,
        intent: format!("{:?}", response.analysis.intent).to_lowercase(),
        tables: response.analysis.tables,
        valid: response.validation.as_ref().map(|v| v.allowed).unwrap_or(true),
        validation_errors: response.validation
            .as_ref()
            .map(|v| v.errors.iter().map(|e| e.message.clone()).collect())
            .unwrap_or_default(),
        warnings: response.warnings,
        suggestions: response.suggestions
            .unwrap_or_default()
            .into_iter()
            .map(|s| SuggestionResponse {
                text: s.text,
                sql: s.sql,
                reason: s.reason,
            })
            .collect(),
        processing_time_ms: start.elapsed().as_millis() as u64,
        cached: response.cached,
    };

    info!(
        "NL Query completed in {}ms, confidence: {:.2}",
        api_response.processing_time_ms,
        api_response.confidence
    );

    Ok(Json(api_response))
}

/// Convert natural language to SQL and execute
///
/// POST /v1/nl/execute
pub async fn nl_execute(
    State(state): State<AppState>,
    Json(request): Json<NlExecuteRequest>,
) -> Result<Json<NlExecuteResponse>, ApiError> {
    let total_start = Instant::now();
    info!("NL Execute request: {}", request.question);

    // First, translate NL to SQL
    let nl_start = Instant::now();

    let schema_context = build_schema_context_from_execute(&state, &request).await?;
    let conversation_context = request.context.map(|entries| {
        ConversationContext {
            history: entries.into_iter().map(|e| QueryHistoryEntry {
                question: e.question,
                sql: e.sql,
                success: e.success,
                timestamp: None,
            }).collect(),
            entities: None,
            session_id: request.session_id.clone(),
        }
    });

    let config = build_config(request.config);

    let nl_request = NlQueryRequest {
        question: request.question.clone(),
        schema: Some(schema_context),
        context: conversation_context,
        config: Some(config.clone()),
        user_id: None,
        tenant_id: None,
        metadata: None,
    };

    let engine = get_nl_engine(&state)?;
    let nl_response = engine.translate(nl_request).await
        .map_err(|e| ApiError::internal(format!("NL translation failed: {}", e)))?;

    let nl_processing_time = nl_start.elapsed().as_millis() as u64;

    // Check if valid
    if let Some(ref v) = nl_response.validation {
        if !v.allowed {
            return Err(ApiError::bad_request(format!(
                "Generated SQL is not valid: {}",
                v.errors.iter().map(|e| e.message.as_str()).collect::<Vec<_>>().join(", ")
            )));
        }
    }

    // Execute the SQL
    let sql_start = Instant::now();
    let branch = request.branch.as_deref().unwrap_or("main");

    // Add limit if not present
    let sql = if request.limit.is_some() && !nl_response.sql.to_uppercase().contains("LIMIT") {
        format!("{} LIMIT {}", nl_response.sql, request.limit.unwrap_or(1000))
    } else {
        nl_response.sql.clone()
    };

    // Execute query
    let tuples = state.db.query(&sql, &[])
        .map_err(|e| {
            error!("SQL execution failed: {}", e);
            ApiError::from(e)
        })?;

    let sql_execution_time = sql_start.elapsed().as_millis() as u64;

    // Build response
    let (columns, column_types, rows) = if tuples.is_empty() {
        (vec![], vec![], vec![])
    } else if let Some(first) = tuples.first() {
        let cols: Vec<String> = (0..first.values.len())
            .map(|i| format!("column_{}", i))
            .collect();
        let types: Vec<String> = first.values.iter()
            .map(|v| format!("{:?}", v).split('(').next().unwrap_or("unknown").to_lowercase())
            .collect();

        let rows: Vec<HashMap<String, serde_json::Value>> = tuples.iter().map(|t| {
            let mut row = HashMap::new();
            for (i, v) in t.values.iter().enumerate() {
                let json_val: serde_json::Value = v.into();
                row.insert(cols.get(i).cloned().unwrap_or_default(), json_val);
            }
            row
        }).collect();

        (cols, types, rows)
    } else {
        (vec![], vec![], vec![])
    };

    let total_time = total_start.elapsed().as_millis() as u64;

    let response = NlExecuteResponse {
        sql: nl_response.sql,
        explanation: nl_response.explanation,
        confidence: nl_response.confidence,
        columns,
        column_types,
        row_count: rows.len(),
        rows,
        nl_processing_time_ms: nl_processing_time,
        sql_execution_time_ms: sql_execution_time,
        total_time_ms: total_time,
        warnings: nl_response.warnings,
    };

    info!(
        "NL Execute completed: {} rows in {}ms (NL: {}ms, SQL: {}ms)",
        response.row_count,
        total_time,
        nl_processing_time,
        sql_execution_time
    );

    Ok(Json(response))
}

/// Explain a SQL query in natural language
///
/// POST /v1/nl/explain
pub async fn nl_explain(
    State(state): State<AppState>,
    Json(request): Json<NlExplainRequest>,
) -> Result<Json<NlExplainResponse>, ApiError> {
    info!("NL Explain request for SQL: {}", request.sql);

    let sql_upper = request.sql.to_uppercase();

    // Parse SQL to extract components (simplified)
    let operation = if sql_upper.starts_with("SELECT") {
        "SELECT"
    } else if sql_upper.starts_with("INSERT") {
        "INSERT"
    } else if sql_upper.starts_with("UPDATE") {
        "UPDATE"
    } else if sql_upper.starts_with("DELETE") {
        "DELETE"
    } else {
        "UNKNOWN"
    };

    // Extract tables (simplified regex-based extraction)
    let tables = extract_tables_from_sql(&request.sql);
    let conditions = extract_conditions_from_sql(&request.sql);
    let joins = extract_joins_from_sql(&request.sql);

    // Build explanation
    let mut explanation_parts = Vec::new();

    match operation {
        "SELECT" => {
            explanation_parts.push(format!(
                "This query retrieves data from {}.",
                if tables.is_empty() {
                    "the database".to_string()
                } else {
                    format!("the {} table(s)", tables.join(", "))
                }
            ));
        }
        "INSERT" => {
            explanation_parts.push("This query inserts new data.".to_string());
        }
        "UPDATE" => {
            explanation_parts.push("This query updates existing data.".to_string());
        }
        "DELETE" => {
            explanation_parts.push("This query deletes data.".to_string());
        }
        _ => {
            explanation_parts.push("This is a database operation.".to_string());
        }
    }

    if !conditions.is_empty() {
        explanation_parts.push(format!(
            "It filters results where {}.",
            conditions.join(" and ")
        ));
    }

    if !joins.is_empty() {
        explanation_parts.push(format!(
            "It combines data using {} join(s).",
            joins.len()
        ));
    }

    // Extract limit
    let limit = if let Some(pos) = sql_upper.find("LIMIT") {
        let after = &request.sql[pos + 5..];
        after.trim().split_whitespace().next()
            .and_then(|s| s.parse::<usize>().ok())
    } else {
        None
    };

    if let Some(lim) = limit {
        explanation_parts.push(format!("Results are limited to {} rows.", lim));
    }

    // Build suggestions
    let mut suggestions = Vec::new();
    if limit.is_none() && operation == "SELECT" {
        suggestions.push("Consider adding a LIMIT clause for large tables.".to_string());
    }
    if sql_upper.contains("SELECT *") {
        suggestions.push("Consider selecting specific columns instead of *.".to_string());
    }

    let response = NlExplainResponse {
        explanation: explanation_parts.join(" "),
        breakdown: QueryBreakdown {
            operation: operation.to_string(),
            tables,
            columns: extract_columns_from_sql(&request.sql),
            conditions,
            joins,
            aggregations: extract_aggregations_from_sql(&request.sql),
            order_by: extract_order_by_from_sql(&request.sql),
            limit,
        },
        suggestions,
    };

    Ok(Json(response))
}

/// Get schema context for NL queries
///
/// GET /v1/nl/schema
pub async fn get_schema_context(
    State(state): State<AppState>,
    Query(params): Query<SchemaQueryParams>,
) -> Result<Json<SchemaContextResponse>, ApiError> {
    info!("Getting schema context for NL queries");

    // Get tables from catalog
    let tables_result = state.db.list_tables()
        .map_err(|e| ApiError::internal(format!("Failed to list tables: {}", e)))?;

    let filter_tables: Option<Vec<String>> = params.tables
        .map(|t| t.split(',').map(|s| s.trim().to_string()).collect());

    let mut tables = Vec::new();
    for table_name in tables_result {
        // Filter if specific tables requested
        if let Some(ref filter) = filter_tables {
            if !filter.iter().any(|f| f.eq_ignore_ascii_case(&table_name)) {
                continue;
            }
        }

        // Get table schema
        if let Ok(schema) = state.db.get_table_schema(&table_name) {
            let columns: Vec<ColumnSchemaResponse> = schema.columns.iter().map(|c| {
                ColumnSchemaResponse {
                    name: c.name.clone(),
                    data_type: format!("{:?}", c.data_type),
                    nullable: c.nullable,
                    description: None,
                    is_primary_key: c.primary_key,
                }
            }).collect();

            let primary_key: Vec<String> = schema.columns.iter()
                .filter(|c| c.primary_key)
                .map(|c| c.name.clone())
                .collect();

            tables.push(TableSchemaResponse {
                name: table_name,
                description: None,
                columns,
                primary_key: if primary_key.is_empty() { None } else { Some(primary_key) },
                foreign_keys: None, // Would need FK introspection
                row_count: None, // Would need count query
            });
        }
    }

    let response = SchemaContextResponse {
        tables,
        database: params.database,
        schema: params.schema,
    };

    Ok(Json(response))
}

/// Get query suggestions based on partial input
///
/// POST /v1/nl/suggest
pub async fn nl_suggest(
    State(state): State<AppState>,
    Json(request): Json<NlSuggestRequest>,
) -> Result<Json<NlSuggestResponse>, ApiError> {
    info!("NL Suggest request: {}", request.partial);

    let limit = request.limit.unwrap_or(5);
    let partial_lower = request.partial.to_lowercase();

    // Get table names for context-aware suggestions
    let tables = state.db.list_tables().unwrap_or_default();

    let mut suggestions = Vec::new();

    // Generate suggestions based on partial input
    if partial_lower.contains("how many") || partial_lower.contains("count") {
        for table in tables.iter().take(3) {
            suggestions.push(QuerySuggestionResponse {
                question: format!("How many records are in {}?", table),
                category: "count".to_string(),
                complexity: "simple".to_string(),
            });
        }
    }

    if partial_lower.contains("show") || partial_lower.contains("list") || partial_lower.contains("get") {
        for table in tables.iter().take(3) {
            suggestions.push(QuerySuggestionResponse {
                question: format!("Show all records from {}", table),
                category: "select".to_string(),
                complexity: "simple".to_string(),
            });
            suggestions.push(QuerySuggestionResponse {
                question: format!("Show the top 10 records from {}", table),
                category: "select".to_string(),
                complexity: "simple".to_string(),
            });
        }
    }

    if partial_lower.contains("average") || partial_lower.contains("avg") || partial_lower.contains("total") || partial_lower.contains("sum") {
        suggestions.push(QuerySuggestionResponse {
            question: "What is the average value?".to_string(),
            category: "aggregate".to_string(),
            complexity: "medium".to_string(),
        });
        suggestions.push(QuerySuggestionResponse {
            question: "What is the total sum?".to_string(),
            category: "aggregate".to_string(),
            complexity: "medium".to_string(),
        });
    }

    if partial_lower.contains("group") || partial_lower.contains("by") {
        suggestions.push(QuerySuggestionResponse {
            question: "Group records by category".to_string(),
            category: "group".to_string(),
            complexity: "medium".to_string(),
        });
    }

    // Add generic suggestions if not enough specific ones
    if suggestions.len() < limit {
        for table in tables.iter() {
            if suggestions.len() >= limit {
                break;
            }
            suggestions.push(QuerySuggestionResponse {
                question: format!("Find records in {} where...", table),
                category: "search".to_string(),
                complexity: "simple".to_string(),
            });
        }
    }

    // Limit results
    suggestions.truncate(limit);

    Ok(Json(NlSuggestResponse { suggestions }))
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Build schema context from API request
async fn build_schema_context(
    state: &AppState,
    request: &NlQueryApiRequest,
) -> Result<SchemaContext, ApiError> {
    let tables_list = if let Some(ref tables) = request.tables {
        tables.clone()
    } else {
        state.db.list_tables()
            .map_err(|e| ApiError::internal(format!("Failed to list tables: {}", e)))?
    };

    let mut tables = Vec::new();
    for table_name in tables_list {
        if let Ok(schema) = state.db.get_table_schema(&table_name) {
            let columns: Vec<ColumnSchema> = schema.columns.iter().map(|c| {
                ColumnSchema {
                    name: c.name.clone(),
                    data_type: format!("{:?}", c.data_type),
                    nullable: c.nullable,
                    description: None,
                    default_value: None,
                    is_primary_key: c.primary_key,
                    is_unique: false,
                    enum_values: None,
                }
            }).collect();

            let primary_key: Vec<String> = schema.columns.iter()
                .filter(|c| c.primary_key)
                .map(|c| c.name.clone())
                .collect();

            tables.push(TableSchema {
                name: table_name,
                description: None,
                columns,
                primary_key: if primary_key.is_empty() { None } else { Some(primary_key) },
                foreign_keys: None,
                indexes: None,
                sample_values: None,
                row_count: None,
            });
        }
    }

    Ok(SchemaContext {
        tables,
        database: request.database.clone(),
        schema: request.schema.clone(),
        hints: None,
    })
}

/// Build schema context from execute request
async fn build_schema_context_from_execute(
    state: &AppState,
    request: &NlExecuteRequest,
) -> Result<SchemaContext, ApiError> {
    let tables_list = if let Some(ref tables) = request.tables {
        tables.clone()
    } else {
        state.db.list_tables()
            .map_err(|e| ApiError::internal(format!("Failed to list tables: {}", e)))?
    };

    let mut tables = Vec::new();
    for table_name in tables_list {
        if let Ok(schema) = state.db.get_table_schema(&table_name) {
            let columns: Vec<ColumnSchema> = schema.columns.iter().map(|c| {
                ColumnSchema {
                    name: c.name.clone(),
                    data_type: format!("{:?}", c.data_type),
                    nullable: c.nullable,
                    description: None,
                    default_value: None,
                    is_primary_key: c.primary_key,
                    is_unique: false,
                    enum_values: None,
                }
            }).collect();

            let primary_key: Vec<String> = schema.columns.iter()
                .filter(|c| c.primary_key)
                .map(|c| c.name.clone())
                .collect();

            tables.push(TableSchema {
                name: table_name,
                description: None,
                columns,
                primary_key: if primary_key.is_empty() { None } else { Some(primary_key) },
                foreign_keys: None,
                indexes: None,
                sample_values: None,
                row_count: None,
            });
        }
    }

    Ok(SchemaContext {
        tables,
        database: request.database.clone(),
        schema: request.schema.clone(),
        hints: None,
    })
}

/// Build NL query config from overrides
fn build_config(overrides: Option<NlQueryConfigOverride>) -> NlQueryConfig {
    let mut config = NlQueryConfig::default();

    if let Some(o) = overrides {
        if let Some(dialect) = o.dialect {
            config.dialect = match dialect.to_lowercase().as_str() {
                "postgresql" | "postgres" => SqlDialect::PostgreSQL,
                "mysql" => SqlDialect::MySQL,
                "sqlite" => SqlDialect::SQLite,
                "mssql" | "sqlserver" => SqlDialect::MSSQL,
                "oracle" => SqlDialect::Oracle,
                "heliosdb" => SqlDialect::HeliosDB,
                _ => SqlDialect::PostgreSQL,
            };
        }
        if let Some(max) = o.max_results {
            config.max_results = max;
        }
        if let Some(validate) = o.validate_sql {
            config.validate_sql = validate;
        }
        if let Some(explain) = o.explain_results {
            config.explain_results = explain;
        }
        if let Some(temp) = o.temperature {
            config.temperature = temp;
        }
        if let Some(model) = o.model {
            config.model = Some(model);
        }
    }

    config
}

/// Get or create NL query engine
fn get_nl_engine(state: &AppState) -> Result<Arc<NlQueryEngine>, ApiError> {
    // In a real implementation, this would be cached in AppState
    // For now, we create a mock provider
    use crate::ai::providers::{LlmProviderConfig, ProviderRegistry};

    // Try to get configured provider
    let provider_config = LlmProviderConfig {
        provider: "ollama".to_string(), // Default to Ollama for local
        api_key: None,
        endpoint: Some("http://localhost:11434".to_string()),
        model: Some("llama3.2".to_string()),
        organization: None,
        deployment: None,
        api_version: None,
        timeout_ms: Some(30000),
        max_retries: Some(3),
        headers: None,
    };

    let provider = ProviderRegistry::from_config(&provider_config)
        .map_err(|e| ApiError::internal(format!("Failed to create LLM provider: {}", e)))?;

    Ok(Arc::new(NlQueryEngine::new(provider)))
}

/// Extract tables from SQL
fn extract_tables_from_sql(sql: &str) -> Vec<String> {
    let mut tables = Vec::new();
    let re = regex::Regex::new(r"(?i)\b(?:FROM|JOIN|INTO|UPDATE)\s+([a-zA-Z_][a-zA-Z0-9_]*)").ok();

    if let Some(re) = re {
        for cap in re.captures_iter(sql) {
            if let Some(m) = cap.get(1) {
                let table = m.as_str().to_string();
                if !tables.contains(&table) {
                    tables.push(table);
                }
            }
        }
    }

    tables
}

/// Extract conditions from SQL
fn extract_conditions_from_sql(sql: &str) -> Vec<String> {
    let mut conditions = Vec::new();

    if let Some(where_pos) = sql.to_uppercase().find("WHERE") {
        let after = &sql[where_pos + 5..];
        // Find end (ORDER BY, GROUP BY, LIMIT, or end)
        let end = after.to_uppercase()
            .find("ORDER BY")
            .or_else(|| after.to_uppercase().find("GROUP BY"))
            .or_else(|| after.to_uppercase().find("LIMIT"))
            .unwrap_or(after.len());

        let where_clause = after[..end].trim();
        // Split by AND/OR
        for part in where_clause.split(['(', ')']) {
            let trimmed = part.trim();
            if !trimmed.is_empty() && !trimmed.eq_ignore_ascii_case("AND") && !trimmed.eq_ignore_ascii_case("OR") {
                conditions.push(trimmed.to_string());
            }
        }
    }

    conditions
}

/// Extract joins from SQL
fn extract_joins_from_sql(sql: &str) -> Vec<String> {
    let mut joins = Vec::new();
    let re = regex::Regex::new(r"(?i)((?:LEFT|RIGHT|INNER|OUTER|CROSS|FULL)?\s*JOIN\s+[^\s]+\s+(?:ON\s+[^,]+)?)").ok();

    if let Some(re) = re {
        for cap in re.captures_iter(sql) {
            if let Some(m) = cap.get(1) {
                joins.push(m.as_str().trim().to_string());
            }
        }
    }

    joins
}

/// Extract columns from SQL
fn extract_columns_from_sql(sql: &str) -> Vec<String> {
    let upper = sql.to_uppercase();
    if let Some(select_pos) = upper.find("SELECT") {
        if let Some(from_pos) = upper.find("FROM") {
            let columns_part = &sql[select_pos + 6..from_pos];
            return columns_part.split(',')
                .map(|c| c.trim().to_string())
                .filter(|c| !c.is_empty())
                .collect();
        }
    }
    Vec::new()
}

/// Extract aggregations from SQL
fn extract_aggregations_from_sql(sql: &str) -> Vec<String> {
    let mut aggs = Vec::new();
    let re = regex::Regex::new(r"(?i)(COUNT|SUM|AVG|MIN|MAX)\s*\([^)]+\)").ok();

    if let Some(re) = re {
        for cap in re.captures_iter(sql) {
            if let Some(m) = cap.get(0) {
                aggs.push(m.as_str().to_string());
            }
        }
    }

    aggs
}

/// Extract ORDER BY from SQL
fn extract_order_by_from_sql(sql: &str) -> Option<String> {
    let upper = sql.to_uppercase();
    if let Some(pos) = upper.find("ORDER BY") {
        let after = &sql[pos + 8..];
        let end = after.to_uppercase()
            .find("LIMIT")
            .unwrap_or(after.len());
        return Some(after[..end].trim().to_string());
    }
    None
}