echo_agent 0.1.2

Production-grade AI Agent framework for Rust — ReAct engine, multi-agent, memory, streaming, MCP, IM channels, workflows
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
//! Data processing tools
//!
//! Data processing capabilities based on Polars, supporting:
//! - CSV/JSON/Parquet file reading
//! - Data filtering, aggregation, sorting
//! - Statistical computation
//! - Data transformation
//! - Data profiling (dimension/metric identification)
//! - TopN / contribution analysis / numeric binning

use std::path::Path;

use futures::future::BoxFuture;
use polars::prelude::*;
use serde_json::Value;

use super::security::SecurityConfig;
use crate::error::{Result, ToolError};
use crate::tools::{Tool, ToolParameters, ToolResult};

const TOOL_NAME: &str = "data_tools";

// ── Shared data loading helpers ──────────────────────────────────────

/// Detect format based on file extension
fn detect_format<'a>(path: &'a Path, hint: Option<&'a str>) -> &'a str {
    hint.unwrap_or_else(|| match path.extension().and_then(|e| e.to_str()) {
        Some("csv") | Some("txt") | Some("tsv") => "csv",
        Some("json") | Some("jsonl") => "json",
        Some("parquet") | Some("pq") => "parquet",
        _ => "csv",
    })
}

/// Load DataFrame (eager), supports CSV/JSON/Parquet
fn load_dataframe(path: &Path, format: Option<&str>) -> Result<DataFrame> {
    let fmt = detect_format(path, format);

    let file = std::fs::File::open(path).map_err(|e| ToolError::ExecutionFailed {
        tool: TOOL_NAME.to_string(),
        message: format!("Failed to open file: {}", e),
    })?;

    match fmt {
        "csv" => Ok(CsvReader::new(file)
            .finish()
            .map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Failed to read CSV: {}", e),
            })?),
        "json" => {
            let file2 = std::fs::File::open(path).map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Failed to open JSON file: {}", e),
            })?;
            Ok(JsonReader::new(file2)
                .finish()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Failed to read JSON: {}", e),
                })?)
        }
        "parquet" => {
            let file2 = std::fs::File::open(path).map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Failed to open Parquet file: {}", e),
            })?;
            Ok(ParquetReader::new(file2)
                .finish()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Failed to read Parquet: {}", e),
                })?)
        }
        _ => Err(ToolError::InvalidParameter {
            name: "format".to_string(),
            message: format!("Unsupported file format: '{}'", fmt),
        }
        .into()),
    }
}

/// Load LazyFrame, supports CSV/JSON/Parquet
fn load_lazyframe(path: &Path, format: Option<&str>) -> Result<LazyFrame> {
    let fmt = detect_format(path, format);
    let path_str = path.to_string_lossy().to_string();

    match fmt {
        "csv" => Ok(LazyCsvReader::new(PlRefPath::from(path_str.as_str()))
            .finish()
            .map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Failed to read CSV: {}", e),
            })?),
        "json" => {
            // For JSON, we use the eager reader and convert to LazyFrame
            let df = load_dataframe(path, Some("json"))?;
            Ok(df.lazy())
        }
        "parquet" => {
            let file = std::fs::File::open(path).map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Failed to open Parquet file: {}", e),
            })?;
            let df = ParquetReader::new(file)
                .finish()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Failed to read Parquet: {}", e),
                })?;
            Ok(df.lazy())
        }
        _ => Err(ToolError::InvalidParameter {
            name: "format".to_string(),
            message: format!("Unsupported file format: '{}'", fmt),
        }
        .into()),
    }
}

/// Check if a column type is numeric
fn is_numeric(dtype: &DataType) -> bool {
    matches!(
        dtype,
        DataType::Int8
            | DataType::Int16
            | DataType::Int32
            | DataType::Int64
            | DataType::UInt8
            | DataType::UInt16
            | DataType::UInt32
            | DataType::UInt64
            | DataType::Float32
            | DataType::Float64
    )
}

/// Check if a column type is temporal
fn is_temporal(dtype: &DataType) -> bool {
    matches!(
        dtype,
        DataType::Date | DataType::Datetime(_, _) | DataType::Time | DataType::Duration(_)
    )
}

/// Column category type
#[derive(Debug, PartialEq)]
enum ColumnCategory {
    Dimension,
    Metric,
    Temporal,
    Unknown,
}

fn classify_column(dtype: &DataType, distinct_count: usize, row_count: usize) -> ColumnCategory {
    if is_temporal(dtype) {
        return ColumnCategory::Temporal;
    }

    let distinct_ratio = if row_count > 0 {
        distinct_count as f64 / row_count as f64
    } else {
        0.0
    };

    // Low cardinality (< 10% or < 50 distinct values) → dimension
    // String type → dimension
    if is_numeric(dtype) {
        if distinct_ratio < 0.1 || distinct_count < 50 {
            ColumnCategory::Dimension
        } else {
            ColumnCategory::Metric
        }
    } else {
        if matches!(
            dtype,
            DataType::String | DataType::Categorical(_, _) | DataType::Enum(_, _)
        ) || distinct_ratio < 0.3
        {
            ColumnCategory::Dimension
        } else {
            ColumnCategory::Unknown
        }
    }
}

// ── Data reader tool ─────────────────────────────────────────────────

pub struct DataReadTool;

impl Tool for DataReadTool {
    fn name(&self) -> &str {
        "read_data"
    }

    fn description(&self) -> &str {
        "Read data files (CSV, JSON, Parquet), returning basic info and a preview of the first rows."
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "format": {
                    "type": "string",
                    "description": "File format: 'csv', 'json', or 'parquet' (optional, auto-detected)"
                },
                "preview_rows": {
                    "type": "integer",
                    "description": "Number of preview rows (default 10)"
                }
            },
            "required": ["file_path"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let format = parameters.get("format").and_then(|v| v.as_str());

            let preview_rows = parameters
                .get("preview_rows")
                .and_then(|v| v.as_u64())
                .unwrap_or(10) as usize;

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;

            let detected_format = detect_format(&path, format);
            let df = load_dataframe(&path, format)?;

            let effective_preview_rows = preview_rows.min(security.limits.max_preview_rows);

            // Basic info
            let shape = df.shape();
            let columns: Vec<String> = df
                .get_column_names()
                .iter()
                .map(|s| s.to_string())
                .collect();

            let preview = df.head(Some(effective_preview_rows));
            let preview_json = df_to_json(&preview)?;

            let result = serde_json::json!({
                "file": file_path,
                "format": detected_format,
                "rows": shape.0,
                "columns": shape.1,
                "column_info": columns.iter().map(|col| {
                    if let Ok(c) = df.column(col.as_str()) {
                        serde_json::json!({"name": col, "dtype": c.dtype().to_string()})
                    } else {
                        serde_json::json!({"name": col, "dtype": "unknown"})
                    }
                }).collect::<Vec<_>>(),
                "preview_rows": effective_preview_rows,
                "preview": preview_json,
            });

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Data filter tool ─────────────────────────────────────────────────

pub struct DataFilterTool;

impl Tool for DataFilterTool {
    fn name(&self) -> &str {
        "filter_data"
    }

    fn description(&self) -> &str {
        "Filter a data file, supporting conditional expressions (comparisons, AND/OR combinations, contains matching, etc.). Returns a preview of the filtered data."
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "filter": {
                    "type": "string",
                    "description": "Filter condition. Supports: 'col > 100', 'col == \"value\"', 'col contains \"text\"', 'A > 10 AND B < 5', 'col starts_with \"prefix\"'"
                },
                "limit": {
                    "type": "integer",
                    "description": "Result row count limit (optional)"
                }
            },
            "required": ["file_path", "filter"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let filter_expr = parameters
                .get("filter")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("filter".to_string()))?;

            let limit = parameters
                .get("limit")
                .and_then(|v| v.as_u64())
                .map(|n| n as usize);

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let lf = load_lazyframe(&path, format)?;

            let expr = parse_filter_expression(filter_expr)?;
            let filtered_lf = lf.filter(expr);
            let df = filtered_lf
                .collect()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Filter execution failed: {}", e),
                })?;

            let max_rows = security.limits.max_preview_rows;
            let effective_limit = limit.map(|n| n.min(max_rows)).unwrap_or(max_rows);
            let result_df = df.head(Some(effective_limit));
            let data_json = df_to_json(&result_df)?;

            let result = serde_json::json!({
                "filter": filter_expr,
                "matched_rows": df.shape().0,
                "data": data_json,
            });

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Data aggregation tool ────────────────────────────────────────────

pub struct DataAggregateTool;

impl Tool for DataAggregateTool {
    fn name(&self) -> &str {
        "aggregate_data"
    }

    fn description(&self) -> &str {
        "Group aggregation operations on data: group stats, sum, mean, count, distinct count, variance, stddev, median, p25/p75/p90/p95/arbitrary percentile, etc. Supported operations: sum, mean/avg, min, max, count, count_distinct/n_unique, variance/var, stddev/std, median, p25/p75/p90/p95, percentile:N/pct:N, first, last. Example: aggregate_data(file_path='sales.csv', group_by='region', aggregations='sales:sum,profit:mean,users:count_distinct,revenue:p95')"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "group_by": {
                    "type": "string",
                    "description": "Group-by column name (optional, comma-separated for multiple)"
                },
                "aggregations": {
                    "type": "string",
                    "description": "Aggregation operations, format: 'column:op', comma-separated for multiple. Ops: sum, mean/avg, min, max, count, count_distinct, variance, stddev, median, p90, p95, percentile:N, etc."
                }
            },
            "required": ["file_path", "aggregations"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let group_by = parameters.get("group_by").and_then(|v| v.as_str());

            let aggregations_str = parameters
                .get("aggregations")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("aggregations".to_string()))?;

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let lf = load_lazyframe(&path, format)?;
            let agg_exprs = parse_aggregations(aggregations_str)?;

            let result_lf = if let Some(gb) = group_by {
                let group_cols: Vec<Expr> = gb.split(',').map(|s| col(s.trim())).collect();
                lf.group_by(group_cols).agg(agg_exprs)
            } else {
                lf.select(agg_exprs)
            };

            let df = result_lf
                .collect()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Aggregation execution failed: {}", e),
                })?;

            let data_json = df_to_json(&df)?;

            let result = serde_json::json!({
                "group_by": group_by,
                "data": data_json,
            });

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Data stats tool ──────────────────────────────────────────────────

pub struct DataStatsTool;

impl Tool for DataStatsTool {
    fn name(&self) -> &str {
        "data_stats"
    }

    fn description(&self) -> &str {
        "Compute detailed per-column statistics (no grouping): count, nulls and null rate, distinct count and distinct rate, mean, stddev, variance, min, max, median, p25/p75/p90/p95 percentiles; for string columns also shows shortest/longest/average length and most frequent value. Difference from aggregate_data: data_stats is per-column overall stats (no grouping), aggregate_data is grouped aggregation. Example: data_stats(file_path='data.csv', columns='age,income,region')"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "columns": {
                    "type": "string",
                    "description": "Column names to compute statistics for, comma-separated (optional, defaults to all numeric columns)"
                }
            },
            "required": ["file_path"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let column_filter: Option<Vec<&str>> = parameters
                .get("columns")
                .and_then(|v| v.as_str())
                .map(|s| s.split(',').map(|c| c.trim()).collect());

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;
            let shape = df.shape();
            let all_columns: Vec<String> = df
                .get_column_names()
                .iter()
                .map(|s| s.to_string())
                .collect();

            let mut columns_json = Vec::new();

            // Filter columns to compute stats for
            let target_cols: Vec<String> = if let Some(ref filter) = column_filter {
                filter.iter().map(|s| s.to_string()).collect()
            } else {
                all_columns.clone()
            };

            for col_name in &target_cols {
                let c = match df.column(col_name.as_str()) {
                    Ok(c) => c,
                    Err(_) => {
                        columns_json.push(serde_json::json!({
                            "name": col_name,
                            "error": "Column not found",
                        }));
                        continue;
                    }
                };

                let dtype = c.dtype();
                let null_count = c.null_count();
                let total = c.len();
                let non_null_count = total - null_count;
                let null_pct = if total > 0 {
                    (null_count as f64 / total as f64) * 100.0
                } else {
                    0.0
                };

                let mut col_json = serde_json::json!({
                    "name": col_name,
                    "dtype": dtype.to_string(),
                    "total": total,
                    "non_null": non_null_count,
                    "null_count": null_count,
                    "null_pct": (null_pct * 100.0).round() / 100.0,
                });

                // Distinct count
                if let Ok(unique_count) = c.n_unique() {
                    let unique_pct = if total > 0 {
                        (unique_count as f64 / total as f64) * 100.0
                    } else {
                        0.0
                    };
                    col_json["unique_count"] = serde_json::json!(unique_count);
                    col_json["unique_pct"] =
                        serde_json::json!((unique_pct * 100.0).round() / 100.0);
                }

                // Numeric statistics
                if is_numeric(dtype) && non_null_count > 0 {
                    let series = c.as_materialized_series();
                    let chunked = match dtype {
                        DataType::Int64 => {
                            let ca: &polars::prelude::Int64Chunked =
                                series.i64().map_err(|e| ToolError::ExecutionFailed {
                                    tool: TOOL_NAME.to_string(),
                                    message: format!("Expected Int64 series: {e}"),
                                })?;
                            let v: Vec<Option<f64>> =
                                ca.iter().map(|opt| opt.map(|x| x as f64)).collect();
                            polars::prelude::Float64Chunked::from_slice_options(
                                PlSmallStr::from_static("tmp"),
                                &v,
                            )
                        }
                        DataType::Float64 => series
                            .f64()
                            .map_err(|e| ToolError::ExecutionFailed {
                                tool: TOOL_NAME.to_string(),
                                message: format!("Expected Float64 series: {e}"),
                            })?
                            .clone(),
                        _ => series
                            .cast(&DataType::Float64)
                            .unwrap_or_default()
                            .f64()
                            .unwrap_or(&polars::prelude::Float64Chunked::full(
                                PlSmallStr::from_static("tmp"),
                                0.0,
                                0,
                            ))
                            .clone(),
                    };

                    let values: Vec<f64> = chunked.iter().flatten().collect();

                    if !values.is_empty() {
                        let mut sorted = values.clone();
                        sorted
                            .sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

                        let n = sorted.len();
                        let min_val = sorted[0];
                        let max_val = sorted[n - 1];
                        let sum: f64 = sorted.iter().sum();
                        let mean = sum / n as f64;

                        let variance: f64 =
                            sorted.iter().map(|x| (x - mean) * (x - mean)).sum::<f64>()
                                / (n - 1) as f64;
                        let stddev = variance.sqrt();

                        let median = if n.is_multiple_of(2) {
                            (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
                        } else {
                            sorted[n / 2]
                        };

                        let p25_idx = (n as f64 * 0.25).round() as usize;
                        let p75_idx = (n as f64 * 0.75).round() as usize;
                        let p90_idx = (n as f64 * 0.90).round() as usize;
                        let p95_idx = (n as f64 * 0.95).round() as usize;

                        let p25 = sorted[p25_idx.min(n - 1)];
                        let p75 = sorted[p75_idx.min(n - 1)];
                        let p90 = sorted[p90_idx.min(n - 1)];
                        let p95 = sorted[p95_idx.min(n - 1)];

                        col_json["numeric_stats"] = serde_json::json!({
                            "min": min_val,
                            "max": max_val,
                            "mean": mean,
                            "median": median,
                            "stddev": stddev,
                            "variance": variance,
                            "p25": p25,
                            "p75": p75,
                            "p90": p90,
                            "p95": p95,
                        });
                    }
                }

                // String column statistics
                if matches!(dtype, DataType::String) && non_null_count > 0 {
                    let series = c.as_materialized_series();
                    let ca = series.str().map_err(|e| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: format!("Expected String series: {e}"),
                    })?;
                    let lengths: Vec<usize> = ca.iter().flatten().map(|s| s.len()).collect();
                    if !lengths.is_empty() {
                        let avg_len = lengths.iter().sum::<usize>() as f64 / lengths.len() as f64;
                        let min_len = lengths.iter().min().unwrap_or(&0);
                        let max_len = lengths.iter().max().unwrap_or(&0);
                        col_json["string_stats"] = serde_json::json!({
                            "min_len": min_len,
                            "max_len": max_len,
                            "avg_len": (avg_len * 10.0).round() / 10.0,
                        });
                    }

                    // Top 3 frequent values
                    let freq: std::collections::HashMap<&str, usize> =
                        ca.iter()
                            .flatten()
                            .fold(std::collections::HashMap::new(), |mut acc, s| {
                                *acc.entry(s).or_insert(0) += 1;
                                acc
                            });
                    let mut freq_vec: Vec<(&&str, &usize)> = freq.iter().collect();
                    freq_vec.sort_by(|a, b| b.1.cmp(a.1));
                    let top_values: Vec<serde_json::Value> = freq_vec.iter().take(3).map(|(val, count)| {
                        serde_json::json!({
                            "value": val,
                            "count": count,
                            "pct": ((**count as f64 / non_null_count as f64) * 10000.0).round() / 100.0,
                        })
                    }).collect();
                    col_json["top_values"] = serde_json::json!(top_values);
                }

                columns_json.push(col_json);
            }

            let result = serde_json::json!({
                "file": file_path,
                "total_rows": shape.0,
                "total_cols": shape.1,
                "columns": columns_json,
            });

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Data transform tool ──────────────────────────────────────────────

pub struct DataTransformTool;

impl Tool for DataTransformTool {
    fn name(&self) -> &str {
        "transform_data"
    }

    fn description(&self) -> &str {
        "Transform data: sort, select columns, rename columns, drop columns, etc."
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "operation": {
                    "type": "string",
                    "description": "Operation type: 'sort', 'select' (select columns), 'drop' (remove columns), 'rename' (rename columns)"
                },
                "params": {
                    "type": "string",
                    "description": "Operation params. sort: 'col:asc/desc'; select: 'col1,col2'; drop: 'col1,col2'; rename: 'old:new' (one pair) or 'old1:new1,old2:new2' (multiple pairs)"
                },
                "limit": {
                    "type": "integer",
                    "description": "Result row count limit (optional)"
                }
            },
            "required": ["file_path", "operation", "params"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let operation = parameters
                .get("operation")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("operation".to_string()))?;

            let params = parameters
                .get("params")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("params".to_string()))?;

            let limit = parameters
                .get("limit")
                .and_then(|v| v.as_u64())
                .map(|n| n as usize);

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let lf = load_lazyframe(&path, format)?;

            let result_lf = match operation {
                "sort" => {
                    let parts: Vec<&str> = params.split(':').collect();
                    let col_name = parts[0].trim();
                    let descending = parts
                        .get(1)
                        .map(|s| s.trim().to_lowercase() == "desc")
                        .unwrap_or(false);

                    lf.sort(
                        [col_name],
                        SortMultipleOptions {
                            descending: vec![descending],
                            nulls_last: vec![true],
                            multithreaded: true,
                            maintain_order: false,
                            limit: None,
                        },
                    )
                }
                "select" => {
                    let cols: Vec<Expr> = params.split(',').map(|s| col(s.trim())).collect();
                    lf.select(cols)
                }
                "drop" => {
                    let drop_cols: Vec<&str> = params.split(',').map(|s| s.trim()).collect();
                    lf.drop(cols(drop_cols))
                }
                "rename" => {
                    let mut renamed = lf;
                    for pair in params.split(',') {
                        let parts: Vec<&str> = pair.trim().split(':').collect();
                        if parts.len() == 2 {
                            renamed = renamed.rename(
                                [parts[0].trim().to_string()],
                                [parts[1].trim().to_string()],
                                false,
                            );
                        }
                    }
                    renamed
                }
                _ => {
                    return Err(ToolError::InvalidParameter {
                        name: "operation".to_string(),
                        message: format!(
                            "Unsupported operation: '{}', please use sort/select/drop/rename",
                            operation
                        ),
                    }
                    .into());
                }
            };

            let df = result_lf
                .collect()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Transform execution failed: {}", e),
                })?;

            let max_rows = security.limits.max_preview_rows;
            let effective_limit = limit.map(|n| n.min(max_rows)).unwrap_or(max_rows);
            let result_df = df.head(Some(effective_limit));

            let data_json = df_to_json(&result_df)?;
            Ok(ToolResult::success_json(serde_json::json!({
                "operation": operation,
                "params": params,
                "data": data_json,
            })))
        })
    }
}

// ── Data export tool ─────────────────────────────────────────────────

pub struct DataExportTool;

impl Tool for DataExportTool {
    fn name(&self) -> &str {
        "export_data"
    }

    fn description(&self) -> &str {
        "Export processed data to CSV, JSON, or Parquet file."
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "input_file": {
                    "type": "string",
                    "description": "Input data file path"
                },
                "output_file": {
                    "type": "string",
                    "description": "Output file path"
                },
                "format": {
                    "type": "string",
                    "description": "Output format: 'csv', 'json', or 'parquet'"
                },
                "filter": {
                    "type": "string",
                    "description": "Optional filter condition"
                },
                "columns": {
                    "type": "string",
                    "description": "Optional column selection"
                }
            },
            "required": ["input_file", "output_file", "format"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let input_file = parameters
                .get("input_file")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("input_file".to_string()))?;

            let output_file = parameters
                .get("output_file")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("output_file".to_string()))?;

            let format = parameters
                .get("format")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("format".to_string()))?;

            let filter = parameters.get("filter").and_then(|v| v.as_str());
            let columns = parameters.get("columns").and_then(|v| v.as_str());

            let security = SecurityConfig::global();
            let path = security.validate_file(input_file)?;

            let mut lf = load_lazyframe(&path, None)?;

            if let Some(filter_expr) = filter {
                let expr = parse_filter_expression(filter_expr)?;
                lf = lf.filter(expr);
            }

            if let Some(cols) = columns {
                let col_exprs: Vec<Expr> = cols.split(',').map(|s| col(s.trim())).collect();
                lf = lf.select(col_exprs);
            }

            let mut df = lf.collect().map_err(|e| ToolError::ExecutionFailed {
                tool: TOOL_NAME.to_string(),
                message: format!("Data processing failed: {}", e),
            })?;

            let max_export_rows = security.limits.max_preview_rows;
            if df.shape().0 > max_export_rows {
                df = df.head(Some(max_export_rows));
            }

            let output_path = security.validate_output_file(output_file)?;
            if let Some(parent) = output_path.parent()
                && !parent.as_os_str().is_empty()
            {
                std::fs::create_dir_all(parent).map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Failed to create output directory: {}", e),
                })?;
            }

            match format {
                "csv" => {
                    let mut file = std::fs::File::create(&output_path).map_err(|e| {
                        ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: format!("Failed to create output file: {}", e),
                        }
                    })?;
                    CsvWriter::new(&mut file).finish(&mut df).map_err(|e| {
                        ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: format!("Failed to write CSV: {}", e),
                        }
                    })?;
                }
                "json" => {
                    let json_value = df_to_json(&df)?;
                    std::fs::write(&output_path, serde_json::to_string_pretty(&json_value)?)
                        .map_err(|e| ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: format!("Failed to write JSON: {}", e),
                        })?;
                }
                "parquet" => {
                    let file = std::fs::File::create(&output_path).map_err(|e| {
                        ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: format!("Failed to create output file: {}", e),
                        }
                    })?;
                    ParquetWriter::new(file).finish(&mut df).map_err(|e| {
                        ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: format!("Failed to write Parquet: {}", e),
                        }
                    })?;
                }
                _ => {
                    return Err(ToolError::InvalidParameter {
                        name: "format".to_string(),
                        message: format!("Unsupported export format: '{}'", format),
                    }
                    .into());
                }
            }

            Ok(ToolResult::success_json(serde_json::json!({
                "input_file": input_file,
                "output_file": output_file,
                "format": format,
                "exported_rows": df.shape().0,
                "truncated": df.shape().0 >= max_export_rows,
                "max_export_rows": max_export_rows,
            })))
        })
    }
}

// ── Data profiling tool (dimension/metric identification) ────────────

pub struct DataProfileTool;

impl Tool for DataProfileTool {
    fn name(&self) -> &str {
        "profile_data"
    }

    fn description(&self) -> &str {
        "[Quick data understanding - preferred tool] Automatically identifies each column as dimension or metric: computes missing rate, distinct rate, [min,max,mean,sum] for numeric columns, length range for string columns, and top 5 sample values. Output also includes column classification summary (dimension/metric/time column counts) and suggests follow-up analysis tools (topn_data, contribution_data, bin_data, etc.). Does not return detailed data, only a profile scan. Example: profile_data(file_path='sales.csv')"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                }
            },
            "required": ["file_path"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;
            let shape = df.shape();
            let row_count = shape.0;
            let col_count = shape.1;

            let columns: Vec<String> = df
                .get_column_names()
                .iter()
                .map(|s| s.to_string())
                .collect();

            let mut dim_count = 0;
            let mut metric_count = 0;
            let mut temporal_count = 0;

            let mut columns_json = Vec::new();

            for col_name in &columns {
                let c = match df.column(col_name.as_str()) {
                    Ok(c) => c,
                    Err(_) => continue,
                };

                let dtype = c.dtype();
                let null_count = c.null_count();
                let null_pct = if row_count > 0 {
                    ((null_count as f64 / row_count as f64) * 10000.0).round() / 100.0
                } else {
                    0.0
                };

                let distinct_count = c.n_unique().unwrap_or(0);
                let distinct_pct = if row_count > 0 {
                    ((distinct_count as f64 / row_count as f64) * 10000.0).round() / 100.0
                } else {
                    0.0
                };

                let category = classify_column(dtype, distinct_count, row_count);
                let cat_label = match category {
                    ColumnCategory::Dimension => "dimension",
                    ColumnCategory::Metric => "metric",
                    ColumnCategory::Temporal => "temporal",
                    ColumnCategory::Unknown => "other",
                };

                match category {
                    ColumnCategory::Dimension => dim_count += 1,
                    ColumnCategory::Metric => metric_count += 1,
                    ColumnCategory::Temporal => temporal_count += 1,
                    _ => {}
                }

                let mut col_json = serde_json::json!({
                    "name": col_name,
                    "dtype": dtype.to_string(),
                    "category": cat_label,
                    "null_count": null_count,
                    "null_pct": null_pct,
                    "distinct_count": distinct_count,
                    "distinct_pct": distinct_pct,
                });

                // Numeric columns: range/stats
                if is_numeric(dtype) && (row_count - null_count) > 0 {
                    let series = c.as_materialized_series();
                    if let Ok(f64_series) = series.cast(&DataType::Float64)
                        && let Ok(ca) = f64_series.f64()
                    {
                        let vals: Vec<f64> = ca.iter().flatten().collect();
                        if !vals.is_empty() {
                            let min_v = vals.iter().fold(f64::INFINITY, |a, &b| a.min(b));
                            let max_v = vals.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
                            let sum: f64 = vals.iter().sum();
                            let mean = sum / vals.len() as f64;
                            col_json["numeric_range"] = serde_json::json!({
                                "min": min_v,
                                "max": max_v,
                                "mean": mean,
                                "sum": sum,
                            });
                        }
                    }
                }

                // String columns: length info
                if matches!(dtype, DataType::String) && (row_count - null_count) > 0 {
                    let series = c.as_materialized_series();
                    if let Ok(ca) = series.str() {
                        let lengths: Vec<usize> = ca.iter().flatten().map(|s| s.len()).collect();
                        if !lengths.is_empty() {
                            let min_len = lengths.iter().min().unwrap_or(&0);
                            let max_len = lengths.iter().max().unwrap_or(&0);
                            let avg_len =
                                lengths.iter().sum::<usize>() as f64 / lengths.len() as f64;
                            col_json["string_length"] = serde_json::json!({
                                "min": min_len,
                                "max": max_len,
                                "avg": (avg_len * 10.0).round() / 10.0,
                            });
                        }
                    }
                }

                // Sample values (top 5)
                let sample_count = 5.min(row_count - null_count);
                if sample_count > 0 {
                    let mut sample_values: Vec<String> = Vec::new();
                    let mut seen = std::collections::HashSet::new();
                    for i in 0..row_count.min(1000) {
                        let val_str = c
                            .get(i)
                            .map(|v| format_value(&v))
                            .unwrap_or_else(|_| "-".to_string());
                        if val_str != "-" && seen.insert(val_str.clone()) {
                            sample_values.push(val_str);
                            if sample_values.len() >= 5 {
                                break;
                            }
                        }
                    }
                    if !sample_values.is_empty() {
                        col_json["sample_values"] = serde_json::json!(sample_values);
                    }
                }

                columns_json.push(col_json);
            }

            // Suggestions
            let mut suggestions: Vec<String> = Vec::new();
            if metric_count > 0 && dim_count > 0 {
                suggestions.push(format!(
                    "Use topn_data to analyze dimension rankings on metrics ({} dims x {} metrics)",
                    dim_count, metric_count
                ));
                suggestions.push(
                    "Use contribution_data to analyze contribution ratios by dimension".to_string(),
                );
            }
            if metric_count >= 2 {
                suggestions.push(
                    "Metric columns may have correlations worth further exploration".to_string(),
                );
            }
            if metric_count > 0 {
                suggestions
                    .push("Use bin_data to analyze the distribution of metric columns".to_string());
            }

            let result = serde_json::json!({
                "file": file_path,
                "rows": row_count,
                "cols": col_count,
                "columns": columns_json,
                "summary": {
                    "dimensions": dim_count,
                    "metrics": metric_count,
                    "temporal": temporal_count,
                    "other": col_count - dim_count - metric_count - temporal_count,
                },
                "suggestions": suggestions,
            });

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── TopN analysis tool ───────────────────────────────────────────────

pub struct DataTopNTool;

impl Tool for DataTopNTool {
    fn name(&self) -> &str {
        "topn_data"
    }

    fn description(&self) -> &str {
        "Sort by a metric column and take Top N. Without dimension columns, returns global top N; with dimension_columns specified, returns top N within each group. Suitable for questions like 'top 10 products by sales', 'top 3 categories by revenue in each region'. Example: topn_data(file_path='sales.csv', metric_column='revenue', dimension_columns='region', top_n=3)"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "metric_column": {
                    "type": "string",
                    "description": "Metric column name for sorting"
                },
                "dimension_columns": {
                    "type": "string",
                    "description": "Grouping dimension columns (optional, comma-separated). Global sort if not specified"
                },
                "top_n": {
                    "type": "integer",
                    "description": "Return top N rows (default 10)"
                },
                "ascending": {
                    "type": "boolean",
                    "description": "Whether to sort ascending (default false, i.e., descending)"
                }
            },
            "required": ["file_path", "metric_column"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let metric_col = parameters
                .get("metric_column")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("metric_column".to_string()))?;

            let dim_cols_str = parameters.get("dimension_columns").and_then(|v| v.as_str());

            let top_n = parameters
                .get("top_n")
                .and_then(|v| v.as_u64())
                .unwrap_or(10)
                .clamp(1, 100) as usize;

            let ascending = parameters
                .get("ascending")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;

            let result_df = if let Some(dim_str) = dim_cols_str {
                // Grouped TopN: take top_n records within each group
                let dim_cols: Vec<&str> = dim_str.split(',').map(|s| s.trim()).collect();
                let group_cols: Vec<Expr> = dim_cols.iter().map(|&d| col(d)).collect();

                // Collect all column names (for head operation in agg phase)
                let all_col_names: Vec<String> = df
                    .get_column_names()
                    .iter()
                    .map(|s| s.to_string())
                    .collect();

                // After sorting by metric column, group_by + agg uses head(N) to get top N per group
                // This is the correct way to implement within-group TopN with Polars
                let sort_desc = !ascending;
                let agg_exprs: Vec<Expr> = all_col_names
                    .iter()
                    .map(|c| {
                        if dim_cols.contains(&c.as_str()) {
                            col(c).first()
                        } else {
                            col(c).head(Some(top_n))
                        }
                    })
                    .collect();

                let sorted = df.lazy().sort(
                    [metric_col],
                    SortMultipleOptions {
                        descending: vec![sort_desc],
                        nulls_last: vec![true],
                        multithreaded: true,
                        maintain_order: false,
                        limit: None,
                    },
                );

                // For each group, take TopN rows in sorted order
                // group_by + agg(all().sort_by(metric).head(n)) ensures top n within each group
                sorted
                    .group_by(group_cols)
                    .agg(agg_exprs)
                    .limit((top_n * dim_cols.len().max(1)).try_into().map_err(|_| {
                        ToolError::ExecutionFailed {
                            tool: TOOL_NAME.to_string(),
                            message: "top_n value too large".to_string(),
                        }
                    })?)
                    .collect()
                    .map_err(|e| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: format!("Grouped TopN execution failed: {}", e),
                    })?
            } else {
                // Global TopN
                df.lazy()
                    .sort(
                        [metric_col],
                        SortMultipleOptions {
                            descending: vec![!ascending],
                            nulls_last: vec![true],
                            multithreaded: true,
                            maintain_order: false,
                            limit: Some(top_n.try_into().map_err(|_| {
                                ToolError::ExecutionFailed {
                                    tool: TOOL_NAME.to_string(),
                                    message: "top_n value too large".to_string(),
                                }
                            })?),
                        },
                    )
                    .collect()
                    .map_err(|e| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: format!("TopN sort failed: {}", e),
                    })?
            };

            let data_json = df_to_json(&result_df)?;

            let mut result = serde_json::json!({
                "top_n": top_n,
                "metric_column": metric_col,
                "ascending": ascending,
                "data": data_json,
            });
            if let Some(dim) = dim_cols_str {
                result["dimension_columns"] = serde_json::json!(dim);
            }

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Contribution ratio analysis tool ─────────────────────────────────

pub struct DataContributionTool;

impl Tool for DataContributionTool {
    fn name(&self) -> &str {
        "contribution_data"
    }

    fn description(&self) -> &str {
        "Calculate contribution ratio (percentage) and cumulative ratio (Pareto analysis / 80-20 rule) of each dimension value to the metric column. Outputs dimension value, metric value, ratio (%), cumulative (%). Dimension values beyond top_n are merged into 'Other'. Suitable for questions like 'sales ratio by region', 'which categories contribute 80% of revenue? (Pareto analysis)'. Example: contribution_data(file_path='sales.csv', dimension_column='category', metric_column='revenue', top_n=15)"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "dimension_column": {
                    "type": "string",
                    "description": "Dimension column name (column used for grouping)"
                },
                "metric_column": {
                    "type": "string",
                    "description": "Metric column name (column used for sum calculation)"
                },
                "top_n": {
                    "type": "integer",
                    "description": "Show top N dimension values (default 20, rest grouped as \"Other\")"
                }
            },
            "required": ["file_path", "dimension_column", "metric_column"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let dim_col = parameters
                .get("dimension_column")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("dimension_column".to_string()))?;

            let metric_col = parameters
                .get("metric_column")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("metric_column".to_string()))?;

            let top_n = parameters
                .get("top_n")
                .and_then(|v| v.as_u64())
                .unwrap_or(20)
                .clamp(1, 200) as usize;

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;

            // Group by dimension, sum metric
            let agg_df = df
                .lazy()
                .group_by([col(dim_col)])
                .agg([col(metric_col).sum().alias(metric_col)])
                .sort(
                    [metric_col],
                    SortMultipleOptions {
                        descending: vec![true],
                        nulls_last: vec![true],
                        multithreaded: true,
                        maintain_order: false,
                        limit: None,
                    },
                )
                .collect()
                .map_err(|e| ToolError::ExecutionFailed {
                    tool: TOOL_NAME.to_string(),
                    message: format!("Group aggregation failed: {}", e),
                })?;

            let total: f64 = agg_df
                .column(metric_col)
                .ok()
                .and_then(|c| {
                    let s = c.as_materialized_series();
                    s.sum::<f64>().ok()
                })
                .unwrap_or(0.0);

            if total == 0.0 {
                return Ok(ToolResult::success_json(serde_json::json!({
                    "dimension_column": dim_col,
                    "metric_column": metric_col,
                    "total": 0.0,
                    "error": "Metric total is 0, cannot calculate ratio",
                })));
            }

            let height = agg_df.height();

            let mut items = Vec::new();
            let mut cumulative = 0.0;
            let display_rows = top_n.min(height);
            let mut other_sum = 0.0;
            let mut other_count = 0u64;

            for i in 0..height {
                let dim_val = agg_df
                    .column(dim_col)
                    .and_then(|c| c.get(i).map(|v| format_value(&v)))
                    .unwrap_or_else(|_| "-".to_string());

                let metric_val: f64 = agg_df
                    .column(metric_col)
                    .map(|c| {
                        let s = c.as_materialized_series();
                        s.get(i)
                            .map(|v| match v {
                                polars::prelude::AnyValue::Float64(f) => f,
                                polars::prelude::AnyValue::Float32(f) => f as f64,
                                polars::prelude::AnyValue::Int64(i) => i as f64,
                                polars::prelude::AnyValue::Int32(i) => i as f64,
                                polars::prelude::AnyValue::UInt64(i) => i as f64,
                                polars::prelude::AnyValue::UInt32(i) => i as f64,
                                _ => 0.0,
                            })
                            .unwrap_or(0.0)
                    })
                    .unwrap_or(0.0);

                if i < display_rows {
                    let pct = ((metric_val / total) * 10000.0).round() / 100.0;
                    cumulative += pct;
                    items.push(serde_json::json!({
                        "dim_value": dim_val,
                        "metric_value": metric_val,
                        "pct": pct,
                        "cumulative_pct": (cumulative * 100.0).round() / 100.0,
                    }));
                } else {
                    other_sum += metric_val;
                    other_count += 1;
                }
            }

            let mut result = serde_json::json!({
                "dimension_column": dim_col,
                "metric_column": metric_col,
                "total": total,
                "items": items,
            });

            if other_count > 0 {
                let other_pct = ((other_sum / total) * 10000.0).round() / 100.0;
                cumulative += other_pct;
                result["other"] = serde_json::json!({
                    "count": other_count,
                    "sum": other_sum,
                    "pct": other_pct,
                    "cumulative_pct": (cumulative * 100.0).round() / 100.0,
                });
            }

            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Numeric binning tool ─────────────────────────────────────────────

pub struct DataBinTool;

impl Tool for DataBinTool {
    fn name(&self) -> &str {
        "bin_data"
    }

    fn description(&self) -> &str {
        "Bin numeric columns (equal-width / equal-frequency), counting records per bin and summarizing metrics. Suitable for analyzing data distribution and generating histogram data."
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "column": {
                    "type": "string",
                    "description": "Numeric column name to bin"
                },
                "num_bins": {
                    "type": "integer",
                    "description": "Number of bins (default 10)"
                },
                "method": {
                    "type": "string",
                    "description": "Binning method: 'equal_width' (default) or 'equal_frequency'"
                }
            },
            "required": ["file_path", "column"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let col_name = parameters
                .get("column")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("column".to_string()))?;

            let num_bins = parameters
                .get("num_bins")
                .and_then(|v| v.as_u64())
                .unwrap_or(10)
                .clamp(2, 50) as usize;

            let method = parameters
                .get("method")
                .and_then(|v| v.as_str())
                .unwrap_or("equal_width");

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;
            let c = df
                .column(col_name)
                .map_err(|_| ToolError::InvalidParameter {
                    name: "column".to_string(),
                    message: format!("Column '{}' not found", col_name),
                })?;

            let series = c.as_materialized_series();
            let values: Vec<f64> = series
                .cast(&DataType::Float64)
                .unwrap_or_default()
                .f64()
                .unwrap_or(&polars::prelude::Float64Chunked::full(
                    PlSmallStr::from_static("tmp"),
                    0.0,
                    0,
                ))
                .iter()
                .flatten()
                .collect();

            if values.is_empty() {
                return Ok(ToolResult::success(
                    "This column has no valid numeric data".to_string(),
                ));
            }

            let mut sorted = values.clone();
            sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

            let min_val = sorted[0];
            let max_val = sorted[sorted.len() - 1];

            let bins = match method {
                "equal_frequency" => {
                    let n = sorted.len();
                    let mut bins = Vec::new();
                    let per_bin = (n as f64 / num_bins as f64).ceil() as usize;
                    for i in 0..num_bins {
                        let start_idx = i * per_bin;
                        if start_idx >= n {
                            break;
                        }
                        let end_idx = ((i + 1) * per_bin).min(n);
                        let bin_start = sorted[start_idx];
                        let bin_end = if end_idx >= n {
                            sorted[n - 1]
                        } else {
                            sorted[end_idx - 1]
                        };
                        let count = end_idx - start_idx;
                        let bin_vals: Vec<f64> = sorted[start_idx..end_idx].to_vec();
                        let bin_sum: f64 = bin_vals.iter().sum();
                        let bin_mean = bin_sum / count as f64;
                        bins.push((bin_start, bin_end, count, bin_sum, bin_mean));
                    }
                    bins
                }
                _ => {
                    // equal_width (default)
                    let mut bins = Vec::new();
                    let width = (max_val - min_val) / num_bins as f64;
                    if width == 0.0 {
                        bins.push((
                            min_val,
                            max_val,
                            values.len(),
                            values.iter().sum(),
                            values.iter().sum::<f64>() / values.len() as f64,
                        ));
                    } else {
                        for i in 0..num_bins {
                            let bin_start = min_val + i as f64 * width;
                            let bin_end = if i == num_bins - 1 {
                                max_val + 0.0001 // include max
                            } else {
                                bin_start + width
                            };
                            let bin_vals: Vec<f64> = values
                                .iter()
                                .filter(|&&v| {
                                    if i == num_bins - 1 {
                                        v >= bin_start && v <= max_val
                                    } else {
                                        v >= bin_start && v < bin_end
                                    }
                                })
                                .copied()
                                .collect();
                            let count = bin_vals.len();
                            let bin_sum: f64 = bin_vals.iter().sum();
                            let bin_mean = if count > 0 {
                                bin_sum / count as f64
                            } else {
                                0.0
                            };
                            bins.push((bin_start, bin_end, count, bin_sum, bin_mean));
                        }
                    }
                    bins
                }
            };

            let total_count = values.len();
            let bins_json: Vec<Value> = bins
                .iter()
                .map(|(start, end, count, sum_val, mean_val)| {
                    let pct = (*count as f64 / total_count as f64) * 100.0;
                    serde_json::json!({
                        "range": [format!("{:.2}", start), format!("{:.2}", end)],
                        "count": count,
                        "pct": format!("{:.1}", pct),
                        "sum": format!("{:.2}", sum_val),
                        "mean": format!("{:.2}", mean_val),
                    })
                })
                .collect();

            let result = serde_json::json!({
                "column": col_name,
                "method": if method == "equal_frequency" { "equal_frequency" } else { "equal_width" },
                "num_bins": bins.len(),
                "range": [min_val, max_val],
                "total_count": total_count,
                "bins": bins_json,
            });
            Ok(ToolResult::success_json(result))
        })
    }
}

// ── Ratio / expression computation tool ──────────────────────────────

pub struct DataRatioTool;

impl Tool for DataRatioTool {
    fn name(&self) -> &str {
        "ratio_data"
    }

    fn description(&self) -> &str {
        "Compute arithmetic expressions and ratios between columns. Supports +, -, *, / and parentheses, with optional grouping dimensions for within-group ratios. Suitable for computing profit margin, conversion rate, YoY/MoM, proportions, etc. Example: ratio_data(file_path='sales.csv', expressions='profit_margin:(revenue-cost)/revenue*100, ratio:cost/revenue')"
    }

    fn parameters(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "file_path": {
                    "type": "string",
                    "description": "Absolute path to the data file"
                },
                "expressions": {
                    "type": "string",
                    "description": "Expression definitions, comma-separated. Format: 'alias:expression'. Expressions support +, -, *, / and parentheses, referencing column names and numeric constants. Example: 'margin:(revenue-cost)/revenue*100, ratio:a/b'"
                },
                "dimension_columns": {
                    "type": "string",
                    "description": "Grouping dimension column names (optional, comma-separated). When specified, expressions are computed within each group"
                },
                "limit": {
                    "type": "integer",
                    "description": "Row count limit (default 50)"
                }
            },
            "required": ["file_path", "expressions"]
        })
    }

    fn execute(&self, parameters: ToolParameters) -> BoxFuture<'_, Result<ToolResult>> {
        Box::pin(async move {
            let file_path = parameters
                .get("file_path")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("file_path".to_string()))?;

            let exprs_str = parameters
                .get("expressions")
                .and_then(|v| v.as_str())
                .ok_or_else(|| ToolError::MissingParameter("expressions".to_string()))?;

            let dim_cols_str = parameters.get("dimension_columns").and_then(|v| v.as_str());

            let limit = parameters
                .get("limit")
                .and_then(|v| v.as_u64())
                .unwrap_or(50)
                .clamp(1, 500) as usize;

            let security = SecurityConfig::global();
            let path = security.validate_file(file_path)?;
            let format = parameters.get("format").and_then(|v| v.as_str());

            let df = load_dataframe(&path, format)?;

            // Collect valid column names
            let valid_columns: Vec<String> = df
                .get_column_names()
                .iter()
                .map(|s| s.to_string())
                .collect();

            // Parse expressions
            let parsed_exprs = parse_ratio_expressions(exprs_str, &valid_columns)?;

            // Build Polars expressions
            let mut polars_exprs: Vec<Expr> = Vec::new();
            for (alias, _) in &parsed_exprs {
                let polars_expr = build_ratio_expr(exprs_str, &valid_columns, alias)?;
                polars_exprs.push(polars_expr);
            }

            // If grouping columns exist, group first then compute; otherwise select directly
            let result_df = if let Some(dim_str) = dim_cols_str {
                let dim_cols: Vec<&str> = dim_str.split(',').map(|s| s.trim()).collect();

                // Validate all grouping columns exist
                for dc in &dim_cols {
                    if !valid_columns.iter().any(|c| c == dc) {
                        return Err(ToolError::InvalidParameter {
                            name: "dimension_columns".to_string(),
                            message: format!(
                                "Group column '{}' not found. Available columns: {}",
                                dc,
                                valid_columns.join(", ")
                            ),
                        }
                        .into());
                    }
                }

                let group_cols: Vec<Expr> = dim_cols.iter().map(|&d| col(d)).collect();

                df.lazy()
                    .group_by(group_cols)
                    .agg(polars_exprs.clone())
                    .sort(
                        [dim_cols[0]],
                        SortMultipleOptions {
                            descending: vec![false],
                            nulls_last: vec![true],
                            multithreaded: true,
                            maintain_order: false,
                            limit: Some(limit.try_into().map_err(|_| {
                                ToolError::ExecutionFailed {
                                    tool: TOOL_NAME.to_string(),
                                    message: "limit value too large".to_string(),
                                }
                            })?),
                        },
                    )
                    .collect()
                    .map_err(|e| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: format!("Grouped ratio calculation failed: {}", e),
                    })?
            } else {
                // No grouping: directly select expression columns + first rows of all original columns as context
                let mut all_exprs: Vec<Expr> = valid_columns.iter().map(col).collect();
                all_exprs.extend(polars_exprs);

                df.lazy()
                    .select(all_exprs)
                    .limit(limit.try_into().map_err(|_| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: "limit value too large".to_string(),
                    })?)
                    .collect()
                    .map_err(|e| ToolError::ExecutionFailed {
                        tool: TOOL_NAME.to_string(),
                        message: format!("Expression evaluation failed: {}", e),
                    })?
            };

            let data_json = df_to_json(&result_df)?;
            let mut result = serde_json::json!({
                "expressions": exprs_str,
                "data": data_json,
            });
            if let Some(dim) = dim_cols_str {
                result["dimension_columns"] = serde_json::json!(dim);
            }
            Ok(ToolResult::success_json(result))
        })
    }
}

/// Parse ratio expression string, returns (alias, expression) list
/// Format: "alias1:expr1, alias2:expr2"
fn parse_ratio_expressions(
    expr_str: &str,
    valid_columns: &[String],
) -> Result<Vec<(String, String)>> {
    let mut result = Vec::new();
    let mut depth = 0;
    let mut current = String::new();

    for ch in expr_str.chars() {
        match ch {
            '(' => {
                depth += 1;
                current.push(ch);
            }
            ')' => {
                depth -= 1;
                current.push(ch);
            }
            ',' if depth == 0 => {
                let trimmed = current.trim().to_string();
                if !trimmed.is_empty() {
                    let (alias, expr) = parse_single_expression(&trimmed, valid_columns)?;
                    result.push((alias, expr));
                }
                current.clear();
            }
            _ => current.push(ch),
        }
    }

    let trimmed = current.trim().to_string();
    if !trimmed.is_empty() {
        let (alias, expr) = parse_single_expression(&trimmed, valid_columns)?;
        result.push((alias, expr));
    }

    if result.is_empty() {
        return Err(ToolError::InvalidParameter {
            name: "expressions".to_string(),
            message: format!(
                "Expression format error: '{}'. Correct format: 'alias:expression', e.g. 'profit_margin:(revenue-cost)/revenue*100'",
                expr_str
            ),
        }
        .into());
    }

    Ok(result)
}

/// Parse a single "alias:expression" pair
fn parse_single_expression(spec: &str, _valid_columns: &[String]) -> Result<(String, String)> {
    let colon_pos = spec.find(':').ok_or_else(|| ToolError::InvalidParameter {
        name: "expressions".to_string(),
        message: format!(
            "Expression '{}' is missing colon separator. Format: 'alias:expression'",
            spec
        ),
    })?;

    let alias = spec[..colon_pos].trim().to_string();
    let expr = spec[colon_pos + 1..].trim().to_string();

    if alias.is_empty() || expr.is_empty() {
        return Err(ToolError::InvalidParameter {
            name: "expressions".to_string(),
            message: format!("Expression '{}' has empty alias or expression", spec),
        }
        .into());
    }

    // Alias cannot be a number
    if alias.parse::<f64>().is_ok() {
        return Err(ToolError::InvalidParameter {
            name: "expressions".to_string(),
            message: format!("Alias '{}' cannot be a numeric value", alias),
        }
        .into());
    }

    Ok((alias, expr))
}

/// Build a Polars Expr from expression and alias (for a single parsed expression)
fn build_ratio_expr(exprs_str: &str, valid_columns: &[String], target_alias: &str) -> Result<Expr> {
    let parsed = parse_ratio_expressions(exprs_str, valid_columns)?;
    for (alias, expr_text) in &parsed {
        if alias == target_alias {
            return build_single_polars_expr(expr_text, valid_columns, alias);
        }
    }
    Err(ToolError::InvalidParameter {
        name: "expressions".to_string(),
        message: format!("Cannot find expression for alias '{}'", target_alias),
    }
    .into())
}

/// Compile a single arithmetic expression into a Polars Expr
fn build_single_polars_expr(
    expr_text: &str,
    valid_columns: &[String],
    alias: &str,
) -> Result<Expr> {
    let tokens = tokenize_expr(expr_text, valid_columns)?;
    let (_, expr) = parse_expr_tokens(&tokens, 0, valid_columns)?;

    // Try casting to ensure Float64 type
    Ok(expr.alias(alias))
}

/// Token type
#[derive(Debug, Clone, PartialEq)]
enum ExprToken {
    ColRef(String),
    Number(f64),
    Plus,
    Minus,
    Star,
    Slash,
    LParen,
    RParen,
}

/// Tokenize an expression string
fn tokenize_expr(expr_text: &str, valid_columns: &[String]) -> Result<Vec<ExprToken>> {
    let mut tokens = Vec::new();
    let chars: Vec<char> = expr_text.chars().collect();
    let len = chars.len();
    let mut i = 0;

    while i < len {
        let ch = chars[i];

        // Skip whitespace
        if ch.is_whitespace() {
            i += 1;
            continue;
        }

        match ch {
            '+' => {
                tokens.push(ExprToken::Plus);
                i += 1;
            }
            '-' => {
                tokens.push(ExprToken::Minus);
                i += 1;
            }
            '*' => {
                tokens.push(ExprToken::Star);
                i += 1;
            }
            '/' => {
                tokens.push(ExprToken::Slash);
                i += 1;
            }
            '(' => {
                tokens.push(ExprToken::LParen);
                i += 1;
            }
            ')' => {
                tokens.push(ExprToken::RParen);
                i += 1;
            }
            _ if ch.is_ascii_digit() || ch == '.' => {
                // Number literal
                let start = i;
                while i < len && (chars[i].is_ascii_digit() || chars[i] == '.') {
                    i += 1;
                }
                let num_str: String = chars[start..i].iter().collect();
                let num: f64 = num_str.parse().map_err(|_| ToolError::InvalidParameter {
                    name: "expressions".to_string(),
                    message: format!("Cannot parse number: '{}'", num_str),
                })?;
                tokens.push(ExprToken::Number(num));
            }
            _ if ch.is_alphabetic() || ch == '_' => {
                // Identifier (column name)
                let start = i;
                while i < len && (chars[i].is_alphanumeric() || chars[i] == '_') {
                    i += 1;
                }
                let ident: String = chars[start..i].iter().collect();

                // Check if it's a valid column name
                if !valid_columns.iter().any(|c| c == &ident) {
                    return Err(ToolError::InvalidParameter {
                        name: "expressions".to_string(),
                        message: format!(
                            "Column '{}' in expression does not exist. Available columns: {}",
                            ident,
                            valid_columns.join(", ")
                        ),
                    }
                    .into());
                }

                tokens.push(ExprToken::ColRef(ident));
            }
            _ => {
                return Err(ToolError::InvalidParameter {
                    name: "expressions".to_string(),
                    message: format!("Invalid character in expression: '{}'", ch),
                }
                .into());
            }
        }
    }

    Ok(tokens)
}

/// Recursive descent parser for expression token stream
/// Grammar: expr = term (('+' | '-') term)*
///       term = factor (('*' | '/') factor)*
///       factor = NUMBER | ColRef | '(' expr ')'
fn parse_expr_tokens(
    tokens: &[ExprToken],
    pos: usize,
    valid_columns: &[String],
) -> Result<(usize, Expr)> {
    let (pos, mut left) = parse_term(tokens, pos, valid_columns)?;

    let mut p = pos;
    while p < tokens.len() {
        match tokens[p] {
            ExprToken::Plus => {
                let (next_pos, right) = parse_term(tokens, p + 1, valid_columns)?;
                left = left + right;
                p = next_pos;
            }
            ExprToken::Minus => {
                let (next_pos, right) = parse_term(tokens, p + 1, valid_columns)?;
                left = left - right;
                p = next_pos;
            }
            _ => break,
        }
    }

    Ok((p, left))
}

/// Parse term: factor (('*' | '/') factor)*
fn parse_term(tokens: &[ExprToken], pos: usize, valid_columns: &[String]) -> Result<(usize, Expr)> {
    let (pos, mut left) = parse_factor(tokens, pos, valid_columns)?;

    let mut p = pos;
    while p < tokens.len() {
        match tokens[p] {
            ExprToken::Star => {
                let (next_pos, right) = parse_factor(tokens, p + 1, valid_columns)?;
                left = left * right;
                p = next_pos;
            }
            ExprToken::Slash => {
                let (next_pos, right) = parse_factor(tokens, p + 1, valid_columns)?;
                left = left / right;
                p = next_pos;
            }
            _ => break,
        }
    }

    Ok((p, left))
}

/// Parse factor: NUMBER | ColRef | '(' expr ')'
fn parse_factor(
    tokens: &[ExprToken],
    pos: usize,
    _valid_columns: &[String],
) -> Result<(usize, Expr)> {
    if pos >= tokens.len() {
        return Err(ToolError::InvalidParameter {
            name: "expressions".to_string(),
            message: "Incomplete expression: missing operand".to_string(),
        }
        .into());
    }

    match &tokens[pos] {
        ExprToken::Number(n) => Ok((pos + 1, lit(*n))),
        ExprToken::ColRef(name) => Ok((pos + 1, col(name.as_str()))),
        ExprToken::LParen => {
            let (next_pos, inner) = parse_expr_tokens(tokens, pos + 1, _valid_columns)?;
            if next_pos < tokens.len() && tokens[next_pos] == ExprToken::RParen {
                Ok((next_pos + 1, inner))
            } else {
                Err(ToolError::InvalidParameter {
                    name: "expressions".to_string(),
                    message: "Expression is missing closing parenthesis ')'".to_string(),
                }
                .into())
            }
        }
        _ => Err(ToolError::InvalidParameter {
            name: "expressions".to_string(),
            message: "Unexpected token in expression: expected number, column name, or '(' but got operator".to_string(),
        }
        .into()),
    }
}

// ── Helper functions ──────────────────────────────────────────────────

/// Format a Polars value
fn format_value(value: &AnyValue) -> String {
    match value {
        AnyValue::Null => "-".to_string(),
        AnyValue::Boolean(b) => b.to_string(),
        AnyValue::Int8(i) => i.to_string(),
        AnyValue::Int16(i) => i.to_string(),
        AnyValue::Int32(i) => i.to_string(),
        AnyValue::Int64(i) => i.to_string(),
        AnyValue::UInt8(i) => i.to_string(),
        AnyValue::UInt16(i) => i.to_string(),
        AnyValue::UInt32(i) => i.to_string(),
        AnyValue::UInt64(i) => i.to_string(),
        AnyValue::Float32(f) => format!("{:.2}", f),
        AnyValue::Float64(f) => format!("{:.2}", f),
        AnyValue::String(s) => s.to_string(),
        AnyValue::StringOwned(s) => s.to_string(),
        _ => value.to_string(),
    }
}

/// Convert DataFrame to a JSON array
fn df_to_json(df: &DataFrame) -> Result<Value> {
    let columns: Vec<String> = df
        .get_column_names()
        .iter()
        .map(|s| s.to_string())
        .collect();
    let mut records = Vec::new();

    for i in 0..df.height() {
        let mut record = serde_json::Map::new();
        for col in &columns {
            if let Ok(c) = df.column(col.as_str()) {
                let value = c
                    .get(i)
                    .map(|v| any_value_to_json(&v))
                    .unwrap_or(Value::Null);
                record.insert(col.clone(), value);
            }
        }
        records.push(Value::Object(record));
    }

    Ok(Value::Array(records))
}

/// Convert AnyValue to JSON Value
fn any_value_to_json(value: &AnyValue) -> Value {
    match value {
        AnyValue::Null => Value::Null,
        AnyValue::Boolean(b) => Value::Bool(*b),
        AnyValue::Int8(i) => Value::Number((*i).into()),
        AnyValue::Int16(i) => Value::Number((*i).into()),
        AnyValue::Int32(i) => Value::Number((*i).into()),
        AnyValue::Int64(i) => Value::Number((*i).into()),
        AnyValue::UInt8(i) => Value::Number((*i).into()),
        AnyValue::UInt16(i) => Value::Number((*i).into()),
        AnyValue::UInt32(i) => Value::Number((*i).into()),
        AnyValue::UInt64(i) => Value::Number((*i).into()),
        AnyValue::Float32(f) => serde_json::Number::from_f64(*f as f64)
            .map(Value::Number)
            .unwrap_or(Value::Null),
        AnyValue::Float64(f) => serde_json::Number::from_f64(*f)
            .map(Value::Number)
            .unwrap_or(Value::Null),
        AnyValue::String(s) => Value::String(s.to_string()),
        AnyValue::StringOwned(s) => Value::String(s.to_string()),
        _ => Value::String(value.to_string()),
    }
}

/// Parse filter expression (extended: supports AND/OR, contains, starts_with, ends_with, in)
fn parse_filter_expression(expr_str: &str) -> Result<Expr> {
    // Try splitting by AND / OR first
    for separator in [" AND ", " and ", " OR ", " or "] {
        let parts: Vec<&str> = if let Some(pos) = expr_str.find(separator) {
            let left = &expr_str[..pos];
            let right = &expr_str[pos + separator.len()..];
            vec![left, right]
        } else {
            vec![]
        };

        if parts.len() == 2 {
            let left_expr = parse_filter_expression(parts[0])?;
            let right_expr = parse_filter_expression(parts[1])?;
            return if separator.trim().to_lowercase() == "and" {
                Ok(left_expr.and(right_expr))
            } else {
                Ok(left_expr.or(right_expr))
            };
        }
    }

    let s = expr_str.trim();

    // Numeric comparison
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*>=\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).gt_eq(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*<=\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).lt_eq(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*!=\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).neq(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*==\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).eq(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*>\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).gt(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r"^(\w+)\s*<\s*([\d.]+)$")
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val: f64 = cap.get(2).unwrap().as_str().parse().unwrap_or(0.0);
        return Ok(col(col_name).lt(lit(val)));
    }

    // String comparison
    if let Ok(re) = regex::Regex::new(r#"^(\w+)\s*==\s*"([^"]+)"$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val = cap.get(2).unwrap().as_str();
        return Ok(col(col_name).eq(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r#"^(\w+)\s*!=\s*"([^"]+)"$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val = cap.get(2).unwrap().as_str();
        return Ok(col(col_name).neq(lit(val)));
    }

    // String contains/starts_with/ends_with matching
    if let Ok(re) = regex::Regex::new(r#"^(\w+)\s+contains\s+"([^"]+)"$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val = cap.get(2).unwrap().as_str();
        return Ok(col(col_name).str().contains(lit(val), false));
    }
    if let Ok(re) = regex::Regex::new(r#"^(\w+)\s+starts_with\s+"([^"]+)"$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val = cap.get(2).unwrap().as_str();
        return Ok(col(col_name).str().starts_with(lit(val)));
    }
    if let Ok(re) = regex::Regex::new(r#"^(\w+)\s+ends_with\s+"([^"]+)"$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let val = cap.get(2).unwrap().as_str();
        return Ok(col(col_name).str().ends_with(lit(val)));
    }

    // IN expression
    if let Ok(re) = regex::Regex::new(r#"(?s)^(\w+)\s+in\s*\((.+)\)$"#)
        && let Some(cap) = re.captures(s)
    {
        let col_name = cap.get(1).unwrap().as_str();
        let vals_str = cap.get(2).unwrap().as_str();
        let vals: Vec<String> = vals_str
            .split(',')
            .map(|v| v.trim().trim_matches('"').to_string())
            .collect();
        if !vals.is_empty() {
            let series = Series::new(PlSmallStr::EMPTY, vals);
            return Ok(col(col_name).is_in(lit(series), false));
        }
    }

    Err(ToolError::InvalidParameter {
        name: "filter".to_string(),
        message: format!(
            "Cannot parse filter expression: '{}'. Supported formats: col > 10, col == \"val\", col contains \"sub\", col starts_with \"pre\", col in (\"a\",\"b\"), A > 10 AND B < 5",
            expr_str
        ),
    }
    .into())
}

/// Parse aggregation expression (extended: supports more operations)
fn parse_aggregations(agg_str: &str) -> Result<Vec<Expr>> {
    let mut exprs = Vec::new();

    for part in agg_str.split(',') {
        let parts: Vec<&str> = part.trim().split(':').collect();
        if parts.len() != 2 {
            return Err(ToolError::InvalidParameter {
                name: "aggregations".to_string(),
                message: format!(
                    "Aggregation expression format error: '{}', expected 'column:operation'",
                    part
                ),
            }
            .into());
        }

        let col_name = parts[0].trim();
        let op = parts[1].trim();

        let expr = match op {
            "sum" => col(col_name).sum().alias(format!("{}_sum", col_name)),
            "mean" | "avg" => col(col_name).mean().alias(format!("{}_mean", col_name)),
            "min" => col(col_name).min().alias(format!("{}_min", col_name)),
            "max" => col(col_name).max().alias(format!("{}_max", col_name)),
            "count" => col(col_name).count().alias(format!("{}_count", col_name)),
            "count_distinct" | "n_unique" => col(col_name)
                .n_unique()
                .alias(format!("{}_distinct", col_name)),
            "variance" | "var" => col(col_name).var(1).alias(format!("{}_var", col_name)),
            "stddev" | "std" => col(col_name).std(1).alias(format!("{}_std", col_name)),
            "median" => col(col_name).median().alias(format!("{}_median", col_name)),
            "p90" => col(col_name)
                .quantile(0.9.into(), QuantileMethod::default())
                .alias(format!("{}_p90", col_name)),
            "p95" => col(col_name)
                .quantile(0.95.into(), QuantileMethod::default())
                .alias(format!("{}_p95", col_name)),
            "p25" => col(col_name)
                .quantile(0.25.into(), QuantileMethod::default())
                .alias(format!("{}_p25", col_name)),
            "p75" => col(col_name)
                .quantile(0.75.into(), QuantileMethod::default())
                .alias(format!("{}_p75", col_name)),
            "first" => col(col_name).first().alias(format!("{}_first", col_name)),
            "last" => col(col_name).last().alias(format!("{}_last", col_name)),
            _ => {
                // Support percentile:N for custom percentile
                if op.starts_with("percentile:") || op.starts_with("pct:") {
                    let pct_str = op
                        .strip_prefix("percentile:")
                        .or_else(|| op.strip_prefix("pct:"))
                        .unwrap_or("50");
                    let pct: f64 = pct_str.parse().map_err(|_| ToolError::InvalidParameter {
                        name: "aggregations".to_string(),
                        message: format!("Invalid percentile format: '{}'", pct_str),
                    })?;
                    if !(0.0..=100.0).contains(&pct) {
                        return Err(ToolError::InvalidParameter {
                            name: "aggregations".to_string(),
                            message: format!("Percentile must be between 0 and 100: {}", pct),
                        }
                        .into());
                    }
                    let q = pct / 100.0;
                    col(col_name)
                        .quantile(q.into(), QuantileMethod::default())
                        .alias(format!("{}_p{:.0}", col_name, pct))
                } else {
                    return Err(ToolError::InvalidParameter {
                        name: "aggregations".to_string(),
                        message: format!(
                            "Unsupported aggregation operation: '{}'. Supported: sum, mean/avg, min, max, count, count_distinct, variance, stddev, median, p25, p75, p90, p95, percentile:N, first, last",
                            op
                        ),
                    }
                    .into());
                }
            }
        };

        exprs.push(expr);
    }

    Ok(exprs)
}