jpx 0.1.15

JMESPath CLI with 320+ extended functions - a powerful jq alternative
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
//! Interactive REPL for jpx
//!
//! Provides an interactive environment for exploring JSON data with JMESPath queries.

// Allow nested if-let blocks instead of if-let chains for MSRV compatibility
#![allow(clippy::collapsible_if)]

use anyhow::{Context, Result};
use jmespath::{Runtime, Variable};
use jmespath_extensions::register_all;
use jmespath_extensions::registry::{Category, FunctionRegistry};
use rustyline::completion::{Completer, Pair};
use rustyline::error::ReadlineError;
use rustyline::highlight::{CmdKind, Highlighter};
use rustyline::hint::Hinter;
use rustyline::history::DefaultHistory;
use rustyline::validate::Validator;
use rustyline::{Editor, Helper};
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;

// ANSI color codes - using basic 16-color for better terminal compatibility
mod colors {
    pub const RESET: &str = "\x1b[0m";
    pub const BOLD: &str = "\x1b[1m";

    // JMESPath syntax (basic 16-color)
    pub const FUNCTION: &str = "\x1b[36m"; // Cyan
    pub const STRING: &str = "\x1b[32m"; // Green
    pub const NUMBER: &str = "\x1b[33m"; // Yellow
    pub const LITERAL: &str = "\x1b[35m"; // Magenta
    pub const OPERATOR: &str = "\x1b[31m"; // Red
    pub const BRACKET: &str = "\x1b[37m"; // White
    pub const FIELD: &str = "\x1b[97m"; // Bright white
    pub const AT: &str = "\x1b[93m"; // Bright yellow
    pub const AMPERSAND: &str = "\x1b[35m"; // Magenta

    // JSON output
    pub const JSON_KEY: &str = "\x1b[34m"; // Blue
    pub const JSON_STRING: &str = "\x1b[32m"; // Green
    pub const JSON_NUMBER: &str = "\x1b[33m"; // Yellow
    pub const JSON_BOOL: &str = "\x1b[93m"; // Bright yellow
    pub const JSON_NULL: &str = "\x1b[90m"; // Bright black (gray)

    // UI
    pub const PROMPT: &str = "\x1b[36m"; // Cyan
    pub const ERROR: &str = "\x1b[91m"; // Bright red
    pub const SUCCESS: &str = "\x1b[32m"; // Green
    pub const INFO: &str = "\x1b[90m"; // Bright black (gray)
    pub const HINT: &str = "\x1b[90m"; // Bright black (gray)
}

// Demo themes with pre-loaded data and suggested queries
// Generated from demos.toml at build time
//
// Query complexity levels (1-5):
// 1: Basic field access, simple projections
// 2: Filters, sorting, basic functions
// 3: Aggregations, grouping, transforms
// 4: Pipelines, chained operations
// 5: Advanced patterns, complex expressions
include!(concat!(env!("OUT_DIR"), "/demos_generated.rs"));

/// JMESPath syntax highlighter and completer
pub struct JmespathHelper {
    functions: HashSet<String>,
    data_fields: Rc<RefCell<Vec<String>>>,
}

impl JmespathHelper {
    pub fn new(data_fields: Rc<RefCell<Vec<String>>>) -> Self {
        let mut registry = FunctionRegistry::new();
        registry.register_all();

        let functions: HashSet<String> = registry.functions().map(|f| f.name.to_string()).collect();

        Self {
            functions,
            data_fields,
        }
    }

    /// Highlight JMESPath expression
    fn highlight_jmespath(&self, line: &str) -> String {
        let mut result = String::new();
        let chars: Vec<char> = line.chars().collect();
        let mut i = 0;

        while i < chars.len() {
            let c = chars[i];

            match c {
                // String literals
                '\'' => {
                    result.push_str(colors::STRING);
                    result.push(c);
                    i += 1;
                    while i < chars.len() && chars[i] != '\'' {
                        if chars[i] == '\\' && i + 1 < chars.len() {
                            result.push(chars[i]);
                            i += 1;
                            if i < chars.len() {
                                result.push(chars[i]);
                                i += 1;
                            }
                        } else {
                            result.push(chars[i]);
                            i += 1;
                        }
                    }
                    if i < chars.len() {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push_str(colors::RESET);
                }

                // Backtick literals
                '`' => {
                    result.push_str(colors::LITERAL);
                    result.push(c);
                    i += 1;
                    while i < chars.len() && chars[i] != '`' {
                        if chars[i] == '\\' && i + 1 < chars.len() {
                            result.push(chars[i]);
                            i += 1;
                            if i < chars.len() {
                                result.push(chars[i]);
                                i += 1;
                            }
                        } else {
                            result.push(chars[i]);
                            i += 1;
                        }
                    }
                    if i < chars.len() {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push_str(colors::RESET);
                }

                // Current node
                '@' => {
                    result.push_str(colors::AT);
                    result.push(c);
                    result.push_str(colors::RESET);
                    i += 1;
                }

                // Expression reference
                '&' => {
                    result.push_str(colors::AMPERSAND);
                    result.push(c);
                    result.push_str(colors::RESET);
                    i += 1;
                }

                // Brackets
                '[' | ']' | '{' | '}' | '(' | ')' => {
                    result.push_str(colors::BRACKET);
                    result.push(c);
                    result.push_str(colors::RESET);
                    i += 1;
                }

                // Operators
                '|' | '.' | ',' | ':' | '?' | '*' => {
                    result.push_str(colors::OPERATOR);
                    result.push(c);
                    result.push_str(colors::RESET);
                    i += 1;
                }

                // Comparison operators (including ! for !=)
                '=' | '<' | '>' | '!' => {
                    result.push_str(colors::OPERATOR);
                    result.push(c);
                    i += 1;
                    // Handle ==, !=, <=, >=
                    if i < chars.len() && chars[i] == '=' {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push_str(colors::RESET);
                }

                // Identifiers (fields or functions)
                'a'..='z' | 'A'..='Z' | '_' => {
                    let start = i;
                    while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                        i += 1;
                    }
                    let word: String = chars[start..i].iter().collect();

                    // Check if it's a function (followed by '(')
                    let is_function =
                        i < chars.len() && chars[i] == '(' && self.functions.contains(&word);

                    if is_function {
                        result.push_str(colors::FUNCTION);
                        result.push_str(&word);
                        result.push_str(colors::RESET);
                    } else {
                        result.push_str(colors::FIELD);
                        result.push_str(&word);
                        result.push_str(colors::RESET);
                    }
                }

                // Numbers
                '0'..='9' | '-'
                    if c == '-' && i + 1 < chars.len() && chars[i + 1].is_ascii_digit() =>
                {
                    result.push_str(colors::NUMBER);
                    result.push(c);
                    i += 1;
                    while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '.') {
                        result.push(chars[i]);
                        i += 1;
                    }
                    result.push_str(colors::RESET);
                }

                // Everything else
                _ => {
                    result.push(c);
                    i += 1;
                }
            }
        }

        result
    }
}

impl Completer for JmespathHelper {
    type Candidate = Pair;

    fn complete(
        &self,
        line: &str,
        pos: usize,
        _ctx: &rustyline::Context<'_>,
    ) -> rustyline::Result<(usize, Vec<Pair>)> {
        // Find the start of the current word
        let line_to_pos = &line[..pos];
        let word_start = line_to_pos
            .rfind(|c: char| !c.is_alphanumeric() && c != '_')
            .map(|i| i + 1)
            .unwrap_or(0);

        let prefix = &line[word_start..pos];

        if prefix.is_empty() {
            return Ok((pos, vec![]));
        }

        let mut completions: Vec<Pair> = self
            .functions
            .iter()
            .filter(|f| f.starts_with(prefix))
            .map(|f| Pair {
                display: f.clone(),
                replacement: format!("{}(", f),
            })
            .collect();

        // Also complete data field names
        let fields = self.data_fields.borrow();
        for field in fields.iter() {
            if field.starts_with(prefix) {
                completions.push(Pair {
                    display: field.clone(),
                    replacement: field.clone(),
                });
            }
        }

        completions.sort_by(|a, b| a.display.cmp(&b.display));

        Ok((word_start, completions))
    }
}

impl Hinter for JmespathHelper {
    type Hint = String;

    fn hint(&self, line: &str, pos: usize, _ctx: &rustyline::Context<'_>) -> Option<String> {
        if pos < line.len() {
            return None;
        }

        // Find partial function name
        let word_start = line
            .rfind(|c: char| !c.is_alphanumeric() && c != '_')
            .map(|i| i + 1)
            .unwrap_or(0);

        let prefix = &line[word_start..];

        if prefix.len() < 2 {
            return None;
        }

        // Find first matching function
        self.functions
            .iter()
            .filter(|f| f.starts_with(prefix))
            .min()
            .map(|f| {
                let suffix = &f[prefix.len()..];
                format!("{}{}({})", colors::HINT, suffix, colors::RESET)
            })
    }
}

impl Highlighter for JmespathHelper {
    fn highlight<'l>(&self, line: &'l str, _pos: usize) -> Cow<'l, str> {
        Cow::Owned(self.highlight_jmespath(line))
    }

    fn highlight_prompt<'b, 's: 'b, 'p: 'b>(
        &'s self,
        prompt: &'p str,
        _default: bool,
    ) -> Cow<'b, str> {
        Cow::Owned(format!("{}{}{}", colors::PROMPT, prompt, colors::RESET))
    }

    fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
        Cow::Borrowed(hint) // Already colored in hint()
    }

    fn highlight_char(&self, _line: &str, _pos: usize, _kind: CmdKind) -> bool {
        true
    }
}

impl Validator for JmespathHelper {}

impl Helper for JmespathHelper {}

/// Colorize JSON output
pub fn colorize_json(value: &serde_json::Value, indent: usize) -> String {
    let prefix = "  ".repeat(indent);

    match value {
        serde_json::Value::Null => {
            format!("{}null{}", colors::JSON_NULL, colors::RESET)
        }
        serde_json::Value::Bool(b) => {
            format!("{}{}{}", colors::JSON_BOOL, b, colors::RESET)
        }
        serde_json::Value::Number(n) => {
            format!("{}{}{}", colors::JSON_NUMBER, n, colors::RESET)
        }
        serde_json::Value::String(s) => {
            format!(
                "{}\"{}\"{}",
                colors::JSON_STRING,
                escape_string(s),
                colors::RESET
            )
        }
        serde_json::Value::Array(arr) => {
            if arr.is_empty() {
                "[]".to_string()
            } else if arr.len() <= 3
                && arr.iter().all(|v| {
                    matches!(
                        v,
                        serde_json::Value::Number(_)
                            | serde_json::Value::Bool(_)
                            | serde_json::Value::Null
                    )
                })
            {
                // Compact format for short simple arrays
                let items: Vec<String> = arr.iter().map(|v| colorize_json(v, 0)).collect();
                format!("[{}]", items.join(", "))
            } else {
                let items: Vec<String> = arr
                    .iter()
                    .map(|v| format!("{}  {}", prefix, colorize_json(v, indent + 1)))
                    .collect();
                format!("[\n{}\n{}]", items.join(",\n"), prefix)
            }
        }
        serde_json::Value::Object(obj) => {
            if obj.is_empty() {
                "{}".to_string()
            } else {
                let items: Vec<String> = obj
                    .iter()
                    .map(|(k, v)| {
                        format!(
                            "{}  {}\"{}\"{}: {}",
                            prefix,
                            colors::JSON_KEY,
                            k,
                            colors::RESET,
                            colorize_json(v, indent + 1)
                        )
                    })
                    .collect();
                format!("{{\n{}\n{}}}", items.join(",\n"), prefix)
            }
        }
    }
}

fn escape_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Describe a loaded value
fn describe_value(value: &Variable) -> String {
    match value {
        Variable::Null => "null".to_string(),
        Variable::Bool(_) => "boolean".to_string(),
        Variable::Number(_) => "number".to_string(),
        Variable::String(s) => format!("string ({} chars)", s.len()),
        Variable::Array(arr) => format!("array ({} items)", arr.len()),
        Variable::Object(obj) => format!("object ({} keys)", obj.len()),
        Variable::Expref(_) => "expression reference".to_string(),
    }
}

/// Check if a query line needs continuation (multiline input)
fn needs_continuation(line: &str) -> bool {
    let trimmed = line.trim();

    // Ends with pipe - definitely continues
    if trimmed.ends_with('|') {
        return true;
    }

    // Count brackets to check for unclosed structures
    let mut brackets = 0i32;
    let mut parens = 0i32;
    let mut braces = 0i32;
    let mut in_string = false;
    let mut in_literal = false;
    let mut prev_char = ' ';

    for c in trimmed.chars() {
        match c {
            '\'' if !in_literal && prev_char != '\\' => in_string = !in_string,
            '`' if !in_string && prev_char != '\\' => in_literal = !in_literal,
            '[' if !in_string && !in_literal => brackets += 1,
            ']' if !in_string && !in_literal => brackets -= 1,
            '(' if !in_string && !in_literal => parens += 1,
            ')' if !in_string && !in_literal => parens -= 1,
            '{' if !in_string && !in_literal => braces += 1,
            '}' if !in_string && !in_literal => braces -= 1,
            _ => {}
        }
        prev_char = c;
    }

    // Unclosed brackets, parens, or braces
    brackets > 0 || parens > 0 || braces > 0 || in_string || in_literal
}

/// A suggested query with description
pub struct Suggestion {
    pub query: String,
    pub description: String,
    pub level: QueryLevel,
}

/// Analyze JSON structure and suggest relevant queries
pub fn suggest_queries(var: &Variable) -> Vec<Suggestion> {
    let mut suggestions = Vec::new();

    match var {
        Variable::Object(obj) => {
            suggest_for_object(obj, "", &mut suggestions);
            suggest_advanced_object(obj, &mut suggestions);
        }
        Variable::Array(arr) => {
            suggest_for_array(arr, "", &mut suggestions);
            suggest_advanced_array(arr, &mut suggestions);
        }
        _ => {
            // Primitives - not much to suggest
            suggestions.push(Suggestion {
                query: "@".to_string(),
                description: "Current value".to_string(),
                level: 3,
            });
        }
    }

    // Deduplicate and limit suggestions
    let mut seen = HashSet::new();
    suggestions.retain(|s| seen.insert(s.query.clone()));
    suggestions.truncate(20); // Allow more for advanced suggestions

    suggestions
}

fn suggest_for_object(
    obj: &std::collections::BTreeMap<String, Rc<Variable>>,
    prefix: &str,
    suggestions: &mut Vec<Suggestion>,
) {
    let keys: Vec<_> = obj.keys().collect();

    // Basic key access
    if prefix.is_empty() {
        suggestions.push(Suggestion {
            query: "keys(@)".to_string(),
            description: format!("List all {} keys", keys.len()),
            level: 3,
        });
        suggestions.push(Suggestion {
            query: "values(@)".to_string(),
            description: "Get all values".to_string(),
            level: 3,
        });
    }

    // Suggest accessing each field
    for key in &keys {
        let field_path = if prefix.is_empty() {
            (*key).clone()
        } else {
            format!("{}.{}", prefix, key)
        };

        if let Some(value) = obj.get(*key) {
            match value.as_ref() {
                Variable::Array(arr) => {
                    suggestions.push(Suggestion {
                        query: format!("{}[*]", field_path),
                        description: format!("All {} items in {}", arr.len(), key),
                        level: 3,
                    });

                    // Check if array of objects
                    if let Some(first) = arr.first() {
                        if let Variable::Object(inner_obj) = first.as_ref() {
                            let inner_keys: Vec<_> = inner_obj.keys().take(3).collect();
                            if !inner_keys.is_empty() {
                                suggestions.push(Suggestion {
                                    query: format!("{}[*].{}", field_path, inner_keys[0]),
                                    description: format!("Get {} from each item", inner_keys[0]),
                                    level: 3,
                                });
                            }

                            // Suggest filtering
                            for (inner_key, inner_val) in inner_obj.iter().take(2) {
                                match inner_val.as_ref() {
                                    Variable::Bool(_) => {
                                        suggestions.push(Suggestion {
                                            query: format!("{}[?{}]", field_path, inner_key),
                                            description: format!(
                                                "Filter where {} is true",
                                                inner_key
                                            ),
                                            level: 3,
                                        });
                                    }
                                    Variable::Number(_) => {
                                        suggestions.push(Suggestion {
                                            query: format!("{}[?{} > `0`]", field_path, inner_key),
                                            description: format!(
                                                "Filter by {} comparison",
                                                inner_key
                                            ),
                                            level: 3,
                                        });
                                        suggestions.push(Suggestion {
                                            query: format!("sum({}[*].{})", field_path, inner_key),
                                            description: format!("Sum of {}", inner_key),
                                            level: 3,
                                        });
                                        suggestions.push(Suggestion {
                                            query: format!("avg({}[*].{})", field_path, inner_key),
                                            description: format!("Average of {}", inner_key),
                                            level: 3,
                                        });
                                    }
                                    Variable::String(s) => {
                                        // Check for date-like strings
                                        if looks_like_date(s) {
                                            suggestions.push(Suggestion {
                                                query: format!(
                                                    "{}[*].{{item: @, formatted: format_date({}, '%Y-%m-%d')}}",
                                                    field_path, inner_key
                                                ),
                                                description: format!(
                                                    "Format {} dates",
                                                    inner_key
                                                ),
                                                level: 3,
                                            });
                                        } else {
                                            suggestions.push(Suggestion {
                                                query: format!(
                                                    "{}[?{} == '{}']",
                                                    field_path,
                                                    inner_key,
                                                    s.chars().take(10).collect::<String>()
                                                ),
                                                description: format!("Filter by {}", inner_key),
                                                level: 3,
                                            });
                                        }
                                        suggestions.push(Suggestion {
                                            query: format!(
                                                "group_by_expr('{}', {})",
                                                inner_key, field_path
                                            ),
                                            description: format!("Group by {}", inner_key),
                                            level: 3,
                                        });
                                    }
                                    _ => {}
                                }
                            }

                            // Sorting
                            if let Some((sort_key, _)) = inner_obj.iter().find(|(_, v)| {
                                matches!(v.as_ref(), Variable::Number(_) | Variable::String(_))
                            }) {
                                suggestions.push(Suggestion {
                                    query: format!(
                                        "sort_by({}, &{}) | [].{}",
                                        field_path,
                                        sort_key,
                                        inner_keys.first().unwrap_or(&sort_key)
                                    ),
                                    description: format!("Sort by {}", sort_key),
                                    level: 3,
                                });
                            }
                        }
                    }

                    // Numeric array operations
                    if arr
                        .first()
                        .is_some_and(|v| matches!(v.as_ref(), Variable::Number(_)))
                    {
                        suggestions.push(Suggestion {
                            query: format!("sum({})", field_path),
                            description: format!("Sum of {}", key),
                            level: 3,
                        });
                        suggestions.push(Suggestion {
                            query: format!(
                                "{{min: min({}), max: max({}), avg: avg({})}}",
                                field_path, field_path, field_path
                            ),
                            description: format!("Stats for {}", key),
                            level: 3,
                        });
                    }
                }
                Variable::Object(inner) => {
                    suggestions.push(Suggestion {
                        query: format!("{} | keys(@)", field_path),
                        description: format!("Keys in {}", key),
                        level: 3,
                    });
                    // Recurse one level
                    if prefix.is_empty() {
                        suggest_for_object(inner, &field_path, suggestions);
                    }
                }
                Variable::Number(_) => {
                    suggestions.push(Suggestion {
                        query: field_path.clone(),
                        description: format!("Get {} (number)", key),
                        level: 3,
                    });
                }
                Variable::String(s) => {
                    if looks_like_date(s) {
                        suggestions.push(Suggestion {
                            query: format!("format_date({}, '%B %d, %Y')", field_path),
                            description: format!("Format {} as date", key),
                            level: 3,
                        });
                    } else if looks_like_url(s) {
                        suggestions.push(Suggestion {
                            query: format!("parse_url({})", field_path),
                            description: format!("Parse {} as URL", key),
                            level: 3,
                        });
                    }
                }
                _ => {}
            }
        }
    }
}

fn suggest_for_array(arr: &[Rc<Variable>], prefix: &str, suggestions: &mut Vec<Suggestion>) {
    let path = if prefix.is_empty() {
        "@".to_string()
    } else {
        prefix.to_string()
    };

    suggestions.push(Suggestion {
        query: format!("length({})", path),
        description: format!("Count items ({})", arr.len()),
        level: 3,
    });

    if arr.is_empty() {
        return;
    }

    let first = arr.first().unwrap();

    match first.as_ref() {
        Variable::Object(obj) => {
            let keys: Vec<_> = obj.keys().collect();

            // Project specific fields
            if !keys.is_empty() {
                suggestions.push(Suggestion {
                    query: format!("[*].{}", keys[0]),
                    description: format!("Get {} from each item", keys[0]),
                    level: 3,
                });

                if keys.len() >= 2 {
                    suggestions.push(Suggestion {
                        query: format!("[*].{{{}:{}, {}:{}}}", keys[0], keys[0], keys[1], keys[1]),
                        description: "Select specific fields".to_string(),
                        level: 3,
                    });
                }

                suggestions.push(Suggestion {
                    query: "[0]".to_string(),
                    description: "First item".to_string(),
                    level: 3,
                });

                suggestions.push(Suggestion {
                    query: "[-1]".to_string(),
                    description: "Last item".to_string(),
                    level: 3,
                });

                suggestions.push(Suggestion {
                    query: "[*] | unique_by(@, &".to_string() + keys[0] + ")",
                    description: format!("Unique by {}", keys[0]),
                    level: 3,
                });
            }

            // Analyze field types for smarter suggestions
            for (key, val) in obj.iter() {
                match val.as_ref() {
                    Variable::Bool(_) => {
                        suggestions.push(Suggestion {
                            query: format!("[?{}]", key),
                            description: format!("Filter where {} is true", key),
                            level: 3,
                        });
                        suggestions.push(Suggestion {
                            query: format!("[?!{}]", key),
                            description: format!("Filter where {} is false", key),
                            level: 3,
                        });
                    }
                    Variable::Number(_) => {
                        suggestions.push(Suggestion {
                            query: format!("max_by(@, &{}).{}", key, keys.first().unwrap_or(&key)),
                            description: format!("Item with highest {}", key),
                            level: 3,
                        });
                        suggestions.push(Suggestion {
                            query: format!("min_by(@, &{}).{}", key, keys.first().unwrap_or(&key)),
                            description: format!("Item with lowest {}", key),
                            level: 3,
                        });
                    }
                    Variable::String(_) => {
                        suggestions.push(Suggestion {
                            query: format!("[*].{} | unique(@)", key),
                            description: format!("Unique {} values", key),
                            level: 3,
                        });
                    }
                    _ => {}
                }
            }
        }
        Variable::Number(_) => {
            suggestions.push(Suggestion {
                query: "sum(@)".to_string(),
                description: "Sum all values".to_string(),
                level: 3,
            });
            suggestions.push(Suggestion {
                query: "{min: min(@), max: max(@), avg: avg(@)}".to_string(),
                description: "Statistics".to_string(),
                level: 3,
            });
            suggestions.push(Suggestion {
                query: "sort(@)".to_string(),
                description: "Sort ascending".to_string(),
                level: 3,
            });
        }
        Variable::String(_) => {
            suggestions.push(Suggestion {
                query: "unique(@)".to_string(),
                description: "Unique values".to_string(),
                level: 3,
            });
            suggestions.push(Suggestion {
                query: "sort(@)".to_string(),
                description: "Sort alphabetically".to_string(),
                level: 3,
            });
            suggestions.push(Suggestion {
                query: "[*] | [?contains(@, 'search')]".to_string(),
                description: "Search for text".to_string(),
                level: 3,
            });
        }
        _ => {}
    }
}

/// Check if a string looks like a date
fn looks_like_date(s: &str) -> bool {
    // Common date patterns
    s.len() >= 8
        && s.len() <= 30
        && (s.contains('-') || s.contains('/'))
        && s.chars().filter(|c| c.is_ascii_digit()).count() >= 4
}

/// Check if a string looks like a URL
fn looks_like_url(s: &str) -> bool {
    s.starts_with("http://") || s.starts_with("https://")
}

/// Advanced suggestions for objects with arrays
fn suggest_advanced_object(
    obj: &std::collections::BTreeMap<String, Rc<Variable>>,
    suggestions: &mut Vec<Suggestion>,
) {
    // Find arrays of objects for advanced patterns
    let array_fields: Vec<_> = obj
        .iter()
        .filter_map(|(k, v)| {
            if let Variable::Array(arr) = v.as_ref() {
                if arr
                    .first()
                    .is_some_and(|f| matches!(f.as_ref(), Variable::Object(_)))
                {
                    return Some((k.clone(), arr));
                }
            }
            None
        })
        .collect();

    for (field_name, arr) in &array_fields {
        if let Some(first) = arr.first() {
            if let Variable::Object(inner) = first.as_ref() {
                let keys: Vec<_> = inner.keys().collect();

                // Find categorical (string) and numeric fields
                let string_fields: Vec<_> = inner
                    .iter()
                    .filter(|(_, v)| matches!(v.as_ref(), Variable::String(_)))
                    .map(|(k, _)| k.clone())
                    .collect();

                let numeric_fields: Vec<_> = inner
                    .iter()
                    .filter(|(_, v)| matches!(v.as_ref(), Variable::Number(_)))
                    .map(|(k, _)| k.clone())
                    .collect();

                // Aggregation: count by category
                if let Some(cat_field) = string_fields.first() {
                    suggestions.push(Suggestion {
                        query: format!(
                            "group_by_expr('{}', {}) | map_values('length(@)', @)",
                            cat_field, field_name
                        ),
                        description: format!("Count by {}", cat_field),
                        level: 3,
                    });

                    // If we have a numeric field too, sum by category
                    if let Some(num_field) = numeric_fields.first() {
                        suggestions.push(Suggestion {
                            query: format!(
                                "group_by_expr('{}', {}) | map_values('sum([*].{})', @)",
                                cat_field, field_name, num_field
                            ),
                            description: format!("Sum {} by {}", num_field, cat_field),
                            level: 3,
                        });
                    }
                }

                // Pipeline: filter → transform → sort
                if keys.len() >= 2 {
                    let id_field = keys
                        .iter()
                        .find(|k| k.contains("id") || k.contains("name") || k.contains("title"))
                        .unwrap_or(&keys[0]);

                    if let Some(num_field) = numeric_fields.first() {
                        suggestions.push(Suggestion {
                        query: format!(
                            "{}[?{} > `0`] | [*].{{{}: {}, {}: {}}} | sort_by(@, &{}) | reverse(@)",
                            field_name,
                            num_field,
                            id_field,
                            id_field,
                            num_field,
                            num_field,
                            num_field
                        ),
                        description: format!("Top items by {}", num_field),
                        level: 3,
                    });
                    }
                }

                // Nested array flattening
                for (key, val) in inner.iter() {
                    if let Variable::Array(nested) = val.as_ref() {
                        if !nested.is_empty() {
                            suggestions.push(Suggestion {
                                query: format!(
                                    "{}[].{}[] | flatten(@) | unique(@)",
                                    field_name, key
                                ),
                                description: format!("Flatten and unique {}", key),
                                level: 3,
                            });
                        }
                    }
                }

                // Multi-field statistics
                if numeric_fields.len() >= 2 {
                    let stats: Vec<_> = numeric_fields
                        .iter()
                        .take(3)
                        .map(|f| format!("{}_avg: avg({}[*].{})", f, field_name, f))
                        .collect();
                    suggestions.push(Suggestion {
                        query: format!("{{{}}}", stats.join(", ")),
                        description: "Multi-field averages".to_string(),
                        level: 3,
                    });
                }

                // Transform with map_expr
                if !keys.is_empty() {
                    suggestions.push(Suggestion {
                        query: format!(
                            "map_expr({}, &{{original: @, computed: length(to_string(@))}})",
                            field_name
                        ),
                        description: "Transform each item with map_expr".to_string(),
                        level: 3,
                    });
                }
            }
        }
    }

    // Check for multiple arrays that might be joinable
    if array_fields.len() >= 2 {
        let names: Vec<_> = array_fields.iter().map(|(k, _)| k.as_str()).collect();
        suggestions.push(Suggestion {
            query: format!(
                "{{{}}} | to_entries(@)",
                names
                    .iter()
                    .map(|n| format!("{}: length({})", n, n))
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            description: "Compare array sizes".to_string(),
            level: 3,
        });
    }
}

/// Advanced suggestions for top-level arrays
fn suggest_advanced_array(arr: &[Rc<Variable>], suggestions: &mut Vec<Suggestion>) {
    if arr.is_empty() {
        return;
    }

    let first = arr.first().unwrap();

    if let Variable::Object(obj) = first.as_ref() {
        let keys: Vec<_> = obj.keys().collect();

        // Find field types
        let string_fields: Vec<_> = obj
            .iter()
            .filter(|(_, v)| matches!(v.as_ref(), Variable::String(_)))
            .map(|(k, _)| k.clone())
            .collect();

        let numeric_fields: Vec<_> = obj
            .iter()
            .filter(|(_, v)| matches!(v.as_ref(), Variable::Number(_)))
            .map(|(k, _)| k.clone())
            .collect();

        let bool_fields: Vec<_> = obj
            .iter()
            .filter(|(_, v)| matches!(v.as_ref(), Variable::Bool(_)))
            .map(|(k, _)| k.clone())
            .collect();

        // Aggregation with reduce_expr
        if let Some(num_field) = numeric_fields.first() {
            suggestions.push(Suggestion {
                query: format!("reduce_expr(@, &add(acc, @.{}), `0`)", num_field),
                description: format!("Running total of {} with reduce", num_field),
                level: 3,
            });
        }

        // Conditional aggregation
        if let (Some(bool_field), Some(num_field)) = (bool_fields.first(), numeric_fields.first()) {
            suggestions.push(Suggestion {
                query: format!(
                    "{{when_true: sum([?{}].{}), when_false: sum([?!{}].{})}}",
                    bool_field, num_field, bool_field, num_field
                ),
                description: format!("Sum {} split by {}", num_field, bool_field),
                level: 3,
            });
        }

        // Pivot-like: group and extract
        if let (Some(cat_field), Some(val_field)) = (string_fields.first(), numeric_fields.first())
        {
            suggestions.push(Suggestion {
                query: format!(
                    "group_by_expr('{}', @) | to_entries(@) | [*].{{category: key, total: sum(value[*].{}), count: length(value)}}",
                    cat_field, val_field
                ),
                description: format!("Pivot: aggregate {} by {}", val_field, cat_field),
                level: 3,
            });
        }

        // Top N pattern
        if let Some(num_field) = numeric_fields.first() {
            let display_field = keys
                .iter()
                .find(|k| k.contains("name") || k.contains("id") || k.contains("title"))
                .unwrap_or(&keys[0]);
            suggestions.push(Suggestion {
                query: format!(
                    "sort_by(@, &{}) | reverse(@) | [:3] | [*].{{{}: {}, {}: {}}}",
                    num_field, display_field, display_field, num_field, num_field
                ),
                description: format!("Top 3 by {}", num_field),
                level: 3,
            });
        }

        // Scan for running calculations
        if let Some(num_field) = numeric_fields.first() {
            suggestions.push(Suggestion {
                query: format!("scan_expr(@, &add(acc, @.{}), `0`)", num_field),
                description: format!("Running sum of {}", num_field),
                level: 3,
            });
        }

        // Complex filter with multiple conditions
        if !bool_fields.is_empty() && !numeric_fields.is_empty() {
            suggestions.push(Suggestion {
                query: format!(
                    "[?{} && {} > `0`] | length(@)",
                    bool_fields[0], numeric_fields[0]
                ),
                description: format!(
                    "Count where {} and {} > 0",
                    bool_fields[0], numeric_fields[0]
                ),
                level: 4,
            });
        }

        // Nested object exploration
        for (key, val) in obj.iter() {
            if let Variable::Object(nested) = val.as_ref() {
                let nested_keys: Vec<_> = nested.keys().take(2).collect();
                if !nested_keys.is_empty() {
                    suggestions.push(Suggestion {
                        query: format!("[*].{} | [*].{}", key, nested_keys[0]),
                        description: format!("Extract nested {}.{}", key, nested_keys[0]),
                        level: 3,
                    });
                }
            }
        }
    }
}

/// Validate a query against the current data, returning true if it executes without error
fn validate_query(query: &str, var: &Variable, runtime: &Runtime) -> bool {
    match runtime.compile(query) {
        Ok(expr) => expr.search(var).is_ok(),
        Err(_) => false,
    }
}

/// Print a list of queries with validation, filtering out any that fail
/// Groups queries by level and shows level indicators
fn print_validated_queries(
    queries: &[QueryExample],
    var: &Variable,
    runtime: &Runtime,
    header: &str,
    max_level: Option<QueryLevel>,
) {
    let max_lvl = max_level.unwrap_or(5);

    let valid_queries: Vec<_> = queries
        .iter()
        .filter(|(query, _, level)| *level <= max_lvl && validate_query(query, var, runtime))
        .collect();

    if valid_queries.is_empty() {
        return;
    }

    println!("{}{}{}", colors::INFO, header, colors::RESET);

    // Group by level
    for level in 1..=max_lvl {
        let level_queries: Vec<_> = valid_queries
            .iter()
            .filter(|(_, _, l)| *l == level)
            .collect();

        if level_queries.is_empty() {
            continue;
        }

        let level_label = match level {
            1 => "Basic",
            2 => "Filters & Functions",
            3 => "Aggregations",
            4 => "Pipelines",
            5 => "Advanced",
            _ => "Other",
        };

        println!(
            "  {}[L{}] {}:{}",
            colors::HINT,
            level,
            level_label,
            colors::RESET
        );
        for (query, desc, _) in level_queries {
            println!("    {}# {}{}", colors::HINT, desc, colors::RESET);
            println!("    {}", query);
        }
    }
}

/// Print suggestions for the current data
pub fn print_suggestions(var: &Variable, runtime: &Runtime) {
    let suggestions = suggest_queries(var);

    if suggestions.is_empty() {
        println!(
            "{}No suggestions for this data shape{}",
            colors::INFO,
            colors::RESET
        );
        return;
    }

    // Validate and filter suggestions
    let valid_suggestions: Vec<_> = suggestions
        .into_iter()
        .filter(|s| validate_query(&s.query, var, runtime))
        .collect();

    if valid_suggestions.is_empty() {
        println!(
            "{}No valid suggestions for this data shape{}",
            colors::INFO,
            colors::RESET
        );
        return;
    }

    println!("{}Suggested queries:{}", colors::INFO, colors::RESET);

    // Group by level
    for level in 1..=5u8 {
        let level_queries: Vec<_> = valid_suggestions
            .iter()
            .filter(|s| s.level == level)
            .collect();

        if level_queries.is_empty() {
            continue;
        }

        let level_label = match level {
            1 => "Basic",
            2 => "Filters & Functions",
            3 => "Aggregations",
            4 => "Pipelines",
            5 => "Advanced",
            _ => "Other",
        };

        println!(
            "  {}[L{}] {}:{}",
            colors::HINT,
            level,
            level_label,
            colors::RESET
        );
        for s in level_queries {
            println!("    {}# {}{}", colors::HINT, s.description, colors::RESET);
            println!("    {}", s.query);
        }
    }
}

/// Extract top-level field names from a Variable for completion
fn extract_fields(var: &Variable) -> Vec<String> {
    match var {
        Variable::Object(obj) => obj.keys().map(|k| k.to_string()).collect(),
        Variable::Array(arr) => {
            // For arrays, get fields from first object element if any
            arr.iter()
                .find_map(|v| {
                    if let Variable::Object(obj) = v.as_ref() {
                        Some(obj.keys().map(|k| k.to_string()).collect())
                    } else {
                        None
                    }
                })
                .unwrap_or_default()
        }
        _ => vec![],
    }
}

/// Run the REPL
pub fn run(demo_name: Option<&str>) -> Result<()> {
    // Shared state for data field completion
    let data_fields: Rc<RefCell<Vec<String>>> = Rc::new(RefCell::new(vec![]));

    let helper = JmespathHelper::new(Rc::clone(&data_fields));
    let mut rl: Editor<JmespathHelper, DefaultHistory> = Editor::new()?;
    rl.set_helper(Some(helper));

    // Try to load history
    let history_path = dirs::data_local_dir().map(|p| p.join("jpx").join("history.txt"));

    if let Some(ref path) = history_path {
        let _ = std::fs::create_dir_all(path.parent().unwrap());
        let _ = rl.load_history(path);
    }

    // Create runtime
    let mut runtime = Runtime::new();
    runtime.register_builtin_functions();
    register_all(&mut runtime);

    // Create registry for introspection
    let mut registry = FunctionRegistry::new();
    registry.register_all();

    // Current data
    let mut data: Option<Variable> = None;

    // Print banner
    println!(
        "{}{}jpx{} - JMESPath Extended REPL",
        colors::BOLD,
        colors::PROMPT,
        colors::RESET
    );
    println!(
        "{}Type .help for commands, .exit to quit{}\n",
        colors::INFO,
        colors::RESET
    );

    // Load demo if specified
    if let Some(name) = demo_name {
        if let Some(demo) = DEMOS.iter().find(|d| d.name == name) {
            let value = Variable::from_json(demo.data).unwrap();
            *data_fields.borrow_mut() = extract_fields(&value);
            data = Some(value);
            println!(
                "{}Loaded demo:{} {} - {}",
                colors::SUCCESS,
                colors::RESET,
                demo.name,
                demo.description
            );
            println!(
                "{}Data:{} {} (access via `{}`)\n",
                colors::INFO,
                colors::RESET,
                describe_value(data.as_ref().unwrap()),
                demo.root_key
            );
            print_validated_queries(
                demo.queries,
                data.as_ref().unwrap(),
                &runtime,
                "Try these queries:",
                Some(2), // Show basic queries on initial load
            );
            println!();
        } else {
            println!(
                "{}Unknown demo '{}'. Available: {}{}",
                colors::ERROR,
                name,
                DEMOS.iter().map(|d| d.name).collect::<Vec<_>>().join(", "),
                colors::RESET
            );
        }
    }

    loop {
        let prompt = if data.is_some() {
            "jpx> "
        } else {
            "jpx (no data)> "
        };

        match rl.readline(prompt) {
            Ok(line) => {
                let line = line.trim();

                if line.is_empty() {
                    continue;
                }

                // Handle commands
                if line.starts_with('.') {
                    let _ = rl.add_history_entry(line);
                    if let Err(e) =
                        handle_command(line, &mut data, &registry, &runtime, &mut rl, &data_fields)
                    {
                        println!("{}Error: {}{}", colors::ERROR, e, colors::RESET);
                    }
                    continue;
                }

                // Check for multiline query (ends with | or has unclosed brackets)
                let full_query = if needs_continuation(line) {
                    let mut lines = vec![line.to_string()];
                    loop {
                        match rl.readline("... ") {
                            Ok(cont) => {
                                let cont = cont.trim();
                                if cont.is_empty() {
                                    break;
                                }
                                lines.push(cont.to_string());
                                let combined = lines.join(" ");
                                if !needs_continuation(&combined) {
                                    break;
                                }
                            }
                            Err(ReadlineError::Interrupted) => {
                                println!("{}Cancelled{}", colors::INFO, colors::RESET);
                                lines.clear();
                                break;
                            }
                            Err(_) => break,
                        }
                    }
                    if lines.is_empty() {
                        continue;
                    }
                    lines.join(" ")
                } else {
                    line.to_string()
                };

                let _ = rl.add_history_entry(&full_query);

                // Execute JMESPath expression
                if let Some(ref d) = data {
                    match runtime.compile(&full_query) {
                        Ok(expr) => match expr.search(d) {
                            Ok(result) => {
                                if !result.is_null() {
                                    let json_value: serde_json::Value =
                                        serde_json::to_value(&*result).unwrap();
                                    println!("{}", colorize_json(&json_value, 0));
                                } else {
                                    println!("{}null{}", colors::JSON_NULL, colors::RESET);
                                }
                            }
                            Err(e) => {
                                println!("{}Runtime error: {}{}", colors::ERROR, e, colors::RESET);
                            }
                        },
                        Err(e) => {
                            println!("{}Parse error: {}{}", colors::ERROR, e, colors::RESET);
                        }
                    }
                } else {
                    println!(
                        "{}No data loaded. Use .load <file> or .demo <name>{}",
                        colors::ERROR,
                        colors::RESET
                    );
                }
            }
            Err(ReadlineError::Interrupted) => {
                println!("{}Use .exit to quit{}", colors::INFO, colors::RESET);
            }
            Err(ReadlineError::Eof) => {
                break;
            }
            Err(err) => {
                println!("{}Error: {}{}", colors::ERROR, err, colors::RESET);
                break;
            }
        }
    }

    // Save history
    if let Some(ref path) = history_path {
        let _ = rl.save_history(path);
    }

    println!("Goodbye!");
    Ok(())
}

fn handle_command(
    line: &str,
    data: &mut Option<Variable>,
    registry: &FunctionRegistry,
    runtime: &Runtime,
    rl: &mut Editor<JmespathHelper, DefaultHistory>,
    data_fields: &Rc<RefCell<Vec<String>>>,
) -> Result<()> {
    let parts: Vec<&str> = line.splitn(2, ' ').collect();
    let cmd = parts[0];
    let arg = parts.get(1).map(|s| s.trim());

    match cmd {
        ".exit" | ".quit" | ".q" => {
            std::process::exit(0);
        }

        ".help" | ".h" | ".?" => {
            println!("{}Commands:{}", colors::BOLD, colors::RESET);
            println!(
                "  {}.load <file>{}     Load JSON from file",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.json [json]{}     Load JSON (inline or multiline mode)",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.data{}            Show current data",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.demo [name]{}     Load demo dataset (users, geo, text, datetime, ecommerce)",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.demos{}           List available demos",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.suggest{}         Suggest queries for current data",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.functions{}       List all functions",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.describe <fn>{}   Describe a function",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.clear{}           Clear screen",
                colors::FUNCTION,
                colors::RESET
            );
            println!(
                "  {}.exit{}            Exit REPL",
                colors::FUNCTION,
                colors::RESET
            );
            println!();
            println!("{}Tips:{}", colors::BOLD, colors::RESET);
            println!("  - Tab completion for function names");
            println!("  - Up/Down arrows for history");
            println!("  - Ctrl+R to search history");
        }

        ".load" => {
            let path = arg.ok_or_else(|| anyhow::anyhow!("Usage: .load <file>"))?;
            let content = std::fs::read_to_string(path)
                .with_context(|| format!("Failed to read file: {}", path))?;
            let value = Variable::from_json(&content)
                .map_err(|e| anyhow::anyhow!("Invalid JSON: {}", e))?;
            println!(
                "{}Loaded:{} {}",
                colors::SUCCESS,
                colors::RESET,
                describe_value(&value)
            );
            *data_fields.borrow_mut() = extract_fields(&value);
            *data = Some(value);
        }

        ".json" => {
            let json_str = if let Some(inline) = arg {
                // Inline JSON provided
                inline.to_string()
            } else {
                // Multiline input mode
                println!(
                    "{}Enter JSON (empty line to finish, Ctrl+C to cancel):{}",
                    colors::INFO,
                    colors::RESET
                );
                let mut lines = Vec::new();
                loop {
                    match rl.readline("... ") {
                        Ok(line) => {
                            if line.trim().is_empty() && !lines.is_empty() {
                                // Check if we have valid JSON so far
                                let current = lines.join("\n");
                                if serde_json::from_str::<serde_json::Value>(&current).is_ok() {
                                    break;
                                }
                            }
                            lines.push(line);

                            // Try to parse - if valid, we're done
                            let current = lines.join("\n");
                            if serde_json::from_str::<serde_json::Value>(&current).is_ok() {
                                break;
                            }
                        }
                        Err(rustyline::error::ReadlineError::Interrupted) => {
                            println!("{}Cancelled{}", colors::INFO, colors::RESET);
                            return Ok(());
                        }
                        Err(e) => {
                            return Err(anyhow::anyhow!("Read error: {}", e));
                        }
                    }
                }
                lines.join("\n")
            };

            let value = Variable::from_json(&json_str)
                .map_err(|e| anyhow::anyhow!("Invalid JSON: {}", e))?;
            println!(
                "{}Loaded:{} {}",
                colors::SUCCESS,
                colors::RESET,
                describe_value(&value)
            );
            *data_fields.borrow_mut() = extract_fields(&value);
            *data = Some(value);
        }

        ".data" => {
            if let Some(d) = data {
                let json_value: serde_json::Value = serde_json::to_value(&*d).unwrap();
                println!("{}", colorize_json(&json_value, 0));
            } else {
                println!("{}No data loaded{}", colors::INFO, colors::RESET);
            }
        }

        ".demo" => {
            let name = arg.unwrap_or("users");
            if let Some(demo) = DEMOS.iter().find(|d| d.name == name) {
                let value = Variable::from_json(demo.data).unwrap();
                *data_fields.borrow_mut() = extract_fields(&value);
                *data = Some(value);
                println!(
                    "{}Loaded demo:{} {} - {}",
                    colors::SUCCESS,
                    colors::RESET,
                    demo.name,
                    demo.description
                );
                println!(
                    "{}Data:{} {} (access via `{}`)\n",
                    colors::INFO,
                    colors::RESET,
                    describe_value(data.as_ref().unwrap()),
                    demo.root_key
                );
                print_validated_queries(
                    demo.queries,
                    data.as_ref().unwrap(),
                    runtime,
                    "Try these queries:",
                    Some(2), // Show basic queries on initial load
                );
            } else {
                println!(
                    "{}Unknown demo '{}'. Available: {}{}",
                    colors::ERROR,
                    name,
                    DEMOS.iter().map(|d| d.name).collect::<Vec<_>>().join(", "),
                    colors::RESET
                );
            }
        }

        ".demos" => {
            println!("{}Available demos:{}", colors::BOLD, colors::RESET);
            for demo in DEMOS {
                println!(
                    "  {}{:<12}{} - {}",
                    colors::FUNCTION,
                    demo.name,
                    colors::RESET,
                    demo.description
                );
            }
            println!(
                "\nUse {}.demo <name>{} to load",
                colors::FUNCTION,
                colors::RESET
            );
        }

        ".suggest" | ".s" => {
            if let Some(d) = &data {
                print_suggestions(d, runtime);
            } else {
                println!(
                    "{}No data loaded. Use .load <file> or .demo <name>{}",
                    colors::ERROR,
                    colors::RESET
                );
            }
        }

        ".functions" | ".funcs" => {
            // Group by category
            for category in Category::all() {
                if !category.is_available() {
                    continue;
                }

                let funcs: Vec<_> = registry.functions_in_category(*category).collect();
                if funcs.is_empty() {
                    continue;
                }

                let names: Vec<_> = funcs.iter().map(|f| f.name).collect();
                println!(
                    "{}{}{}: {}",
                    colors::BOLD,
                    category.name().to_uppercase(),
                    colors::RESET,
                    names.join(", ")
                );
            }
        }

        ".describe" | ".desc" => {
            let name = arg.ok_or_else(|| anyhow::anyhow!("Usage: .describe <function>"))?;
            if let Some(func) = registry.get_function(name) {
                println!("{}{}{}", colors::BOLD, func.name, colors::RESET);
                println!("  Category:    {}", func.category.name());
                println!("  Description: {}", func.description);
                println!("  Signature:   {}", func.signature);
                println!("  Example:     {}", func.example);
                if let Some(jep) = func.jep {
                    println!("  JEP:         {}", jep);
                }
            } else {
                println!(
                    "{}Unknown function '{}'{}",
                    colors::ERROR,
                    name,
                    colors::RESET
                );
            }
        }

        ".clear" | ".cls" => {
            print!("\x1b[2J\x1b[H");
        }

        _ => {
            println!(
                "{}Unknown command '{}'. Type .help for commands{}",
                colors::ERROR,
                cmd,
                colors::RESET
            );
        }
    }

    Ok(())
}