sz-orm-ai 2.4.0

SZ-ORM AI Extension - Embedding, Vector Store, RAG Engine
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
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
//! NL→SQL(自然语言转 SQL)模块
//!
//! 提供将自然语言查询转换为 SQL 语句的能力,支持:
//! - 内存模拟引擎 [`SimpleNl2SqlEngine`](基于规则匹配,无需外部 API)
//! - 真实 LLM 引擎 [`OpenAINl2SqlEngine`](调用 OpenAI 兼容 API,需 `real` feature)
//!
//! 所有生成的 SQL 均经过安全验证:只允许 SELECT 查询,并检测注入风险。

use async_trait::async_trait;
use thiserror::Error;

use crate::safety;

#[cfg(feature = "real")]
use serde::{Deserialize, Serialize};

// ==================== 数据结构 ====================

/// NL→SQL 查询结果
#[derive(Debug, Clone)]
pub struct SqlQuery {
    /// 生成的 SQL(使用 $1, $2 等参数化占位符)
    pub sql: String,
    /// 生成过程的自然语言解释
    pub explanation: String,
    /// 置信度(0.0 ~ 1.0)
    pub confidence: f32,
}

/// Schema 上下文,描述数据库中的表和列信息
#[derive(Debug, Clone, Default)]
pub struct SchemaContext {
    pub tables: Vec<TableInfo>,
}

/// 表信息
#[derive(Debug, Clone)]
pub struct TableInfo {
    pub name: String,
    pub columns: Vec<ColumnInfo>,
}

/// 列信息
#[derive(Debug, Clone)]
pub struct ColumnInfo {
    pub name: String,
    pub data_type: String,
    pub nullable: bool,
    pub is_primary_key: bool,
}

// ==================== 错误类型 ====================

/// NL→SQL 相关错误
#[derive(Debug, Error)]
pub enum Nl2SqlError {
    /// 无法解析自然语言查询
    #[error("Invalid query: {0}")]
    InvalidQuery(String),
    /// Schema 信息不足(表/列不存在)
    #[error("Schema error: {0}")]
    SchemaError(String),
    /// SQL 安全验证失败
    #[error("Safety error: {0}")]
    SafetyError(String),
    /// SQL 生成失败
    #[error("Generation error: {0}")]
    GenerationError(String),
    /// API 调用错误(仅 real feature)
    #[error("API error (status {0}): {1}")]
    ApiError(u16, String),
    /// 网络错误(仅 real feature)
    #[error("Network error: {0}")]
    NetworkError(String),
    /// 配置错误
    #[error("Config error: {0}")]
    ConfigError(String),
}

// ==================== Trait 定义 ====================

/// NL→SQL 引擎 trait
///
/// 所有 NL→SQL 实现必须实现此 trait,以保证一致的接口。
#[async_trait]
pub trait Nl2SqlEngine: Send + Sync {
    /// 将自然语言查询转换为 SQL
    ///
    /// # 参数
    /// - `nl_query`: 自然语言查询(如 "show all users where age > 25")
    /// - `schema`: 数据库 schema 上下文
    ///
    /// # 返回值
    /// - `Ok(SqlQuery)`: 生成的 SQL 查询
    /// - `Err(Nl2SqlError)`: 转换失败
    async fn generate(
        &self,
        nl_query: &str,
        schema: &SchemaContext,
    ) -> Result<SqlQuery, Nl2SqlError>;

    /// 验证生成的 SQL 是否安全可用
    ///
    /// 执行以下检查:
    /// - 只允许 SELECT 语句
    /// - 无 SQL 注入风险
    async fn validate(&self, query: &SqlQuery) -> Result<bool, Nl2SqlError>;
}

// ==================== SimpleNl2SqlEngine ====================

/// 基于规则匹配的 NL→SQL 引擎(内存模拟,无需 LLM API)
///
/// 通过关键词匹配和模式识别,将常见自然语言查询转换为 SQL。
/// 支持以下查询模式:
/// - 简单 SELECT(`SELECT *` / `SELECT col1, col2`)
/// - COUNT / COUNT DISTINCT
/// - 聚合函数(SUM, AVG, MIN, MAX)
/// - WHERE 条件(=, >, <, >=, <=, !=, LIKE)
/// - ORDER BY(ASC / DESC)
/// - GROUP BY
/// - JOIN(通过外键名称推断关联)
/// - LIMIT
///
/// 所有生成的 SQL 使用 `$1`, `$2` 等参数化占位符防止注入。
///
/// # 示例
///
/// ```ignore
/// use sz_orm_ai::nl2sql::{SimpleNl2SqlEngine, Nl2SqlEngine, SchemaContext, TableInfo, ColumnInfo};
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let engine = SimpleNl2SqlEngine::new();
/// let schema = SchemaContext {
///     tables: vec![TableInfo {
///         name: "users".into(),
///         columns: vec![
///             ColumnInfo { name: "id".into(), data_type: "INTEGER".into(), nullable: false, is_primary_key: true },
///             ColumnInfo { name: "name".into(), data_type: "TEXT".into(), nullable: true, is_primary_key: false },
///         ],
///     }],
/// };
/// let result = engine.generate("show all users", &schema).await?;
/// assert_eq!(result.sql, "SELECT * FROM users");
/// # Ok(())
/// # }
/// ```
pub struct SimpleNl2SqlEngine {
    /// 表别名映射(如 "user" → "users")
    aliases: std::collections::HashMap<String, String>,
}

impl Default for SimpleNl2SqlEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl SimpleNl2SqlEngine {
    pub fn new() -> Self {
        Self {
            aliases: std::collections::HashMap::new(),
        }
    }

    /// 注册表别名(例如 `with_alias("user", "users")`)
    pub fn with_alias(mut self, alias: &str, table: &str) -> Self {
        self.aliases
            .insert(alias.to_lowercase(), table.to_lowercase());
        self
    }

    /// 在 schema 中查找表名(匹配原名或别名)
    fn find_table<'a>(&self, query: &str, schema: &'a SchemaContext) -> Option<&'a TableInfo> {
        let lower = query.to_lowercase();

        // 按名称在 query 中出现的顺序打分
        let mut best: Option<&TableInfo> = None;
        let mut best_pos: Option<usize> = None;

        for table in &schema.tables {
            let name = table.name.to_lowercase();
            if let Some(pos) = lower.find(&name) {
                let is_better = match best_pos {
                    None => true,
                    Some(best) => pos < best,
                };
                if is_better {
                    best = Some(table);
                    best_pos = Some(pos);
                }
            }
        }

        // 检查别名:遍历所有别名,看查询中是否包含别名
        for (alias, target_table_name) in &self.aliases {
            if let Some(pos) = lower.find(alias) {
                // 找到对应表
                if let Some(table) = schema
                    .tables
                    .iter()
                    .find(|t| t.name.to_lowercase() == *target_table_name)
                {
                    let is_better = match best_pos {
                        None => true,
                        Some(best) => pos < best,
                    };
                    if is_better {
                        best = Some(table);
                        best_pos = Some(pos);
                    }
                }
            }
        }

        best
    }

    /// 只在 SELECT/NL 部分提取列名(排除 WHERE/ORDER BY/GROUP BY 后的部分)
    fn extract_columns_from_nl(query: &str, table: &TableInfo) -> Vec<String> {
        let lower = query.to_lowercase();
        // 截取到第一个子句关键字之前
        let select_part = if let Some(pos) = lower.find(" where ") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" having ") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" order by") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" sorted by") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" sort by") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" ordered by") {
            &query[..pos]
        } else if let Some(pos) = lower.find(" group by") {
            &query[..pos]
        } else {
            query
        };

        Self::extract_columns(select_part, table)
    }

    /// 确认 query 中提到的表名(不含 FROM 子句的上下文检测)
    fn find_mentioned_table<'a>(
        &self,
        query: &str,
        schema: &'a SchemaContext,
    ) -> Option<&'a TableInfo> {
        self.find_table(query, schema)
    }

    /// 在 query 中查找提及的所有表名(用于 JOIN 检测)
    fn find_all_tables<'a>(&self, query: &str, schema: &'a SchemaContext) -> Vec<&'a TableInfo> {
        let lower = query.to_lowercase();
        let mut found = Vec::new();

        for table in &schema.tables {
            let name = table.name.to_lowercase();
            if lower.contains(&name) {
                found.push(table);
            }
        }

        found
    }

    /// 检查列名是否存在于 schema 的指定表中
    fn column_exists(col: &str, table: &TableInfo) -> bool {
        let col_lower = col.to_lowercase();
        table
            .columns
            .iter()
            .any(|c| c.name.to_lowercase() == col_lower)
    }

    /// 从 query 中提取列名列表(匹配 schema 中的列)
    fn extract_columns(query: &str, table: &TableInfo) -> Vec<String> {
        let lower = query.to_lowercase();
        let mut columns = Vec::new();

        for col in &table.columns {
            let col_lower = col.name.to_lowercase();
            // 避免匹配短列名导致误匹配(如 "id")
            if col_lower.len() >= 2
                && (lower.contains(&col_lower) || lower.contains(&format!(" {} ", col_lower)))
            {
                // 确认不是 from 子句后的表名列
                if !columns.contains(&col.name) {
                    columns.push(col.name.clone());
                }
            }
        }

        columns
    }

    /// 尝试寻找两个表之间的外键关联列
    fn find_join_columns(t1: &TableInfo, t2: &TableInfo) -> Option<(String, String)> {
        // 模式 1: t2 中有 t1_name + _id → t2.t1_name_id = t1.id
        for col1 in &t1.columns {
            if col1.is_primary_key {
                let expected_fk = format!("{}_{}", t2.name, col1.name).to_lowercase();
                for col2 in &t2.columns {
                    if col2.name.to_lowercase() == expected_fk {
                        return Some((
                            format!("{}.{}", t1.name, col1.name),
                            format!("{}.{}", t2.name, col2.name),
                        ));
                    }
                }
            }
        }
        // 模式 2: t1 中有 t2_name + _id → t1.t2_name_id = t2.id
        for col2 in &t2.columns {
            if col2.is_primary_key {
                let expected_fk = format!("{}_{}", t1.name, col2.name).to_lowercase();
                for col1 in &t1.columns {
                    if col1.name.to_lowercase() == expected_fk {
                        return Some((
                            format!("{}.{}", t1.name, col1.name),
                            format!("{}.{}", t2.name, col2.name),
                        ));
                    }
                }
            }
        }
        None
    }

    /// 从 WHERE 条件文本中提取参数化条件
    fn parse_conditions(where_text: &str, table: &TableInfo) -> (Vec<String>, Vec<String>) {
        let text = where_text.trim();
        let mut conditions = Vec::new();
        let mut params = Vec::new();
        let param_idx = 1;

        // 处理单个条件表达式
        let expr = text;
        if expr.is_empty() {
            return (conditions, params);
        }

        let operators = [
            (">=", ">="),
            ("<=", "<="),
            ("!=", "!="),
            ("=", "="),
            (">", ">"),
            ("<", "<"),
        ];

        let mut resolved = false;
        for (op_str, sql_op) in &operators {
            if let Some(pos) = expr.find(op_str) {
                let field = expr[..pos].trim();
                let raw_val = expr[pos + op_str.len()..].trim();

                if !field.is_empty() && !raw_val.is_empty() {
                    // 检查 raw_val 是否为列名
                    if Self::column_exists(raw_val, table) {
                        conditions.push(format!("{} {} {}", field, sql_op, raw_val));
                    } else {
                        let param = format!("${}", param_idx);
                        conditions.push(format!("{} {} {}", field, sql_op, param));
                        params.push(raw_val.to_string());
                    }
                    resolved = true;
                }
                break;
            }
        }

        // 处理 LIKE / contains
        if !resolved {
            let lower_expr = expr.to_lowercase();
            if let Some(pos) = lower_expr.find(" like ") {
                let field = expr[..pos].trim();
                let raw_val = expr[pos + 6..].trim();
                if !field.is_empty() && !raw_val.is_empty() {
                    let param = "$1".to_string();
                    conditions.push(format!("{} LIKE {}", field, param));
                    params.push(raw_val.to_string());
                    resolved = true;
                }
            } else if let Some(pos) = lower_expr.find(" contains ") {
                let field = expr[..pos].trim();
                let raw_val = expr[pos + 10..].trim();
                if !field.is_empty() && !raw_val.is_empty() {
                    let param = "$1".to_string();
                    conditions.push(format!("{} LIKE '%' || {} || '%'", field, param));
                    params.push(raw_val.to_string());
                    resolved = true;
                }
            }
        }

        // 未解析的条件原样保留
        if !resolved && !expr.is_empty() {
            conditions.push(expr.to_string());
        }

        (conditions, params)
    }
}

#[async_trait]
impl Nl2SqlEngine for SimpleNl2SqlEngine {
    async fn generate(
        &self,
        nl_query: &str,
        schema: &SchemaContext,
    ) -> Result<SqlQuery, Nl2SqlError> {
        if nl_query.trim().is_empty() {
            return Err(Nl2SqlError::InvalidQuery("自然语言查询不能为空".into()));
        }

        if schema.tables.is_empty() {
            return Err(Nl2SqlError::SchemaError("Schema 中未定义任何表".into()));
        }

        let lower = nl_query.to_lowercase();
        let table = self.find_mentioned_table(nl_query, schema).ok_or_else(|| {
            Nl2SqlError::SchemaError(format!(
                "无法在查询中识别表名,schema 包含: {}",
                schema
                    .tables
                    .iter()
                    .map(|t| t.name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            ))
        })?;

        // ============ 识别查询类型 ============

        // 是否是 COUNT 查询
        let is_count =
            lower.contains("count ") || lower.contains("how many") || lower.contains("number of");
        let is_distinct = lower.contains("distinct");
        let has_aggregation = lower.contains("sum ")
            || lower.contains("total ")
            || lower.contains("avg ")
            || lower.contains("average ")
            || lower.contains("mean ")
            || lower.contains("min ")
            || lower.contains("minimum ")
            || lower.contains("max ")
            || lower.contains("maximum ")
            || lower.contains("highest");

        // 提取 ORDER BY、GROUP BY、LIMIT、JOIN
        let has_order = lower.contains("order by")
            || lower.contains("sort by")
            || lower.contains("ordered by")
            || lower.contains("sorted by");
        let has_group = lower.contains("group by");
        let has_limit =
            lower.contains("limit") || lower.contains("top ") || lower.contains("first ");
        let has_join =
            lower.contains(" join ") || lower.contains("combine with") || lower.contains("with ");

        // ============ 提取列(仅从 SELECT/NL 部分提取,排除子句关键字后的部分) ============
        let columns = Self::extract_columns_from_nl(nl_query, table);

        // ============ 提取 WHERE 条件 ============
        let where_text = if let Some(pos) = lower.find(" where ") {
            Some(nl_query[pos + 7..].trim())
        } else if let Some(pos) = lower.find(" having ") {
            Some(nl_query[pos + 8..].trim())
        } else if let Some(pos) = lower.find(" with ") {
            // "with" 可能表示条件或 JOIN
            let after = nl_query[pos + 5..].trim();
            // 如果 with 后跟列名和操作符,视为条件
            if table
                .columns
                .iter()
                .any(|c| after.to_lowercase().starts_with(&c.name.to_lowercase()))
            {
                Some(after)
            } else {
                None
            }
        } else {
            None
        };

        let (conditions, params) = match where_text {
            Some(text) => {
                // 去掉 order by / group by / limit 部分
                let clean_text = if let Some(pos) = text.to_lowercase().find(" order by") {
                    &text[..pos]
                } else if let Some(pos) = text.to_lowercase().find(" group by") {
                    &text[..pos]
                } else if let Some(pos) = text.to_lowercase().find(" limit") {
                    &text[..pos]
                } else {
                    text
                };
                Self::parse_conditions(clean_text, table)
            }
            None => (Vec::new(), Vec::new()),
        };

        // ============ 提取 ORDER BY ============
        let order_clause: Option<String> = if has_order {
            let (after_text, is_desc) = if let Some(pos) = lower.find("order by") {
                let txt = nl_query[pos + 8..].trim().to_string();
                let d = txt.to_lowercase().contains("desc")
                    || txt.to_lowercase().contains("descending");
                (txt, d)
            } else if let Some(pos) = lower.find("sort by") {
                let txt = nl_query[pos + 7..].trim().to_string();
                let d = txt.to_lowercase().contains("desc")
                    || txt.to_lowercase().contains("descending");
                (txt, d)
            } else if let Some(pos) = lower.find("sorted by") {
                let txt = nl_query[pos + 9..].trim().to_string();
                let d = txt.to_lowercase().contains("desc")
                    || txt.to_lowercase().contains("descending");
                (txt, d)
            } else if let Some(pos) = lower.find("ordered by") {
                let txt = nl_query[pos + 10..].trim().to_string();
                let d = txt.to_lowercase().contains("desc")
                    || txt.to_lowercase().contains("descending");
                (txt, d)
            } else {
                (String::new(), false)
            };

            let field = after_text
                .split([' ', ','])
                .next()
                .unwrap_or("")
                .trim()
                .to_string();
            if field.is_empty() {
                None
            } else if is_desc {
                Some(format!(" ORDER BY {} DESC", field))
            } else {
                Some(format!(" ORDER BY {} ASC", field))
            }
        } else {
            None
        };

        // ============ 提取 GROUP BY ============
        let group_clause = if has_group {
            let after = if let Some(pos) = lower.find("group by") {
                &nl_query[pos + 8..]
            } else {
                ""
            };
            let group_cols: Vec<String> = after
                .split(',')
                .filter_map(|s| {
                    let first = s.split_whitespace().next()?;
                    if first.is_empty() {
                        None
                    } else {
                        Some(first.to_string())
                    }
                })
                .collect();
            if group_cols.is_empty() {
                None
            } else {
                Some(format!(" GROUP BY {}", group_cols.join(", ")))
            }
        } else {
            None
        };

        // ============ 提取 LIMIT ============
        let limit_val: Option<usize> = if has_limit {
            let limit_text = if let Some(pos) = lower.find("limit ") {
                let after = &nl_query[pos + 6..].trim();
                after.split_whitespace().next()
            } else if let Some(pos) = lower.find("top ") {
                let after = &nl_query[pos + 4..].trim();
                after.split_whitespace().next()
            } else if let Some(pos) = lower.find("first ") {
                let after = &nl_query[pos + 6..].trim();
                after.split_whitespace().next()
            } else {
                None
            };
            limit_text.and_then(|s| s.parse::<usize>().ok())
        } else {
            None
        };

        // ============ 提取 JOIN ============
        let join_tables = if has_join {
            let all_tables = self.find_all_tables(nl_query, schema);
            let others: Vec<&&TableInfo> =
                all_tables.iter().filter(|t| t.name != table.name).collect();
            others.into_iter().copied().collect()
        } else {
            Vec::new()
        };

        // ============ 构建 SQL ============

        // 构建 SELECT 子句
        let select_clause = if is_count {
            if is_distinct && !columns.is_empty() {
                format!("SELECT COUNT(DISTINCT {})", columns[0])
            } else {
                "SELECT COUNT(*)".to_string()
            }
        } else if has_aggregation && !columns.is_empty() {
            // 检测具体聚合函数
            let agg_func = if lower.contains("sum ") || lower.contains("total ") {
                "SUM"
            } else if lower.contains("avg ")
                || lower.contains("average ")
                || lower.contains("mean ")
            {
                "AVG"
            } else if lower.contains("min ") || lower.contains("minimum ") {
                "MIN"
            } else if lower.contains("max ")
                || lower.contains("maximum ")
                || lower.contains("highest")
            {
                "MAX"
            } else {
                "SUM"
            };
            if group_clause.is_some() {
                // 聚合 + group by
                format!("SELECT {}, {}({})", columns.join(", "), agg_func, agg_func)
            } else if !columns.is_empty() {
                format!("SELECT {}({})", agg_func, columns[0])
            } else {
                format!("SELECT {}(*)", agg_func)
            }
        } else if !columns.is_empty() {
            format!("SELECT {}", columns.join(", "))
        } else {
            "SELECT *".to_string()
        };

        // 修正 group by 场景的 select
        let select_clause = if group_clause.is_some() && has_aggregation {
            let agg_func = if lower.contains("sum ") || lower.contains("total ") {
                "SUM"
            } else if lower.contains("avg ")
                || lower.contains("average ")
                || lower.contains("mean ")
            {
                "AVG"
            } else if lower.contains("min ") || lower.contains("minimum ") {
                "MIN"
            } else if lower.contains("max ")
                || lower.contains("maximum ")
                || lower.contains("highest")
            {
                "MAX"
            } else {
                "COUNT"
            };

            if !columns.is_empty() {
                if columns.len() >= 2 {
                    format!("SELECT {}, {}({})", columns[1], agg_func, columns[1])
                } else {
                    format!("SELECT {}, {}({})", columns[0], agg_func, columns[0])
                }
            } else {
                select_clause
            }
        } else {
            select_clause
        };

        // 构建 FROM 子句 + JOIN
        let mut from_clause = format!(" FROM {}", table.name);
        let mut join_descriptions = Vec::new();
        for join_table in &join_tables {
            if let Some((left, right)) = Self::find_join_columns(table, join_table) {
                from_clause.push_str(&format!(
                    " JOIN {} ON {} = {}",
                    join_table.name, left, right
                ));
                join_descriptions.push(format!("{} ON {} = {}", join_table.name, left, right));
            }
        }

        // 构建 WHERE 子句(参数化)
        let where_clause = if conditions.is_empty() {
            String::new()
        } else {
            // SAFETY: conditions 来自 NL2SQL 内部解析(field/op/param 由 schema 和 NLP 解析生成),非直接用户输入;AI 生成 SQL 需在执行前经 sql-validator 校验
            format!(" WHERE {}", conditions.join(" AND "))
        };

        // 组合 SQL
        let sql = format!(
            "{}{}{}{}{}{}",
            select_clause,
            from_clause,
            where_clause,
            order_clause.as_deref().unwrap_or(""),
            group_clause.as_deref().unwrap_or(""),
            limit_val
                .map(|v| format!(" LIMIT {}", v))
                .unwrap_or_default(),
        );

        // 安全验证
        if !safety::validate_select_only(&sql) {
            return Err(Nl2SqlError::SafetyError(
                "生成的 SQL 不是 SELECT 查询".into(),
            ));
        }
        if !safety::validate_no_injection(&sql) {
            return Err(Nl2SqlError::SafetyError("生成的 SQL 包含注入风险".into()));
        }
        let sql = safety::sanitize_sql(&sql);

        // 构建解释
        let mut explanation_parts = Vec::new();
        explanation_parts.push(format!("查询 {}", table.name));
        if !columns.is_empty() {
            explanation_parts.push(format!("列: {}", columns.join(", ")));
        }
        if is_count {
            explanation_parts.push("统计数量".to_string());
        }
        if !conditions.is_empty() {
            let cond_desc: Vec<String> = conditions
                .iter()
                .enumerate()
                .map(|(i, cond)| {
                    if i < params.len() {
                        cond.replace(&format!("${}", i + 1), &format!("'{}'", params[i]))
                    } else {
                        cond.clone()
                    }
                })
                .collect();
            explanation_parts.push(format!("条件: {}", cond_desc.join(", ")));
        }
        if !params.is_empty() {
            explanation_parts.push(format!(
                "参数: [{}]",
                params
                    .iter()
                    .map(|p| format!("'{}'", p))
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }
        let explanation = explanation_parts.join("");

        // 计算置信度
        // 简单的规则:明确的模式匹配给高置信度
        let mut confidence = 0.7;
        if !conditions.is_empty() || is_count {
            confidence = 0.8;
        }
        if !columns.is_empty() && !conditions.is_empty() {
            confidence = 0.9;
        }

        Ok(SqlQuery {
            sql,
            explanation,
            confidence,
        })
    }

    async fn validate(&self, query: &SqlQuery) -> Result<bool, Nl2SqlError> {
        if !safety::validate_select_only(&query.sql) {
            return Ok(false);
        }
        if !safety::validate_no_injection(&query.sql) {
            return Ok(false);
        }
        if query.confidence < 0.0 || query.confidence > 1.0 {
            return Err(Nl2SqlError::GenerationError(format!(
                "confidence 必须在 0.0~1.0 范围内,实际 {}",
                query.confidence
            )));
        }
        Ok(true)
    }
}

// ==================== OpenAINl2SqlEngine ====================

/// OpenAI 兼容的 NL→SQL 引擎(调用 LLM API)
///
/// 仅在启用 `real` feature 时编译。
/// 调用 OpenAI 兼容的 `/v1/chat/completions` 接口生成 SQL。
///
/// 生成的 SQL 经过安全验证:只允许 SELECT,并检测注入风险。
///
/// # 用法
///
/// ```ignore
/// use sz_orm_ai::nl2sql::{Nl2SqlEngine, OpenAINl2SqlEngine, SchemaContext};
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let engine = OpenAINl2SqlEngine::new("sk-xxxx")
///     .with_model("gpt-4o-mini");
/// let schema = SchemaContext::default();
/// let result = engine.generate("show all users", &schema).await?;
/// println!("SQL: {}", result.sql);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "real")]
pub struct OpenAINl2SqlEngine {
    /// API 基础地址(默认 `https://api.openai.com/v1`)
    api_base: String,
    /// API Key(Bearer token)
    api_key: String,
    /// 模型名称(默认 `gpt-4o-mini`)
    model: String,
    /// HTTP 客户端
    http_client: reqwest::Client,
}

#[cfg(feature = "real")]
impl OpenAINl2SqlEngine {
    /// 默认 API 基础地址
    const DEFAULT_API_BASE: &'static str = "https://api.openai.com/v1";
    /// 默认模型
    const DEFAULT_MODEL: &'static str = "gpt-4o-mini";

    /// 创建客户端实例
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_base: Self::DEFAULT_API_BASE.to_string(),
            api_key: api_key.into(),
            model: Self::DEFAULT_MODEL.to_string(),
            http_client: reqwest::Client::new(),
        }
    }

    /// 设置 API base URL
    pub fn with_api_base(mut self, api_base: impl Into<String>) -> Self {
        self.api_base = api_base.into();
        self
    }

    /// 设置模型名称
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    /// 校验 API key 非空
    fn ensure_api_key(&self) -> Result<(), Nl2SqlError> {
        if self.api_key.is_empty() {
            return Err(Nl2SqlError::ConfigError(
                "API key 为空,无法调用 OpenAI API".into(),
            ));
        }
        Ok(())
    }

    /// 构建 system prompt(包含 schema 信息和 SQL 生成规范)
    fn build_system_prompt(schema: &SchemaContext) -> String {
        let mut prompt = String::from(
            "You are a SQL generator. Given a database schema and a natural language query, ",
        );
        prompt.push_str("generate a valid SQL SELECT statement.\n\n");
        prompt.push_str("Rules:\n");
        prompt.push_str("- Only generate SELECT statements (no INSERT, UPDATE, DELETE, DROP, ALTER, TRUNCATE)\n");
        prompt.push_str("- Use parameterized placeholders ($1, $2, ...) for all values to prevent SQL injection\n");
        prompt.push_str("- If the query is ambiguous, choose the most likely interpretation\n");
        prompt
            .push_str("- Return ONLY the SQL statement, no explanation or markdown formatting\n\n");

        prompt.push_str("Database Schema:\n");
        for table in &schema.tables {
            prompt.push_str(&format!("CREATE TABLE {} (\n", table.name));
            for col in &table.columns {
                prompt.push_str(&format!(
                    "  {} {} {} {},\n",
                    col.name,
                    col.data_type,
                    if col.is_primary_key {
                        "PRIMARY KEY"
                    } else {
                        ""
                    },
                    if col.nullable { "NULL" } else { "NOT NULL" },
                ));
            }
            prompt.push_str(");\n\n");
        }

        prompt
    }
}

#[cfg(feature = "real")]
#[derive(Serialize)]
struct ChatCompletionRequest<'a> {
    model: &'a str,
    messages: Vec<Message<'a>>,
    temperature: f32,
    max_tokens: u32,
}

#[cfg(feature = "real")]
#[derive(Serialize)]
struct Message<'a> {
    role: &'a str,
    content: String,
}

#[cfg(feature = "real")]
#[derive(Deserialize)]
struct ChatCompletionResponse {
    choices: Vec<Choice>,
}

#[cfg(feature = "real")]
#[derive(Deserialize)]
struct Choice {
    message: ChoiceMessage,
    #[allow(dead_code)]
    finish_reason: Option<String>,
}

#[cfg(feature = "real")]
#[derive(Deserialize)]
struct ChoiceMessage {
    content: Option<String>,
}

#[cfg(feature = "real")]
#[async_trait]
impl Nl2SqlEngine for OpenAINl2SqlEngine {
    async fn generate(
        &self,
        nl_query: &str,
        schema: &SchemaContext,
    ) -> Result<SqlQuery, Nl2SqlError> {
        self.ensure_api_key()?;

        if nl_query.trim().is_empty() {
            return Err(Nl2SqlError::InvalidQuery("自然语言查询不能为空".into()));
        }

        let system_prompt = Self::build_system_prompt(schema);
        let user_message = format!(
            "Given the schema above, generate a SQL query for: {}",
            nl_query
        );

        let body = ChatCompletionRequest {
            model: &self.model,
            messages: vec![
                Message {
                    role: "system",
                    content: system_prompt,
                },
                Message {
                    role: "user",
                    content: user_message,
                },
            ],
            temperature: 0.1,
            max_tokens: 500,
        };

        let url = format!("{}/chat/completions", self.api_base);
        let resp = self
            .http_client
            .post(&url)
            .bearer_auth(&self.api_key)
            .json(&body)
            .send()
            .await
            .map_err(|e| Nl2SqlError::NetworkError(e.to_string()))?;

        let status = resp.status().as_u16();
        if !resp.status().is_success() {
            let message = resp.text().await.unwrap_or_default();
            return Err(Nl2SqlError::ApiError(status, message));
        }

        let parsed: ChatCompletionResponse = resp
            .json()
            .await
            .map_err(|e| Nl2SqlError::NetworkError(e.to_string()))?;

        let raw_sql = parsed
            .choices
            .into_iter()
            .next()
            .and_then(|c| c.message.content)
            .ok_or_else(|| Nl2SqlError::ApiError(status, "API 返回空响应".into()))?;

        // 清理 LLM 返回的 SQL(去掉 markdown 代码块标记)
        let cleaned_sql = clean_llm_sql_output(&raw_sql);

        let query = SqlQuery {
            sql: cleaned_sql.clone(),
            explanation: format!("{} 模型根据自然语言查询生成", self.model),
            confidence: 0.8,
        };

        // 安全验证
        if !safety::validate_select_only(&query.sql) {
            return Err(Nl2SqlError::SafetyError(
                "生成的 SQL 不是 SELECT 查询,已被拦截".into(),
            ));
        }
        if !safety::validate_no_injection(&query.sql) {
            return Err(Nl2SqlError::SafetyError(
                "生成的 SQL 包含注入风险,已被拦截".into(),
            ));
        }

        Ok(query)
    }

    async fn validate(&self, query: &SqlQuery) -> Result<bool, Nl2SqlError> {
        if !safety::validate_select_only(&query.sql) {
            return Ok(false);
        }
        if !safety::validate_no_injection(&query.sql) {
            return Ok(false);
        }
        if query.confidence < 0.0 || query.confidence > 1.0 {
            return Err(Nl2SqlError::GenerationError(format!(
                "confidence 必须在 0.0~1.0 范围内,实际 {}",
                query.confidence
            )));
        }
        Ok(true)
    }
}

/// 清理 LLM 输出的 SQL(移除 markdown 代码块标记和前后空白)
#[cfg(feature = "real")]
fn clean_llm_sql_output(raw: &str) -> String {
    let trimmed = raw.trim();
    // 移除 ```sql ... ``` 或 ``` ... ``` 包装
    if trimmed.starts_with("```") {
        let content = trimmed.trim_start_matches('`');
        if let Some(end) = content.rfind("```") {
            let inner = content[..end].trim();
            // 移除 "sql" 语言标记(如 ```sql\n...)
            return inner
                .strip_prefix("sql")
                .unwrap_or(inner)
                .trim()
                .to_string();
        }
        return content.trim().to_string();
    }
    trimmed.to_string()
}

// ==================== 查询优化提示 ====================

/// 优化建议的严重级别
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HintSeverity {
    /// 信息级:可选的优化建议
    Info,
    /// 警告级:可能影响性能
    Warning,
    /// 严重级:强烈建议修改
    Critical,
}

impl HintSeverity {
    /// 转换为字符串
    pub fn as_str(&self) -> &'static str {
        match self {
            HintSeverity::Info => "INFO",
            HintSeverity::Warning => "WARNING",
            HintSeverity::Critical => "CRITICAL",
        }
    }
}

/// 单条查询优化建议
#[derive(Debug, Clone)]
pub struct QueryOptimizationHint {
    /// 建议标题
    pub title: String,
    /// 详细描述
    pub description: String,
    /// 严重级别
    pub severity: HintSeverity,
    /// 优化后的 SQL 建议(可选)
    pub suggested_sql: Option<String>,
}

impl QueryOptimizationHint {
    /// 创建一条信息级建议
    pub fn info(title: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            severity: HintSeverity::Info,
            suggested_sql: None,
        }
    }

    /// 创建一条警告级建议
    pub fn warning(title: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            severity: HintSeverity::Warning,
            suggested_sql: None,
        }
    }

    /// 创建一条严重级建议
    pub fn critical(title: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            severity: HintSeverity::Critical,
            suggested_sql: None,
        }
    }

    /// 附加优化后的 SQL 建议
    pub fn with_suggested_sql(mut self, sql: impl Into<String>) -> Self {
        self.suggested_sql = Some(sql.into());
        self
    }
}

/// 查询分析结果
#[derive(Debug, Clone)]
pub struct QueryAnalysis {
    /// 原始 SQL
    pub original_sql: String,
    /// 所有优化建议
    pub hints: Vec<QueryOptimizationHint>,
    /// 预估的 SQL 复杂度评分(0-100,越高越复杂)
    pub complexity_score: u32,
    /// 检测到的表名列表
    pub detected_tables: Vec<String>,
    /// 是否包含 WHERE 子句
    pub has_where: bool,
    /// 是否包含 LIMIT 子句
    pub has_limit: bool,
    /// 是否包含 JOIN
    pub has_join: bool,
    /// 是否包含子查询
    pub has_subquery: bool,
    /// 是否使用了 SELECT *
    pub uses_select_star: bool,
}

impl QueryAnalysis {
    /// 返回严重级别为 Critical 的建议数量
    pub fn critical_count(&self) -> usize {
        self.hints
            .iter()
            .filter(|h| h.severity == HintSeverity::Critical)
            .count()
    }

    /// 返回严重级别为 Warning 的建议数量
    pub fn warning_count(&self) -> usize {
        self.hints
            .iter()
            .filter(|h| h.severity == HintSeverity::Warning)
            .count()
    }

    /// 是否存在任何建议
    pub fn has_hints(&self) -> bool {
        !self.hints.is_empty()
    }
}

/// SQL 查询优化分析器
///
/// 基于规则分析 SQL 查询,生成优化建议。
/// 不依赖外部 LLM API,纯规则匹配,适用于离线场景。
pub struct QueryOptimizer {
    /// 是否检测 SELECT *
    pub check_select_star: bool,
    /// 是否检测缺失的 LIMIT
    pub check_missing_limit: bool,
    /// 是否检测缺失的 WHERE
    pub check_missing_where: bool,
    /// LIMIT 建议的默认行数
    pub default_limit: usize,
    /// 复杂度评分中 JOIN 的权重
    pub join_weight: u32,
    /// 复杂度评分中子查询的权重
    pub subquery_weight: u32,
    /// 复杂度评分中 WHERE 条件的权重
    pub where_weight: u32,
}

impl Default for QueryOptimizer {
    fn default() -> Self {
        Self {
            check_select_star: true,
            check_missing_limit: true,
            check_missing_where: true,
            default_limit: 100,
            join_weight: 15,
            subquery_weight: 20,
            where_weight: 5,
        }
    }
}

impl QueryOptimizer {
    /// 创建新的查询优化分析器
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置默认 LIMIT 行数
    pub fn with_default_limit(mut self, limit: usize) -> Self {
        self.default_limit = limit;
        self
    }

    /// 禁用 SELECT * 检测
    pub fn disable_select_star_check(mut self) -> Self {
        self.check_select_star = false;
        self
    }

    /// 禁用缺失 LIMIT 检测
    pub fn disable_missing_limit_check(mut self) -> Self {
        self.check_missing_limit = false;
        self
    }

    /// 禁用缺失 WHERE 检测
    pub fn disable_missing_where_check(mut self) -> Self {
        self.check_missing_where = false;
        self
    }

    /// 分析 SQL 查询并生成优化建议
    ///
    /// # 参数
    /// - `sql`: 要分析的 SQL 查询语句
    /// - `schema`: 数据库 schema 上下文(用于检测表名和索引)
    pub fn analyze(&self, sql: &str, schema: &SchemaContext) -> QueryAnalysis {
        let normalized = Self::normalize_sql(sql);
        let lower = normalized.to_lowercase();

        let uses_select_star = self.detect_select_star(&lower);
        let has_where = self.detect_where(&lower);
        let has_limit = self.detect_limit(&lower);
        let has_join = self.detect_join(&lower);
        let has_subquery = self.detect_subquery(&lower);
        let detected_tables = self.extract_tables(&lower, schema);

        let mut hints = Vec::new();

        // 检测 SELECT *
        if self.check_select_star && uses_select_star {
            let columns_hint = self.suggest_columns(&detected_tables, schema);
            let mut hint = QueryOptimizationHint::warning(
                "避免使用 SELECT *",
                "SELECT * 会返回所有列,可能导致不必要的数据传输和内存消耗。建议显式指定所需列。",
            );
            if !columns_hint.is_empty() {
                hint = hint.with_suggested_sql(columns_hint);
            }
            hints.push(hint);
        }

        // 检测缺失的 WHERE
        if self.check_missing_where && !has_where {
            hints.push(QueryOptimizationHint::critical(
                "缺少 WHERE 子句",
                "查询没有 WHERE 条件,将扫描全表。对于大表这会导致严重的性能问题。",
            ));
        }

        // 检测缺失的 LIMIT
        if self.check_missing_limit && !has_limit {
            let suggested = self.add_limit_suggestion(&normalized, self.default_limit);
            hints.push(
                QueryOptimizationHint::warning(
                    "缺少 LIMIT 子句",
                    format!(
                        "查询没有 LIMIT 限制,可能返回大量数据。建议添加 LIMIT {}",
                        self.default_limit
                    ),
                )
                .with_suggested_sql(suggested),
            );
        }

        // 检测多表 JOIN 无索引建议
        if has_join {
            let join_count = Self::count_joins(&lower);
            if join_count >= 3 {
                hints.push(QueryOptimizationHint::critical(
                    "JOIN 数量过多",
                    format!(
                        "查询包含 {} 个 JOIN,可能导致性能下降。建议拆分为多个查询或使用临时表。",
                        join_count
                    ),
                ));
            } else if join_count >= 1 {
                hints.push(QueryOptimizationHint::info(
                    "JOIN 查询建议",
                    "确保 JOIN 条件涉及的列已建立索引,避免嵌套循环扫描。",
                ));
            }
        }

        // 检测子查询
        if has_subquery {
            let subquery_count = Self::count_subqueries(&lower);
            if subquery_count >= 2 {
                hints.push(QueryOptimizationHint::warning(
                    "子查询嵌套过深",
                    format!(
                        "查询包含 {} 个子查询,建议考虑使用 JOIN 重写以提高性能。",
                        subquery_count
                    ),
                ));
            }
        }

        // 检测 LIKE '%...' 前缀通配符
        if Self::has_leading_wildcard_like(&lower) {
            hints.push(QueryOptimizationHint::warning(
                "LIKE 使用前缀通配符",
                "LIKE '%keyword' 无法使用索引,会导致全表扫描。如可能,使用 LIKE 'keyword%' 或全文索引。",
            ));
        }

        // 检测 OR 条件(可能导致无法使用索引)
        if Self::count_or_conditions(&lower) >= 3 {
            hints.push(QueryOptimizationHint::info(
                "多个 OR 条件",
                "多个 OR 条件可能导致无法有效使用索引。考虑使用 UNION ALL 重写。",
            ));
        }

        // 检测 ORDER BY 无 LIMIT
        if Self::has_order_by_without_limit(&lower) {
            hints.push(QueryOptimizationHint::warning(
                "ORDER BY 无 LIMIT",
                "ORDER BY 无 LIMIT 时需要排序全部数据,可能消耗大量内存。建议添加 LIMIT。",
            ));
        }

        // 检测 COUNT(*) 建议使用 COUNT(1)
        if lower.contains("count(*)") {
            hints.push(QueryOptimizationHint::info(
                "考虑使用 COUNT(1)",
                "某些数据库中 COUNT(1) 比 COUNT(*) 略快(虽然现代优化器通常已优化)。",
            ));
        }

        // 检测缺失索引建议(基于 WHERE 条件)
        if has_where {
            let where_columns = self.extract_where_columns(&lower, schema);
            for col in &where_columns {
                if !self.column_has_index(col, schema) {
                    hints.push(QueryOptimizationHint::info(
                        format!("建议为列 {} 添加索引", col),
                        format!(
                            "WHERE 条件中使用了列 {},但该列似乎没有索引。添加索引可提高查询速度。",
                            col
                        ),
                    ));
                }
            }
        }

        // 计算复杂度评分
        let complexity_score =
            self.calculate_complexity(has_join, has_subquery, has_where, &detected_tables, &lower);

        QueryAnalysis {
            original_sql: sql.to_string(),
            hints,
            complexity_score,
            detected_tables,
            has_where,
            has_limit,
            has_join,
            has_subquery,
            uses_select_star,
        }
    }

    /// 生成优化报告文本
    pub fn format_report(analysis: &QueryAnalysis) -> String {
        let mut report = String::new();
        report.push_str("=== SQL 查询优化分析报告 ===\n\n");
        report.push_str(&format!("原始 SQL: {}\n", analysis.original_sql));
        report.push_str(&format!("复杂度评分: {}/100\n", analysis.complexity_score));
        report.push_str(&format!(
            "检测到的表: {}\n",
            if analysis.detected_tables.is_empty() {
                "".to_string()
            } else {
                analysis.detected_tables.join(", ")
            }
        ));
        report.push_str(&format!("包含 WHERE: {}\n", analysis.has_where)); // SAFETY: 报告文本拼接,非 SQL 执行
        report.push_str(&format!("包含 LIMIT: {}\n", analysis.has_limit));
        report.push_str(&format!("包含 JOIN: {}\n", analysis.has_join));
        report.push_str(&format!("包含子查询: {}\n", analysis.has_subquery));
        report.push_str(&format!("使用 SELECT *: {}\n\n", analysis.uses_select_star));

        if analysis.hints.is_empty() {
            report.push_str("✓ 未发现优化建议,查询看起来良好。\n");
        } else {
            report.push_str(&format!(
                "{} 条优化建议({} 严重,{} 警告):\n\n",
                analysis.hints.len(),
                analysis.critical_count(),
                analysis.warning_count()
            ));
            for (i, hint) in analysis.hints.iter().enumerate() {
                report.push_str(&format!(
                    "{}. [{}] {}\n   {}\n",
                    i + 1,
                    hint.severity.as_str(),
                    hint.title,
                    hint.description
                ));
                if let Some(ref sql) = hint.suggested_sql {
                    report.push_str(&format!("   建议SQL: {}\n", sql));
                }
                report.push('\n');
            }
        }

        report
    }

    // ---- 内部辅助方法 ----

    /// 规范化 SQL(去除多余空白、换行)
    fn normalize_sql(sql: &str) -> String {
        sql.split_whitespace().collect::<Vec<_>>().join(" ")
    }

    /// 检测 SELECT *
    fn detect_select_star(&self, lower: &str) -> bool {
        lower.contains("select *") || lower.contains("select  *")
    }

    /// 检测 WHERE 子句
    fn detect_where(&self, lower: &str) -> bool {
        lower.contains(" where ")
    }

    /// 检测 LIMIT 子句
    fn detect_limit(&self, lower: &str) -> bool {
        lower.contains(" limit ")
    }

    /// 检测 JOIN
    fn detect_join(&self, lower: &str) -> bool {
        lower.contains(" join ")
            || lower.contains(" inner join ")
            || lower.contains(" left join ")
            || lower.contains(" right join ")
            || lower.contains(" full join ")
            || lower.contains(" cross join ")
    }

    /// 检测子查询(括号内的 SELECT)
    fn detect_subquery(&self, lower: &str) -> bool {
        if let Some(paren_pos) = lower.find('(') {
            let after = &lower[paren_pos..];
            after.contains("select")
        } else {
            false
        }
    }

    /// 统计 JOIN 数量
    fn count_joins(lower: &str) -> usize {
        lower.matches(" join ").count()
    }

    /// 统计子查询数量
    fn count_subqueries(lower: &str) -> usize {
        lower.matches("(select").count() + lower.matches("( select").count()
    }

    /// 检测前缀通配符 LIKE
    fn has_leading_wildcard_like(lower: &str) -> bool {
        lower.contains("like '%") || lower.contains("like \"%")
    }

    /// 统计 OR 条件数量
    fn count_or_conditions(lower: &str) -> usize {
        lower.matches(" or ").count()
    }

    /// 检测 ORDER BY 无 LIMIT
    fn has_order_by_without_limit(lower: &str) -> bool {
        lower.contains(" order by ") && !lower.contains(" limit ")
    }

    /// 从 SQL 中提取表名(基于 schema)
    fn extract_tables(&self, lower: &str, schema: &SchemaContext) -> Vec<String> {
        let mut tables = Vec::new();
        for table in &schema.tables {
            let name_lower = table.name.to_lowercase();
            if lower.contains(&name_lower) {
                tables.push(table.name.clone());
            }
        }
        tables
    }

    /// 生成列建议 SQL
    fn suggest_columns(&self, tables: &[String], schema: &SchemaContext) -> String {
        if tables.is_empty() {
            return String::new();
        }
        let mut cols = Vec::new();
        for table_name in tables {
            if let Some(table) = schema.tables.iter().find(|t| t.name == *table_name) {
                for col in &table.columns {
                    cols.push(format!("{}.{}", table_name, col.name));
                }
            }
        }
        if cols.is_empty() {
            String::new()
        } else {
            format!("SELECT {} FROM {}", cols.join(", "), tables.join(", "))
        }
    }

    /// 为 SQL 添加 LIMIT 建议
    fn add_limit_suggestion(&self, sql: &str, limit: usize) -> String {
        let trimmed = sql.trim_end_matches(';');
        format!("{} LIMIT {}", trimmed, limit)
    }

    /// 从 WHERE 子句中提取列名
    fn extract_where_columns(&self, lower: &str, schema: &SchemaContext) -> Vec<String> {
        let mut columns = Vec::new();
        if let Some(where_pos) = lower.find(" where ") {
            let after_where = &lower[where_pos + 7..];
            // 截取到 GROUP BY / ORDER BY / LIMIT 之前
            let where_clause = after_where
                .split(" group by ")
                .next()
                .unwrap_or(after_where)
                .split(" order by ")
                .next()
                .unwrap_or(after_where)
                .split(" limit ")
                .next()
                .unwrap_or(after_where);

            for table in &schema.tables {
                for col in &table.columns {
                    let col_lower = col.name.to_lowercase();
                    if col_lower.len() >= 2
                        && where_clause.contains(&col_lower)
                        && !columns.contains(&col.name)
                    {
                        columns.push(col.name.clone());
                    }
                }
            }
        }
        columns
    }

    /// 检查列是否有索引(简化版:主键视为有索引)
    fn column_has_index(&self, col: &str, schema: &SchemaContext) -> bool {
        for table in &schema.tables {
            for c in &table.columns {
                if c.name.eq_ignore_ascii_case(col) && c.is_primary_key {
                    return true;
                }
            }
        }
        false
    }

    /// 计算 SQL 复杂度评分
    fn calculate_complexity(
        &self,
        has_join: bool,
        has_subquery: bool,
        has_where: bool,
        tables: &[String],
        lower: &str,
    ) -> u32 {
        let mut score: u32 = 10; // 基础分

        if has_join {
            let join_count = Self::count_joins(lower) as u32;
            score += join_count * self.join_weight;
        }

        if has_subquery {
            let sub_count = Self::count_subqueries(lower) as u32;
            score += sub_count * self.subquery_weight;
        }

        if has_where {
            let or_count = Self::count_or_conditions(lower) as u32;
            score += self.where_weight + or_count * 3;
        }

        // 表数量影响
        if tables.len() > 1 {
            score += (tables.len() as u32 - 1) * 10;
        }

        // ORDER BY 影响
        if lower.contains(" order by ") {
            score += 5;
        }

        // GROUP BY 影响
        if lower.contains(" group by ") {
            score += 10;
        }

        // DISTINCT 影响
        if lower.contains(" distinct ") {
            score += 5;
        }

        score.min(100)
    }
}

// ==================== 单元测试 ====================

#[cfg(test)]
mod tests {
    use super::*;

    fn test_schema() -> SchemaContext {
        SchemaContext {
            tables: vec![
                TableInfo {
                    name: "users".into(),
                    columns: vec![
                        ColumnInfo {
                            name: "id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: true,
                        },
                        ColumnInfo {
                            name: "name".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "email".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "age".into(),
                            data_type: "INTEGER".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "city".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "score".into(),
                            data_type: "REAL".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                    ],
                },
                TableInfo {
                    name: "orders".into(),
                    columns: vec![
                        ColumnInfo {
                            name: "id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: true,
                        },
                        ColumnInfo {
                            name: "user_id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "product".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "price".into(),
                            data_type: "REAL".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "quantity".into(),
                            data_type: "INTEGER".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                    ],
                },
                TableInfo {
                    name: "products".into(),
                    columns: vec![
                        ColumnInfo {
                            name: "id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: true,
                        },
                        ColumnInfo {
                            name: "name".into(),
                            data_type: "TEXT".into(),
                            nullable: false,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "price".into(),
                            data_type: "REAL".into(),
                            nullable: false,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "category".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                    ],
                },
            ],
        }
    }

    // ============ SimpleNl2SqlEngine ============

    #[tokio::test]
    async fn test_simple_select_all() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine.generate("show all users", &schema).await.unwrap();
        assert_eq!(result.sql, "SELECT * FROM users");
        assert!(result.confidence > 0.0);
    }

    #[tokio::test]
    async fn test_simple_select_columns() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        // "name" and "email" both appear in the query; schema matching picks them up
        let result = engine
            .generate("show name and email of users", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT name, email FROM users");
    }

    #[tokio::test]
    async fn test_simple_count() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine.generate("how many users", &schema).await.unwrap();
        assert_eq!(result.sql, "SELECT COUNT(*) FROM users");
    }

    #[tokio::test]
    async fn test_simple_where_equality() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("find users where name = John", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT * FROM users WHERE name = $1");
        assert!(result.explanation.contains("John"));
    }

    #[tokio::test]
    async fn test_simple_where_comparison() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("find users where age > 25", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT * FROM users WHERE age > $1");
    }

    #[tokio::test]
    async fn test_simple_order_by() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("list users ordered by name", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT * FROM users ORDER BY name ASC");
    }

    #[tokio::test]
    async fn test_simple_order_by_desc() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("list users sorted by name descending", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT * FROM users ORDER BY name DESC");
    }

    #[tokio::test]
    async fn test_simple_limit() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine.generate("first 10 users", &schema).await.unwrap();
        assert_eq!(result.sql, "SELECT * FROM users LIMIT 10");
    }

    #[tokio::test]
    async fn test_simple_order_by_with_limit() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("top 5 users by score", &schema)
            .await
            .unwrap();
        // "top" triggers LIMIT; "score" is a known column, and "by score" matched as sort
        // This generates: SELECT * FROM users ORDER BY score ASC LIMIT 5
        assert!(result.sql.contains("LIMIT 5"));
    }

    #[tokio::test]
    async fn test_simple_aggregation() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("total price from orders", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT SUM(price) FROM orders");
    }

    #[tokio::test]
    async fn test_simple_avg() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("average age of users", &schema)
            .await
            .unwrap();
        assert_eq!(result.sql, "SELECT AVG(age) FROM users");
    }

    #[tokio::test]
    async fn test_simple_empty_query() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine.generate("", &schema).await;
        assert!(result.is_err());
        match result {
            Err(Nl2SqlError::InvalidQuery(_)) => {}
            Err(e) => panic!("期望 InvalidQuery,实际: {:?}", e),
            Ok(_) => panic!("期望错误"),
        }
    }

    #[tokio::test]
    async fn test_simple_empty_schema() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = SchemaContext { tables: vec![] };
        let result = engine.generate("show users", &schema).await;
        assert!(result.is_err());
        match result {
            Err(Nl2SqlError::SchemaError(_)) => {}
            _ => panic!("期望 SchemaError"),
        }
    }

    #[tokio::test]
    async fn test_simple_validate_valid() {
        let engine = SimpleNl2SqlEngine::new();
        let query = SqlQuery {
            sql: "SELECT * FROM users".into(),
            explanation: "test".into(),
            confidence: 0.9,
        };
        assert!(engine.validate(&query).await.unwrap());
    }

    #[tokio::test]
    async fn test_simple_validate_invalid_confidence() {
        let engine = SimpleNl2SqlEngine::new();
        let query = SqlQuery {
            sql: "SELECT * FROM users".into(),
            explanation: "test".into(),
            confidence: 1.5,
        };
        let result = engine.validate(&query).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_simple_validate_rejects_drop() {
        let engine = SimpleNl2SqlEngine::new();
        let query = SqlQuery {
            sql: "DROP TABLE users".into(),
            explanation: "test".into(),
            confidence: 0.9,
        };
        assert!(!engine.validate(&query).await.unwrap());
    }

    #[tokio::test]
    async fn test_simple_alias() {
        let engine = SimpleNl2SqlEngine::new().with_alias("person", "users");
        let schema = test_schema();
        let result = engine.generate("show all persons", &schema).await.unwrap();
        assert_eq!(result.sql, "SELECT * FROM users");
    }

    #[tokio::test]
    async fn test_simple_table_not_found() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        let result = engine
            .generate("show something from nonexistent_table", &schema)
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_simple_sql_in_schema_passthrough() {
        let engine = SimpleNl2SqlEngine::new();
        let schema = test_schema();
        // "name" and "email" are both schema columns
        let result = engine
            .generate("select name and email from users", &schema)
            .await
            .unwrap();
        assert!(result.sql.contains("name"));
        assert!(result.sql.contains("email"));
        assert!(result.sql.contains("users"));
    }

    // ============ 仅在 real feature 下测试 OpenAINl2SqlEngine ============

    #[cfg(feature = "real")]
    #[test]
    fn test_openai_engine_new_with_defaults() {
        let engine = OpenAINl2SqlEngine::new("sk-test-key");
        assert_eq!(engine.api_base, "https://api.openai.com/v1");
        assert_eq!(engine.api_key, "sk-test-key");
        assert_eq!(engine.model, "gpt-4o-mini");
    }

    #[cfg(feature = "real")]
    #[test]
    fn test_openai_engine_with_options() {
        let engine = OpenAINl2SqlEngine::new("sk-test")
            .with_api_base("https://api.deepseek.com/v1")
            .with_model("deepseek-chat");
        assert_eq!(engine.api_base, "https://api.deepseek.com/v1");
        assert_eq!(engine.model, "deepseek-chat");
    }

    #[cfg(feature = "real")]
    #[tokio::test]
    async fn test_openai_engine_missing_api_key() {
        let engine = OpenAINl2SqlEngine::new("");
        let schema = test_schema();
        let result = engine.generate("show users", &schema).await;
        match result {
            Err(Nl2SqlError::ConfigError(_)) => {}
            other => panic!("期望 ConfigError,实际: {:?}", other),
        }
    }

    #[cfg(feature = "real")]
    #[test]
    fn test_clean_llm_sql_output() {
        assert_eq!(
            clean_llm_sql_output("SELECT * FROM users"),
            "SELECT * FROM users"
        );
        assert_eq!(
            clean_llm_sql_output("```sql\nSELECT * FROM users\n```"),
            "SELECT * FROM users"
        );
        assert_eq!(
            clean_llm_sql_output("```\nSELECT * FROM users\n```"),
            "SELECT * FROM users"
        );
        assert_eq!(
            clean_llm_sql_output("\n  SELECT * FROM users  \n"),
            "SELECT * FROM users"
        );
    }

    // ============ 查询优化提示测试 ============

    fn optimizer_test_schema() -> SchemaContext {
        SchemaContext {
            tables: vec![
                TableInfo {
                    name: "users".into(),
                    columns: vec![
                        ColumnInfo {
                            name: "id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: true,
                        },
                        ColumnInfo {
                            name: "name".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "email".into(),
                            data_type: "TEXT".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "age".into(),
                            data_type: "INTEGER".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                    ],
                },
                TableInfo {
                    name: "orders".into(),
                    columns: vec![
                        ColumnInfo {
                            name: "id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: true,
                        },
                        ColumnInfo {
                            name: "user_id".into(),
                            data_type: "INTEGER".into(),
                            nullable: false,
                            is_primary_key: false,
                        },
                        ColumnInfo {
                            name: "amount".into(),
                            data_type: "DECIMAL".into(),
                            nullable: true,
                            is_primary_key: false,
                        },
                    ],
                },
            ],
        }
    }

    #[test]
    fn test_hint_severity_as_str() {
        assert_eq!(HintSeverity::Info.as_str(), "INFO");
        assert_eq!(HintSeverity::Warning.as_str(), "WARNING");
        assert_eq!(HintSeverity::Critical.as_str(), "CRITICAL");
    }

    #[test]
    fn test_query_optimization_hint_info() {
        let hint = QueryOptimizationHint::info("标题", "描述");
        assert_eq!(hint.title, "标题");
        assert_eq!(hint.description, "描述");
        assert_eq!(hint.severity, HintSeverity::Info);
        assert!(hint.suggested_sql.is_none());
    }

    #[test]
    fn test_query_optimization_hint_warning() {
        let hint = QueryOptimizationHint::warning("警告", "警告描述");
        assert_eq!(hint.severity, HintSeverity::Warning);
    }

    #[test]
    fn test_query_optimization_hint_critical() {
        let hint = QueryOptimizationHint::critical("严重", "严重描述");
        assert_eq!(hint.severity, HintSeverity::Critical);
    }

    #[test]
    fn test_query_optimization_hint_with_suggested_sql() {
        let hint =
            QueryOptimizationHint::info("建议", "描述").with_suggested_sql("SELECT id FROM users");
        assert_eq!(hint.suggested_sql.as_deref(), Some("SELECT id FROM users"));
    }

    #[test]
    fn test_query_optimizer_default() {
        let opt = QueryOptimizer::default();
        assert!(opt.check_select_star);
        assert!(opt.check_missing_limit);
        assert!(opt.check_missing_where);
        assert_eq!(opt.default_limit, 100);
    }

    #[test]
    fn test_query_optimizer_new() {
        let opt = QueryOptimizer::new();
        assert!(opt.check_select_star);
    }

    #[test]
    fn test_query_optimizer_with_default_limit() {
        let opt = QueryOptimizer::new().with_default_limit(50);
        assert_eq!(opt.default_limit, 50);
    }

    #[test]
    fn test_query_optimizer_disable_checks() {
        let opt = QueryOptimizer::new()
            .disable_select_star_check()
            .disable_missing_limit_check()
            .disable_missing_where_check();
        assert!(!opt.check_select_star);
        assert!(!opt.check_missing_limit);
        assert!(!opt.check_missing_where);
    }

    #[test]
    fn test_analyze_select_star() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT * FROM users", &schema);

        assert!(analysis.uses_select_star);
        assert!(!analysis.has_where);
        assert!(!analysis.has_limit);
        // 应该有 SELECT * 建议、缺失 WHERE 建议、缺失 LIMIT 建议
        assert!(analysis.has_hints());
        assert!(analysis.critical_count() >= 1); // 缺失 WHERE 是 critical
    }

    #[test]
    fn test_analyze_select_star_with_suggested_columns() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT * FROM users", &schema);

        // 应包含 SELECT * 警告,且附带建议 SQL
        let select_star_hint = analysis
            .hints
            .iter()
            .find(|h| h.title == "避免使用 SELECT *");
        assert!(select_star_hint.is_some());
        let hint = select_star_hint.unwrap();
        assert!(hint.suggested_sql.is_some());
        let suggested = hint.suggested_sql.as_ref().unwrap();
        assert!(suggested.contains("users.id"));
        assert!(suggested.contains("users.name"));
    }

    #[test]
    fn test_analyze_missing_where_critical() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users LIMIT 10", &schema);

        // 没有 WHERE 应产生 critical 建议
        assert!(analysis.critical_count() >= 1);
        let where_hint = analysis.hints.iter().find(|h| h.title == "缺少 WHERE 子句");
        assert!(where_hint.is_some());
        assert_eq!(where_hint.unwrap().severity, HintSeverity::Critical);
    }

    #[test]
    fn test_analyze_missing_limit_warning() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE age > 18", &schema);

        // 没有 LIMIT 应产生 warning 建议
        assert!(analysis.warning_count() >= 1);
        let limit_hint = analysis.hints.iter().find(|h| h.title == "缺少 LIMIT 子句");
        assert!(limit_hint.is_some());
        let hint = limit_hint.unwrap();
        assert!(hint.suggested_sql.is_some());
        assert!(hint.suggested_sql.as_ref().unwrap().contains("LIMIT 100"));
    }

    #[test]
    fn test_analyze_with_limit_no_limit_hint() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE age > 18 LIMIT 10", &schema);

        let limit_hint = analysis.hints.iter().find(|h| h.title == "缺少 LIMIT 子句");
        assert!(limit_hint.is_none());
    }

    #[test]
    fn test_analyze_join_detection() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze(
            "SELECT u.id FROM users u JOIN orders o ON u.id = o.user_id WHERE u.age > 18 LIMIT 10",
            &schema,
        );

        assert!(analysis.has_join);
        assert!(analysis.detected_tables.contains(&"users".to_string()));
        assert!(analysis.detected_tables.contains(&"orders".to_string()));
    }

    #[test]
    fn test_analyze_multiple_joins_critical() {
        let opt = QueryOptimizer::new();
        let schema = SchemaContext {
            tables: vec![
                TableInfo {
                    name: "t1".into(),
                    columns: vec![ColumnInfo {
                        name: "id".into(),
                        data_type: "INT".into(),
                        nullable: false,
                        is_primary_key: true,
                    }],
                },
                TableInfo {
                    name: "t2".into(),
                    columns: vec![ColumnInfo {
                        name: "id".into(),
                        data_type: "INT".into(),
                        nullable: false,
                        is_primary_key: true,
                    }],
                },
                TableInfo {
                    name: "t3".into(),
                    columns: vec![ColumnInfo {
                        name: "id".into(),
                        data_type: "INT".into(),
                        nullable: false,
                        is_primary_key: true,
                    }],
                },
                TableInfo {
                    name: "t4".into(),
                    columns: vec![ColumnInfo {
                        name: "id".into(),
                        data_type: "INT".into(),
                        nullable: false,
                        is_primary_key: true,
                    }],
                },
            ],
        };
        let analysis = opt.analyze(
            "SELECT * FROM t1 JOIN t2 ON t1.id = t2.id JOIN t3 ON t2.id = t3.id JOIN t4 ON t3.id = t4.id LIMIT 10",
            &schema,
        );
        // 4 个 JOIN 应触发 critical
        let join_hint = analysis.hints.iter().find(|h| h.title == "JOIN 数量过多");
        assert!(join_hint.is_some());
        assert_eq!(join_hint.unwrap().severity, HintSeverity::Critical);
    }

    #[test]
    fn test_analyze_subquery_detection() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze(
            "SELECT id FROM users WHERE id IN (SELECT user_id FROM orders) LIMIT 10",
            &schema,
        );

        assert!(analysis.has_subquery);
    }

    #[test]
    fn test_analyze_leading_wildcard_like() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze(
            "SELECT id FROM users WHERE name LIKE '%john' LIMIT 10",
            &schema,
        );

        let like_hint = analysis
            .hints
            .iter()
            .find(|h| h.title == "LIKE 使用前缀通配符");
        assert!(like_hint.is_some());
    }

    #[test]
    fn test_analyze_order_by_without_limit() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE age > 18 ORDER BY id", &schema);

        let order_hint = analysis
            .hints
            .iter()
            .find(|h| h.title == "ORDER BY 无 LIMIT");
        assert!(order_hint.is_some());
    }

    #[test]
    fn test_analyze_count_star_hint() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT COUNT(*) FROM users LIMIT 1", &schema);

        let count_hint = analysis
            .hints
            .iter()
            .find(|h| h.title == "考虑使用 COUNT(1)");
        assert!(count_hint.is_some());
    }

    #[test]
    fn test_analyze_missing_index_hint() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        // age 列不是主键,应建议添加索引
        let analysis = opt.analyze("SELECT id FROM users WHERE age > 18 LIMIT 10", &schema);

        let index_hint = analysis.hints.iter().find(|h| h.title.contains("添加索引"));
        assert!(index_hint.is_some());
        assert!(index_hint.unwrap().title.contains("age"));
    }

    #[test]
    fn test_analyze_primary_key_no_index_hint() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        // id 列是主键,不应建议添加索引
        let analysis = opt.analyze("SELECT name FROM users WHERE id = 1 LIMIT 10", &schema);

        let index_hint = analysis
            .hints
            .iter()
            .find(|h| h.title.contains("添加索引") && h.title.contains("id"));
        // id 是主键,不应有索引建议
        assert!(index_hint.is_none());
    }

    #[test]
    fn test_analyze_complexity_score_simple() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE id = 1 LIMIT 10", &schema);
        // 简单查询应该低分
        assert!(analysis.complexity_score < 30);
    }

    #[test]
    fn test_analyze_complexity_score_complex() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze(
            "SELECT u.id, o.amount FROM users u JOIN orders o ON u.id = o.user_id WHERE u.age > 18 OR u.name LIKE '%a%' GROUP BY u.id ORDER BY o.amount DESC",
            &schema,
        );
        // 复杂查询应该高分
        assert!(analysis.complexity_score > 30);
    }

    #[test]
    fn test_analyze_well_optimized_query() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id, name FROM users WHERE id = 1 LIMIT 10", &schema);

        // 这个查询写得很好,不应该有 critical 或 warning 建议
        assert_eq!(analysis.critical_count(), 0);
        // id 是主键,不应有索引建议
        // 有 LIMIT,不应有 LIMIT 建议
        // 有 WHERE,不应有 WHERE 建议
        // 没有 SELECT *,不应有 SELECT * 建议
    }

    #[test]
    fn test_query_analysis_has_hints() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT * FROM users", &schema);
        assert!(analysis.has_hints());

        let good_analysis = opt.analyze("SELECT id FROM users WHERE id = 1 LIMIT 1", &schema);
        // 可能仍有 info 级建议,但不应有 critical
        assert_eq!(good_analysis.critical_count(), 0);
    }

    #[test]
    fn test_format_report_contains_key_info() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT * FROM users", &schema);
        let report = QueryOptimizer::format_report(&analysis);

        assert!(report.contains("SQL 查询优化分析报告"));
        assert!(report.contains("原始 SQL"));
        assert!(report.contains("复杂度评分"));
        assert!(report.contains("SELECT *"));
    }

    #[test]
    fn test_format_report_no_hints() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE id = 1 LIMIT 1", &schema);
        let report = QueryOptimizer::format_report(&analysis);
        // 即使没有建议,报告也应包含基本字段
        assert!(report.contains("复杂度评分"));
    }

    #[test]
    fn test_analyze_disable_select_star() {
        let opt = QueryOptimizer::new().disable_select_star_check();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT * FROM users WHERE id = 1 LIMIT 10", &schema);

        let star_hint = analysis
            .hints
            .iter()
            .find(|h| h.title == "避免使用 SELECT *");
        assert!(star_hint.is_none());
    }

    #[test]
    fn test_analyze_disable_missing_where() {
        let opt = QueryOptimizer::new().disable_missing_where_check();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users LIMIT 10", &schema);

        let where_hint = analysis.hints.iter().find(|h| h.title == "缺少 WHERE 子句");
        assert!(where_hint.is_none());
    }

    #[test]
    fn test_analyze_disable_missing_limit() {
        let opt = QueryOptimizer::new().disable_missing_limit_check();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users WHERE id = 1", &schema);

        let limit_hint = analysis.hints.iter().find(|h| h.title == "缺少 LIMIT 子句");
        assert!(limit_hint.is_none());
    }

    #[test]
    fn test_analyze_multiple_or_conditions() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze(
            "SELECT id FROM users WHERE age = 1 OR age = 2 OR age = 3 OR age = 4 LIMIT 10",
            &schema,
        );

        let or_hint = analysis.hints.iter().find(|h| h.title == "多个 OR 条件");
        assert!(or_hint.is_some());
    }

    #[test]
    fn test_analyze_detected_tables() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT id FROM users LIMIT 10", &schema);

        assert_eq!(analysis.detected_tables, vec!["users".to_string()]);
    }

    #[test]
    fn test_analyze_no_detected_tables() {
        let opt = QueryOptimizer::new();
        let schema = optimizer_test_schema();
        let analysis = opt.analyze("SELECT 1 LIMIT 10", &schema);

        assert!(analysis.detected_tables.is_empty());
    }

    #[test]
    fn test_normalize_sql() {
        let normalized = QueryOptimizer::normalize_sql("SELECT  id\nFROM   users");
        assert_eq!(normalized, "SELECT id FROM users");
    }
}