kelora 1.5.0

A command-line log analysis tool with embedded Rhai scripting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
#![allow(dead_code)] // Debugging/tracing scaffolding kept for verbose dev builds and future CLI toggles
use anyhow::Result;
use indexmap::IndexMap;
use rhai::{BinaryExpr, Dynamic, Engine, EvalAltResult, Expr, FnCallExpr, Scope, Stmt, Token, AST};
use std::collections::HashMap;

use rhai::debugger::{DebuggerCommand, DebuggerEvent};

use crate::event::Event;
use crate::rhai_functions;
use crate::rhai_functions::datetime::DateTimeWrapper;

/// Truncate text for display, respecting UTF-8 character boundaries
fn truncate_for_display(text: &str, max_len: usize) -> String {
    if text.chars().count() > max_len {
        let truncated: String = text.chars().take(max_len.saturating_sub(3)).collect();
        format!("{}...", truncated)
    } else {
        text.to_string()
    }
}

use rhai::Map;

// Temporary debug types until module structure is fixed
#[derive(Debug, Clone)]
pub struct DebugConfig {
    pub verbosity: u8,
    pub show_timing: bool,
    pub trace_events: bool,
    pub use_emoji: bool,
}

impl DebugConfig {
    pub fn new(verbose_count: u8) -> Self {
        DebugConfig {
            verbosity: verbose_count,
            show_timing: verbose_count >= 1,
            trace_events: verbose_count >= 2,
            use_emoji: true, // Default to true, will be overridden
        }
    }

    pub fn with_emoji(mut self, use_emoji: bool) -> Self {
        self.use_emoji = use_emoji;
        self
    }

    pub fn is_enabled(&self) -> bool {
        self.verbosity > 0
    }
}

#[derive(Debug, Clone, Default)]
pub struct ExecutionContext {
    pub position: Option<rhai::Position>,
    pub source_snippet: Option<String>,
    pub last_operation: Option<String>,
    pub error_location: Option<String>,
}

#[derive(Debug)]
pub struct ConfMutationError;

impl std::fmt::Display for ConfMutationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "conf map is read-only outside --begin; modifications are not allowed"
        )
    }
}

impl std::error::Error for ConfMutationError {}

fn dynamics_equal(lhs: &Dynamic, rhs: &Dynamic) -> bool {
    if lhs.type_name() != rhs.type_name() {
        return false;
    }

    // Primitive comparisons
    if let (Some(l), Some(r)) = (lhs.as_int().ok(), rhs.as_int().ok()) {
        return l == r;
    }
    if let (Some(l), Some(r)) = (lhs.as_float().ok(), rhs.as_float().ok()) {
        return l == r;
    }
    if let (Some(l), Some(r)) = (lhs.as_bool().ok(), rhs.as_bool().ok()) {
        return l == r;
    }
    if let (Ok(l), Ok(r)) = (lhs.clone().into_string(), rhs.clone().into_string()) {
        return l == r;
    }

    // Array comparison
    if let (Some(l_arr), Some(r_arr)) = (
        lhs.clone().try_cast::<rhai::Array>(),
        rhs.clone().try_cast::<rhai::Array>(),
    ) {
        if l_arr.len() != r_arr.len() {
            return false;
        }
        return l_arr
            .iter()
            .zip(r_arr.iter())
            .all(|(l, r)| dynamics_equal(l, r));
    }

    // Map comparison
    if let (Some(l_map), Some(r_map)) = (
        lhs.clone().try_cast::<rhai::Map>(),
        rhs.clone().try_cast::<rhai::Map>(),
    ) {
        return maps_equal(&l_map, &r_map);
    }

    false
}

fn maps_equal(lhs: &rhai::Map, rhs: &rhai::Map) -> bool {
    if lhs.len() != rhs.len() {
        return false;
    }

    lhs.iter().all(|(k, v)| match rhs.get(k) {
        Some(rv) => dynamics_equal(v, rv),
        None => false,
    })
}

use std::sync::atomic::Ordering;
use std::sync::{Arc, Mutex};
pub struct DebugTracker {
    pub config: DebugConfig,
    context: Arc<Mutex<ExecutionContext>>,
    event_count: Arc<Mutex<u64>>,
    error_count: Arc<Mutex<u64>>,
}

impl DebugTracker {
    pub fn new(config: DebugConfig) -> Self {
        DebugTracker {
            config,
            context: Arc::new(Mutex::new(ExecutionContext::default())),
            event_count: Arc::new(Mutex::new(0)),
            error_count: Arc::new(Mutex::new(0)),
        }
    }

    pub fn log_basic(&self, message: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 1 {
            eprintln!("{}", message);
        }
    }

    pub fn log_detailed(&self, stage: &str, event_num: u64, operation: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 2 {
            eprintln!("Trace: Event #{} {} → {}", event_num, stage, operation);
        }
    }

    pub fn log_step(&self, step_info: &str, result: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 3 {
            eprintln!("  → {} → {}", step_info, result);
        }
    }

    pub fn log_execution_start(&self, stage: &str, script: &str, event_data: &str) {
        match self.config.verbosity {
            1 => {
                if self.config.is_enabled() {
                    let prefix = if self.config.use_emoji {
                        "🔹"
                    } else {
                        "kelora: "
                    };
                    eprintln!("{} Executing {} stage", prefix, stage);
                }
            }
            2 => {
                if self.config.is_enabled() {
                    eprintln!("{} execution started", stage);
                    eprintln!("  Script: {}", truncate_for_display(script, 100));
                }
            }
            3.. => {
                if self.config.is_enabled() {
                    eprintln!("{} execution trace:", stage);
                    eprintln!("  Script: {}", script.trim());
                    eprintln!("  Event: {}", truncate_for_display(event_data, 150));
                }
            }
            _ => {}
        }
    }

    pub fn log_execution_result(&self, stage: &str, success: bool, result_info: &str) {
        if self.config.is_enabled() && self.config.verbosity >= 2 {
            let status = if success { "✓" } else { "✗" };
            eprintln!("{} {} ({})", stage, status, result_info);
        }
    }

    pub fn update_context(&self, position: Option<rhai::Position>, source: Option<&str>) {
        if self.config.is_enabled() {
            if let Ok(mut ctx) = self.context.lock() {
                ctx.position = position;
                ctx.source_snippet = source.map(|s| s.to_string());
            }
        }
    }

    pub fn get_context(&self) -> ExecutionContext {
        if let Ok(ctx) = self.context.lock() {
            ctx.clone()
        } else {
            ExecutionContext::default()
        }
    }
}

impl Clone for DebugTracker {
    fn clone(&self) -> Self {
        DebugTracker {
            config: self.config.clone(),
            context: Arc::clone(&self.context),
            event_count: Arc::clone(&self.event_count),
            error_count: Arc::clone(&self.error_count),
        }
    }
}

pub struct ErrorEnhancer {
    debug_config: DebugConfig,
}

impl ErrorEnhancer {
    pub fn new(debug_config: DebugConfig) -> Self {
        ErrorEnhancer { debug_config }
    }

    pub fn enhance_error(
        &self,
        error: &EvalAltResult,
        scope: &Scope,
        script: &str,
        stage: &str,
        execution_context: &ExecutionContext,
    ) -> String {
        let mut output = String::new();

        // Basic error info
        output.push_str(&format!("🔸 Stage {} failed\n", stage));
        output.push_str(&format!("  Code: {}\n", script.trim()));
        output.push_str(&format!("  Error: {}\n", error));

        // Add execution context if available
        if let Some(pos) = &execution_context.position {
            output.push_str(&format!("   Position: {}\n", pos));
        }

        // Suggestions and stage tips should be shown even without verbose mode
        if let Some(suggestions) = self.generate_suggestions(error, scope, Some(script)) {
            output.push_str(&format!("   💡 {}\n", suggestions));
        }

        // Show scope information only if debug enabled (can be verbose)
        if self.debug_config.is_enabled() {
            output.push_str("\n   Variables in scope:\n");
            for (name, _is_const, value) in scope.iter() {
                let preview = format!("{:?}", value);
                let preview = if preview.len() > 50 {
                    format!("{}...", &preview[..47])
                } else {
                    preview
                };
                output.push_str(&format!(
                    "   • {}: {} = {}\n",
                    name,
                    value.type_name(),
                    preview
                ));
            }
        }

        // Add stage-specific help (applies even without debug verbosity)
        output.push_str(&self.get_stage_help(stage, error));

        output
    }

    fn generate_suggestions(
        &self,
        error: &EvalAltResult,
        scope: &Scope,
        script: Option<&str>,
    ) -> Option<String> {
        let base = match error {
            EvalAltResult::ErrorVariableNotFound(var_name, _) => {
                let similar = self.find_similar_variables(var_name, scope);
                if !similar.is_empty() {
                    Some(format!("Did you mean: {}?", similar.join(", ")))
                } else {
                    // Check for common patterns
                    if var_name.contains('.') {
                        Some("Check if the field exists and use safe access like 'if \"field\" in e { e.field } else { \"default\" }'".to_string())
                    } else if var_name.starts_with("e.") {
                        Some("Try using bracket notation for special characters: e[\"field-name\"] or e[\"field.with.dots\"]".to_string())
                    } else {
                        Some("Available variables: e (event), meta (metadata), conf (initialization data), line (raw line)".to_string())
                    }
                }
            }
            EvalAltResult::ErrorPropertyNotFound(prop_name, _) => {
                let mut suggestions = Vec::new();

                // Offer similar field names from `e` map if available
                if let Some(fields) = self.event_field_names(scope) {
                    let similar: Vec<_> = fields
                        .iter()
                        .filter(|name| {
                            let sim = self.calculate_similarity(
                                &prop_name.to_lowercase(),
                                &name.to_lowercase(),
                            );
                            sim > 0.6
                                || name.contains(prop_name)
                                || prop_name.contains(name.as_str())
                        })
                        .take(3)
                        .cloned()
                        .collect();
                    if !similar.is_empty() {
                        suggestions.push(format!("Did you mean field: {}?", similar.join(", ")));
                    } else {
                        let preview: Vec<_> = fields.into_iter().take(5).collect();
                        if !preview.is_empty() {
                            suggestions.push(format!(
                                "Available fields include: {}{}",
                                preview.join(", "),
                                if preview.len() == 5 { " ..." } else { "" }
                            ));
                        }
                    }
                }

                suggestions
                    .push("Try `--stats` or `-F inspect` to see available fields".to_string());
                Some(suggestions.join(" "))
            }
            EvalAltResult::ErrorIndexNotFound(index, _) => Some(format!(
                "Index '{}' not found. Check array bounds with 'if e.array.len() > {} {{ ... }}'",
                index, index
            )),
            EvalAltResult::ErrorFunctionNotFound(func_sig, _) => {
                self.suggest_function_alternatives(func_sig)
            }
            EvalAltResult::ErrorMismatchDataType(expected, actual, _) => {
                let mut hints = vec![format!(
                    "Type mismatch: expected {}, got {}.",
                    expected, actual
                )];

                if expected.contains("bool") {
                    hints.push(
                        "Filters must return true/false; use comparisons like `e.level == \"ERROR\"` or `contains(...)`"
                            .to_string(),
                    );
                }
                if actual.contains("()") || expected.contains("()") {
                    hints.push(
                        "Missing fields return () by default; guard with e.has(\"field\") or e.get(\"field\", default) before chaining"
                            .to_string(),
                    );
                }
                hints.push(
                    "Use type_of() to check types or to_string()/to_number()/parse_json() for conversion"
                        .to_string(),
                );

                Some(hints.join(" "))
            }
            EvalAltResult::ErrorRuntime(msg, _) => {
                // Detect custom error messages involving Unit type "()"
                let msg_str = msg.to_string();
                if msg_str.contains("got ()") {
                    Some(
                        "Received (), which means a field is missing or returned no value. \
                         Use e.get_path('field.path', default) to provide defaults, \
                         or e.has_path('field.path') to check if a field exists first."
                            .to_string(),
                    )
                } else {
                    None
                }
            }
            _ => None,
        };

        let raw_string_hint = script.and_then(|script| Self::raw_string_hint(error, script));

        match (base, raw_string_hint) {
            (Some(base), Some(hint)) => Some(format!("{} {}", base, hint)),
            (Some(base), None) => Some(base),
            (None, Some(hint)) => Some(hint),
            (None, None) => None,
        }
    }

    pub(crate) fn raw_string_hint(error: &EvalAltResult, script: &str) -> Option<String> {
        match error {
            EvalAltResult::ErrorParsing(_, _) if Self::contains_rust_raw_string(script) => Some(
                "It looks like a Rust raw string (r\"...\"). Rhai raw strings use #\"...\"# (or ##\"...\"## for embedded quotes)."
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn contains_rust_raw_string(script: &str) -> bool {
        let bytes = script.as_bytes();
        let mut i = 0;
        while i < bytes.len() {
            if bytes[i] == b'r' {
                let prev = if i == 0 { None } else { Some(bytes[i - 1]) };
                let starts_token = prev.is_none_or(|c| !Self::is_ident_char(c));
                if starts_token {
                    let mut j = i + 1;
                    while j < bytes.len() && bytes[j] == b'#' {
                        j += 1;
                    }
                    if j < bytes.len() && bytes[j] == b'"' {
                        return true;
                    }
                }
            }
            i += 1;
        }
        false
    }

    fn is_ident_char(byte: u8) -> bool {
        byte.is_ascii_alphanumeric() || byte == b'_'
    }

    fn suggest_function_alternatives(&self, func_sig: &str) -> Option<String> {
        // Detect operations on missing fields (unit type)
        if func_sig.contains("()") {
            let func_name = func_sig.split('(').next().unwrap_or("").trim();

            // Check for binary operations with () operand
            if matches!(
                func_name,
                "+" | "-"
                    | "*"
                    | "/"
                    | "%"
                    | "=="
                    | "!="
                    | "<"
                    | ">"
                    | "<="
                    | ">="
                    | "&&"
                    | "||"
                    | "&"
                    | "|"
                    | "^"
            ) {
                return Some(format!(
                    "Field is missing. Use e.has(\"field\") or e.get_path(\"field\", default) before using '{}'",
                    func_name
                ));
            }

            // Check for method/function calls with () as parameter
            // Matches: "method (())", "method ((), other)", "method (other, ())", etc.
            if func_sig.contains(" (())")
                || func_sig.contains("((), ")
                || func_sig.contains(", ())")
            {
                return Some(
                    "Field is missing. Use e.has(\"field\") to check, or e.get_path(\"field\", default) to provide a default"
                        .to_string(),
                );
            }
        }

        let func_name = func_sig.split('(').next().unwrap_or(func_sig).trim();

        let mut best: Vec<(String, f64)> = RhaiEngine::function_catalog()
            .into_iter()
            .map(|candidate| {
                let sim =
                    self.calculate_similarity(&func_name.to_lowercase(), &candidate.to_lowercase());
                (candidate, sim)
            })
            .filter(|(_, sim)| *sim > 0.45)
            .collect();

        best.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        best.truncate(3);

        if !best.is_empty() {
            return Some(format!(
                "Did you mean: {}?",
                best.iter()
                    .map(|(c, _)| c.clone())
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }

        // Common function alternatives as fallbacks
        match func_name {
            "length" => Some("Use 'len()' instead of 'length()'".to_string()),
            "size" => Some("Use 'len()' instead of 'size()'".to_string()),
            "substr" | "substring" => Some(
                "Use string slicing: s[start..end] or extract_regex() for pattern matching"
                    .to_string(),
            ),
            "indexOf" | "index_of" => Some(
                "Use 'contains()' to check existence or 'split()' to find positions".to_string(),
            ),
            "push_back" | "append" => Some("Use 'push()' to add elements to arrays".to_string()),
            "to_int" | "parseInt" => {
                Some("Use 'parse()' or to_number() for type conversion".to_string())
            }
            "to_str" | "toString" => Some("Use 'to_string()' for string conversion".to_string()),
            "match" => Some(
                "Use 'extract_regex()' for regex matching or 'contains()' for simple checks"
                    .to_string(),
            ),
            name if name.ends_with("_re") => Some(
                "Regex functions: extract_regex(), extract_regexes(), extract_regex_maps(), split_regex(), replace_regex()"
                    .to_string(),
            ),
            _ => None,
        }
    }

    fn find_similar_variables(&self, target: &str, scope: &Scope) -> Vec<String> {
        let mut suggestions = Vec::new();
        let target_lower = target.to_lowercase();

        for (name, _is_const, _value) in scope.iter() {
            let name_lower = name.to_lowercase();
            let similarity = self.calculate_similarity(&target_lower, &name_lower);

            // Include variables with good similarity or common patterns
            if similarity > 0.6
                || name_lower.contains(&target_lower)
                || target_lower.contains(&name_lower)
                || self.has_common_prefix(&target_lower, &name_lower)
            {
                suggestions.push(name.to_string());
            }
        }

        // Sort by similarity (best matches first)
        suggestions.sort_by(|a, b| {
            let sim_a = self.calculate_similarity(&target_lower, &a.to_lowercase());
            let sim_b = self.calculate_similarity(&target_lower, &b.to_lowercase());
            sim_b
                .partial_cmp(&sim_a)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Return top 3 suggestions
        suggestions.truncate(3);
        suggestions
    }

    fn event_field_names(&self, scope: &Scope) -> Option<Vec<String>> {
        if let Some(e_map) = scope.get_value::<Map>("e") {
            let mut keys: Vec<String> = e_map
                .into_keys()
                .map(|k| k.to_string())
                .filter(|k| !k.is_empty())
                .collect();
            keys.sort();
            keys.dedup();
            return Some(keys);
        }
        None
    }

    fn calculate_similarity(&self, s1: &str, s2: &str) -> f64 {
        if s1 == s2 {
            return 1.0;
        }
        if s1.is_empty() || s2.is_empty() {
            return 0.0;
        }

        // Simple Levenshtein-based similarity
        let len1 = s1.len();
        let len2 = s2.len();
        let max_len = len1.max(len2);

        let distance = self.levenshtein_distance(s1, s2);
        1.0 - (distance as f64 / max_len as f64)
    }

    fn levenshtein_distance(&self, s1: &str, s2: &str) -> usize {
        let chars1: Vec<char> = s1.chars().collect();
        let chars2: Vec<char> = s2.chars().collect();
        let len1 = chars1.len();
        let len2 = chars2.len();

        if len1 == 0 {
            return len2;
        }
        if len2 == 0 {
            return len1;
        }

        let mut prev_row: Vec<usize> = (0..=len2).collect();

        for i in 1..=len1 {
            let mut curr_row = vec![i];

            for j in 1..=len2 {
                let cost = if chars1[i - 1] == chars2[j - 1] { 0 } else { 1 };
                curr_row.push(
                    (curr_row[j - 1] + 1) // insertion
                        .min(prev_row[j] + 1) // deletion
                        .min(prev_row[j - 1] + cost), // substitution
                );
            }

            prev_row = curr_row;
        }

        prev_row[len2]
    }

    fn has_common_prefix(&self, s1: &str, s2: &str) -> bool {
        if s1.len() < 2 || s2.len() < 2 {
            return false;
        }
        let prefix_len = 2.min(s1.len()).min(s2.len());
        s1[..prefix_len] == s2[..prefix_len]
    }

    fn get_stage_help(&self, stage: &str, error: &EvalAltResult) -> String {
        let mut help = String::new();

        match stage {
            "filter" => {
                help.push_str("\n   🔹 Filter stage tips:\n");
                help.push_str("   • Filters must return true/false (boolean values)\n");
                help.push_str("   • Use 'e.field_name' to access event fields\n");
                help.push_str(
                    "   • Use 'e[\"field-with-special-chars\"]' for complex field names\n",
                );
                help.push_str("   • Use 'if \"field\" in e { ... }' to check field existence\n");

                if let EvalAltResult::ErrorMismatchDataType(_, _, _) = error {
                    help.push_str(
                        "   • Remember: filters need boolean results, not strings or numbers\n",
                    );
                }
            }
            "exec" => {
                help.push_str("\n   🔹 Exec stage tips:\n");
                help.push_str("   • Use 'e.new_field = value' to add fields to events\n");
                help.push_str("   • Use 'e.field = ()' to remove fields from events\n");
                help.push_str("   • Use 'e = ()' to remove entire event (filter out)\n");
                help.push_str("   • Use 'let variable = value' for temporary variables\n");
                help.push_str("   • Use 'print(\"debug: \" + value)' for debugging output\n");
            }
            "begin" => {
                help.push_str("\n   🔹 Begin stage tips:\n");
                help.push_str("   • Use 'conf.field = value' to set global initialization data\n");
                help.push_str("   • Use 'read_file(\"path\")' to load external data\n");
                help.push_str("   • Variables set here are available in all event processing\n");
            }
            "end" => {
                help.push_str("\n   🔹 End stage tips:\n");
                help.push_str("   • Use 'metrics.key' to access accumulated tracking data\n");
                help.push_str("   • Use 'print()' to output final results\n");
                help.push_str("   • This runs after all events are processed\n");
            }
            _ => {}
        }

        help
    }
}

// Execution Tracer for step-by-step debugging
pub struct ExecutionTracer {
    config: DebugConfig,
    current_event: Arc<Mutex<u64>>,
    step_counter: Arc<Mutex<u32>>,
}

impl ExecutionTracer {
    pub fn new(config: DebugConfig) -> Self {
        ExecutionTracer {
            config,
            current_event: Arc::new(Mutex::new(0)),
            step_counter: Arc::new(Mutex::new(0)),
        }
    }

    pub fn trace_stage_execution(&self, stage_number: usize, stage_type: &str) {
        if self.config.verbosity >= 1 {
            let prefix = if self.config.use_emoji {
                "🔹"
            } else {
                "kelora: "
            };
            eprintln!(
                "{}Executing stage {} ({})",
                prefix, stage_number, stage_type
            );
        }
    }

    pub fn trace_step(&self, _event_num: u64, step_info: &str, result: &str) {
        if self.config.verbosity >= 2 {
            eprintln!("  → {} → {}", step_info, result);
        }
    }

    pub fn trace_event_start(&self, event_num: u64, event_data: &str) {
        if self.config.verbosity >= 2 {
            eprintln!("  Filter execution trace for event {}:", event_num);
            eprintln!("    Event: {}", truncate_for_display(event_data, 100));
        }
    }

    pub fn trace_event_result(&self, result: bool, action: &str) {
        if self.config.verbosity >= 2 {
            eprintln!("    Result: {} ({})", result, action);
        }
    }

    pub fn trace_expression_evaluation(&self, expression: &str, intermediate_result: &str) {
        if self.config.verbosity >= 3 {
            eprintln!("    Eval: {} → {}", expression, intermediate_result);
        }
    }

    pub fn trace_variable_access(&self, var_name: &str, value: &str) {
        if self.config.verbosity >= 3 {
            eprintln!(
                "    Access: {} = {}",
                var_name,
                truncate_for_display(value, 30)
            );
        }
    }

    pub fn trace_function_call(&self, func_name: &str, args: &str, result: &str) {
        if self.config.verbosity >= 3 {
            eprintln!(
                "    Call: {}({}) → {}",
                func_name,
                args,
                truncate_for_display(result, 30)
            );
        }
    }

    // Enhanced detailed tracing for -vvv level
    pub fn trace_detailed_step(
        &self,
        context: &str,
        operation: &str,
        input: &str,
        output: &str,
        step_type: &str,
    ) {
        if self.config.verbosity >= 3 {
            let step_num = {
                match self.step_counter.lock() {
                    Ok(mut counter) => {
                        *counter += 1;
                        *counter
                    }
                    Err(_) => {
                        // If mutex is poisoned, continue with a default value
                        eprintln!("Warning: Step counter mutex poisoned, using default");
                        0
                    }
                }
            };

            eprintln!(
                "    [Step {}:{}] {}: {} → {}",
                step_num,
                context,
                operation,
                truncate_for_display(input, 30),
                truncate_for_display(output, 30)
            );

            if step_type != "default" {
                eprintln!("      Type: {}", step_type);
            }
        }
    }

    pub fn trace_scope_inspection(&self, scope: &rhai::Scope) {
        if self.config.verbosity >= 3 {
            eprintln!("    Scope contents:");
            let mut scope_items: Vec<_> = scope.iter().collect();
            scope_items.sort_by(|a, b| a.0.cmp(b.0));
            for (name, _is_const, value) in scope_items {
                let type_info = value.type_name();
                let preview = format!("{:?}", value);
                eprintln!("      {} ({}): {}", name, type_info, preview);
            }
        }
    }

    pub fn trace_ast_node(&self, node_type: &str, position: &str, source: &str) {
        if self.config.verbosity >= 3 {
            eprintln!(
                "    AST: {} at {} → \"{}\"",
                node_type,
                position,
                truncate_for_display(source, 40)
            );
        }
    }

    pub fn next_event(&self) -> u64 {
        if let Ok(mut counter) = self.current_event.lock() {
            *counter += 1;
            *counter
        } else {
            0
        }
    }

    pub fn reset_step_counter(&self) {
        if let Ok(mut counter) = self.step_counter.lock() {
            *counter = 0;
        }
    }
}

impl Clone for ExecutionTracer {
    fn clone(&self) -> Self {
        ExecutionTracer {
            config: self.config.clone(),
            current_event: Arc::clone(&self.current_event),
            step_counter: Arc::clone(&self.step_counter),
        }
    }
}

// Performance and Statistics Tracking using thread-local storage
// This integrates with kelora's parallel processing infrastructure like track_count()

use std::cell::RefCell;

#[derive(Debug, Clone, Default)]
pub struct DebugStatistics {
    pub start_time: Option<std::time::Instant>,
    pub events_processed: u64,
    pub events_passed: u64,
    pub errors_encountered: u64,
    pub script_executions: u64,
}

impl DebugStatistics {
    pub fn new() -> Self {
        DebugStatistics {
            start_time: Some(std::time::Instant::now()),
            events_processed: 0,
            events_passed: 0,
            errors_encountered: 0,
            script_executions: 0,
        }
    }
}

// Thread-local storage for debug statistics (following track_count pattern)
thread_local! {
    static THREAD_DEBUG_STATS: RefCell<DebugStatistics> = RefCell::new(DebugStatistics::new());
}

// Debug statistics collection functions (following stats.rs pattern)
pub fn debug_stats_increment_events_processed() {
    THREAD_DEBUG_STATS.with(|stats| {
        stats.borrow_mut().events_processed += 1;
    });
}

pub fn debug_stats_increment_events_passed() {
    THREAD_DEBUG_STATS.with(|stats| {
        stats.borrow_mut().events_passed += 1;
    });
}

pub fn debug_stats_increment_errors() {
    THREAD_DEBUG_STATS.with(|stats| {
        stats.borrow_mut().errors_encountered += 1;
    });
}

pub fn debug_stats_increment_script_executions() {
    THREAD_DEBUG_STATS.with(|stats| {
        stats.borrow_mut().script_executions += 1;
    });
}

pub fn debug_stats_get_thread_state() -> DebugStatistics {
    THREAD_DEBUG_STATS.with(|stats| stats.borrow().clone())
}

pub fn debug_stats_set_thread_state(stats: &DebugStatistics) {
    THREAD_DEBUG_STATS.with(|local_stats| {
        *local_stats.borrow_mut() = stats.clone();
    });
}

/// Represents the type of access to a field in a Rhai expression
#[derive(Debug, Clone, PartialEq, Eq)]
enum AccessType {
    Read,      // Field is being read/accessed
    Write,     // Field is being assigned (LHS of simple assignment)
    ReadWrite, // Field is both read and written (compound assignments: +=, -=, etc.)
}

/// Represents a field access with its access type
#[derive(Debug, Clone)]
struct FieldAccess {
    field_name: String,
    access_type: AccessType,
}

#[derive(Clone)]
struct NativePredicate {
    root: NativeNode,
}

#[derive(Clone)]
enum NativeNode {
    And(Vec<NativeNode>),
    Or(Vec<NativeNode>),
    Not(Box<NativeNode>),
    Value(NativeValueExpr),
    Compare {
        op: CompareOp,
        lhs: NativeValueExpr,
        rhs: NativeValueExpr,
    },
    /// String method calls like e.field.contains("x")
    StringMethod {
        op: StringOp,
        /// The string value to operate on (e.g., e.message)
        target: NativeValueExpr,
        /// The argument (e.g., "error" in contains("error"))
        arg: NativeValueExpr,
    },
}

#[derive(Clone, Copy)]
enum StringOp {
    Contains,
    StartsWith,
    EndsWith,
}

#[derive(Clone)]
enum NativeValueExpr {
    Field(String),
    Literal(NativeValue),
}

#[derive(Clone, Copy)]
enum CompareOp {
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
}

#[derive(Clone)]
enum NativeValue {
    Bool(bool),
    Int(rhai::INT),
    Float(rhai::FLOAT),
    Str(String),
    Unit,
}

/// Extract field names accessed on variable 'e' from a Rhai AST
///
/// Uses AST walking to find all property access patterns like `e.field_name`
/// including chained method calls like `e.field.to_upper()`.
/// Distinguishes between field reads and writes to avoid false warnings.
///
/// Requires the 'debugging' feature which provides AST access.
fn extract_field_accesses(ast: &AST) -> Vec<FieldAccess> {
    use std::collections::HashMap;

    let mut accesses: HashMap<String, AccessType> = HashMap::new();

    ast.walk(&mut |path| {
        if let Some(node) = path.first() {
            let node_str = format!("{:?}", node);

            // Skip nodes that don't involve Variable(e)
            if !node_str.contains("Variable(e)") {
                return true;
            }

            // Check if this is an assignment statement
            if node_str.contains("Assignment(") {
                extract_assignment_fields(&node_str, &mut accesses);
            } else {
                // Not an assignment - all fields are reads
                extract_read_fields(&node_str, &mut accesses);
            }
        }
        true
    });

    // Convert HashMap to Vec<FieldAccess>
    accesses
        .into_iter()
        .map(|(field_name, access_type)| FieldAccess {
            field_name,
            access_type,
        })
        .collect()
}

fn expression_mutates_event(ast: &AST, field_accesses: &[FieldAccess]) -> bool {
    if field_accesses
        .iter()
        .any(|fa| matches!(fa.access_type, AccessType::Write | AccessType::ReadWrite))
    {
        return true;
    }

    // Whole-map assignments like `e = ()` do not produce field-level writes but still
    // replace the event payload.
    let ast_text = format!("{:?}", ast.statements());
    ast_text.contains("Assignment(") && ast_text.contains("lhs: Variable(e)")
}

/// Flags indicating which scope variables are used by an expression
#[derive(Clone, Copy, Default)]
struct VariableUsage {
    uses_meta: bool,
    uses_conf: bool,
    uses_line: bool,
    meta_usage: MetaUsage,
}

#[derive(Clone, Copy, Default)]
struct MetaUsage {
    populate_all: bool,
    line: bool,
    line_num: bool,
    filename: bool,
    parsed_ts: bool,
    span_status: bool,
    span_id: bool,
    span_start: bool,
    span_end: bool,
}

impl MetaUsage {
    fn any(&self) -> bool {
        self.line
            || self.line_num
            || self.filename
            || self.parsed_ts
            || self.span_status
            || self.span_id
            || self.span_start
            || self.span_end
    }
}

/// Detect which scope variables (meta, conf, line) are used in the AST
fn detect_variable_usage(ast: &AST) -> VariableUsage {
    let mut usage = VariableUsage::default();

    ast.walk(&mut |path| {
        if let Some(node) = path.first() {
            let node_str = format!("{:?}", node);

            // Check for Variable references
            if node_str.contains("Variable(meta)") {
                usage.uses_meta = true;

                if node_str.contains("Property(line_num)") {
                    usage.meta_usage.line_num = true;
                }
                if node_str.contains("Property(filename)") {
                    usage.meta_usage.filename = true;
                }
                if node_str.contains("Property(parsed_ts)") {
                    usage.meta_usage.parsed_ts = true;
                }
                if node_str.contains("Property(line)") {
                    usage.meta_usage.line = true;
                }
                if node_str.contains("Property(span_status)") {
                    usage.meta_usage.span_status = true;
                }
                if node_str.contains("Property(span_id)") {
                    usage.meta_usage.span_id = true;
                }
                if node_str.contains("Property(span_start)") {
                    usage.meta_usage.span_start = true;
                }
                if node_str.contains("Property(span_end)") {
                    usage.meta_usage.span_end = true;
                }

                // If `meta` is referenced but no concrete field is visible in the AST dump,
                // keep the old behavior and populate the full metadata map.
                if !usage.meta_usage.any() {
                    usage.meta_usage.populate_all = true;
                }
            }
            if node_str.contains("Variable(conf)") {
                usage.uses_conf = true;
            }
            if node_str.contains("Variable(line)") {
                usage.uses_line = true;
            }
        }
        true
    });

    usage
}

fn build_native_predicate(ast: &AST) -> Option<NativePredicate> {
    let statements = ast.statements();
    if statements.len() != 1 {
        return None;
    }

    let expr = match &statements[0] {
        Stmt::Expr(expr) => expr.as_ref(),
        _ => return None,
    };

    let root = parse_native_node(expr)?;
    Some(NativePredicate { root })
}

fn parse_native_node(expr: &Expr) -> Option<NativeNode> {
    match expr {
        Expr::And(list, _) => {
            let mut nodes = Vec::with_capacity(list.len());
            for item in list.iter() {
                nodes.push(parse_native_node(item)?);
            }
            Some(NativeNode::And(nodes))
        }
        Expr::Or(list, _) => {
            let mut nodes = Vec::with_capacity(list.len());
            for item in list.iter() {
                nodes.push(parse_native_node(item)?);
            }
            Some(NativeNode::Or(nodes))
        }
        Expr::FnCall(call, _) => {
            if let Some(token) = call.op_token.clone() {
                match token {
                    Token::Bang if call.args.len() == 1 => {
                        let node = parse_native_node(&call.args[0])?;
                        Some(NativeNode::Not(Box::new(node)))
                    }
                    Token::EqualsTo => parse_compare_node(CompareOp::Eq, call),
                    Token::NotEqualsTo => parse_compare_node(CompareOp::Ne, call),
                    Token::LessThan => parse_compare_node(CompareOp::Lt, call),
                    Token::LessThanEqualsTo => parse_compare_node(CompareOp::Le, call),
                    Token::GreaterThan => parse_compare_node(CompareOp::Gt, call),
                    Token::GreaterThanEqualsTo => parse_compare_node(CompareOp::Ge, call),
                    _ => None,
                }
            } else {
                None
            }
        }
        Expr::BoolConstant(value, _) => Some(NativeNode::Value(NativeValueExpr::Literal(
            NativeValue::Bool(*value),
        ))),
        // Handle method calls like e.message.contains("error")
        Expr::Dot(binary, _, _) => parse_string_method(binary),
        _ => parse_value_expr(expr).map(NativeNode::Value),
    }
}

/// Parse string method calls like e.field.contains("pattern")
/// AST structure for `e.level.starts_with("ERR")`:
/// Dot { lhs: Variable(e), rhs: Dot { lhs: Property(level), rhs: MethodCall(...) } }
fn parse_string_method(binary: &BinaryExpr) -> Option<NativeNode> {
    // For e.field.method(arg), the structure is:
    // Dot { lhs: Variable(e), rhs: Dot { lhs: Property(field), rhs: MethodCall } }
    // We need to find the innermost Dot that has a MethodCall as rhs

    // Try to extract method call info by walking the Dot chain
    let (field_path, method_name, arg) = extract_method_call(binary)?;

    // Map method name to operation
    let op = match method_name.as_str() {
        "contains" => StringOp::Contains,
        "starts_with" => StringOp::StartsWith,
        "ends_with" => StringOp::EndsWith,
        _ => return None,
    };

    let target = NativeValueExpr::Field(field_path);
    Some(NativeNode::StringMethod { op, target, arg })
}

/// Extract method call info from a Dot expression chain
/// Returns (field_path, method_name, arg) if successful
fn extract_method_call(binary: &BinaryExpr) -> Option<(String, String, NativeValueExpr)> {
    // Check if this is a direct method call: Dot { lhs: field_expr, rhs: MethodCall }
    if let Expr::MethodCall(call, _) = &binary.rhs {
        if call.args.len() == 1 {
            let arg = parse_value_expr(&call.args[0])?;
            let field_path = extract_field_path(&binary.lhs)?;
            return Some((field_path, call.name.to_string(), arg));
        }
    }

    // Check if rhs is another Dot containing a MethodCall
    if let Expr::Dot(inner_binary, _, _) = &binary.rhs {
        // Recursively check the inner Dot
        if let Some((mut path, method, arg)) = extract_method_call(inner_binary) {
            // Prepend the lhs field to the path
            let lhs_path = extract_field_path(&binary.lhs)?;
            if path.is_empty() {
                path = lhs_path;
            } else if !lhs_path.is_empty() {
                path = format!("{}.{}", lhs_path, path);
            }
            return Some((path, method, arg));
        }
    }

    None
}

fn parse_compare_node(op: CompareOp, call: &FnCallExpr) -> Option<NativeNode> {
    if call.args.len() != 2 {
        return None;
    }

    let lhs = parse_value_expr(&call.args[0])?;
    let rhs = parse_value_expr(&call.args[1])?;

    Some(NativeNode::Compare { op, lhs, rhs })
}

fn parse_value_expr(expr: &Expr) -> Option<NativeValueExpr> {
    match expr {
        Expr::BoolConstant(value, _) => Some(NativeValueExpr::Literal(NativeValue::Bool(*value))),
        Expr::IntegerConstant(value, _) => Some(NativeValueExpr::Literal(NativeValue::Int(*value))),
        Expr::FloatConstant(value, _) => {
            Some(NativeValueExpr::Literal(NativeValue::Float(**value)))
        }
        Expr::StringConstant(value, _) => Some(NativeValueExpr::Literal(NativeValue::Str(
            value.to_string(),
        ))),
        Expr::Unit(..) => Some(NativeValueExpr::Literal(NativeValue::Unit)),
        _ => extract_field_path(expr)
            .filter(|path| !path.is_empty())
            .map(NativeValueExpr::Field),
    }
}

fn extract_field_path(expr: &Expr) -> Option<String> {
    match expr {
        Expr::Dot(binary, options, _) if options.is_empty() => {
            let mut base = extract_field_path(&binary.lhs)?;
            let rhs = extract_field_path(&binary.rhs)?;
            if base.is_empty() {
                base = rhs;
            } else {
                base.push('.');
                base.push_str(&rhs);
            }
            Some(base)
        }
        Expr::Index(binary, options, _) if options.is_empty() => {
            let mut base = extract_field_path(&binary.lhs)?;
            let rhs = match &binary.rhs {
                Expr::StringConstant(s, _) => s.to_string(),
                Expr::IntegerConstant(i, _) => i.to_string(),
                _ => return None,
            };

            if !base.is_empty() {
                base.push('.');
            }
            base.push_str(&rhs);
            Some(base)
        }
        Expr::Property(prop, _) => Some(prop.2.to_string()),
        Expr::Variable(var, ..) => {
            let name = &var.1;
            if name == "e" {
                Some(String::new())
            } else {
                None
            }
        }
        _ => None,
    }
}

impl NativePredicate {
    fn evaluate(&self, event: &Event) -> Option<bool> {
        eval_native_node(&self.root, event)
    }
}

fn eval_native_node(node: &NativeNode, event: &Event) -> Option<bool> {
    match node {
        NativeNode::And(nodes) => {
            let mut result = true;
            for n in nodes {
                let value = eval_native_node(n, event)?;
                result &= value;
                if !result {
                    break;
                }
            }
            Some(result)
        }
        NativeNode::Or(nodes) => {
            let mut result = false;
            for n in nodes {
                let value = eval_native_node(n, event)?;
                result |= value;
                if result {
                    break;
                }
            }
            Some(result)
        }
        NativeNode::Not(child) => eval_native_node(child, event).map(|v| !v),
        NativeNode::Value(expr) => match eval_value_expr(expr, event)? {
            NativeValue::Bool(b) => Some(b),
            _ => None,
        },
        NativeNode::Compare { op, lhs, rhs } => {
            let lhs_val = eval_value_expr(lhs, event)?;
            let rhs_val = eval_value_expr(rhs, event)?;
            compare_values(&lhs_val, &rhs_val, *op)
        }
        NativeNode::StringMethod { op, target, arg } => {
            let target_val = eval_value_expr(target, event)?;
            let arg_val = eval_value_expr(arg, event)?;

            // Both must be strings
            let (target_str, arg_str) = match (&target_val, &arg_val) {
                (NativeValue::Str(t), NativeValue::Str(a)) => (t.as_str(), a.as_str()),
                _ => return None,
            };

            let result = match op {
                StringOp::Contains => target_str.contains(arg_str),
                StringOp::StartsWith => target_str.starts_with(arg_str),
                StringOp::EndsWith => target_str.ends_with(arg_str),
            };
            Some(result)
        }
    }
}

fn eval_value_expr(expr: &NativeValueExpr, event: &Event) -> Option<NativeValue> {
    match expr {
        NativeValueExpr::Literal(value) => Some(value.clone()),
        NativeValueExpr::Field(path) => {
            let value = event.fields.get(path).cloned().unwrap_or(Dynamic::UNIT);
            dynamic_to_native(&value)
        }
    }
}

fn dynamic_to_native(value: &Dynamic) -> Option<NativeValue> {
    if value.is_unit() {
        return Some(NativeValue::Unit);
    }

    if let Ok(b) = value.as_bool() {
        return Some(NativeValue::Bool(b));
    }

    if let Ok(i) = value.as_int() {
        return Some(NativeValue::Int(i));
    }

    if let Ok(f) = value.as_float() {
        return Some(NativeValue::Float(f));
    }

    if let Some(s) = value.clone().try_cast::<rhai::ImmutableString>() {
        return Some(NativeValue::Str(s.to_string()));
    }

    if let Some(s) = value.clone().try_cast::<String>() {
        return Some(NativeValue::Str(s));
    }

    None
}

fn compare_values(lhs: &NativeValue, rhs: &NativeValue, op: CompareOp) -> Option<bool> {
    use CompareOp::*;

    match (lhs, rhs) {
        (NativeValue::Unit, NativeValue::Unit) => match op {
            Eq => Some(true),
            Ne => Some(false),
            _ => Some(false),
        },
        (NativeValue::Unit, _) | (_, NativeValue::Unit) => match op {
            Eq => Some(false),
            Ne => Some(true),
            _ => None,
        },
        (NativeValue::Bool(l), NativeValue::Bool(r)) => match op {
            Eq => Some(l == r),
            Ne => Some(l != r),
            _ => None,
        },
        (NativeValue::Str(l), NativeValue::Str(r)) => match op {
            Eq => Some(l == r),
            Ne => Some(l != r),
            _ => None,
        },
        (NativeValue::Int(l), NativeValue::Int(r)) => {
            compare_numbers(*l as rhai::FLOAT, *r as rhai::FLOAT, op)
        }
        (NativeValue::Float(l), NativeValue::Float(r)) => compare_numbers(*l, *r, op),
        (NativeValue::Int(l), NativeValue::Float(r)) => compare_numbers(*l as rhai::FLOAT, *r, op),
        (NativeValue::Float(l), NativeValue::Int(r)) => compare_numbers(*l, *r as rhai::FLOAT, op),
        _ => None,
    }
}

fn compare_numbers(lhs: rhai::FLOAT, rhs: rhai::FLOAT, op: CompareOp) -> Option<bool> {
    use CompareOp::*;
    match op {
        Eq => Some(lhs == rhs),
        Ne => Some(lhs != rhs),
        Lt => Some(lhs < rhs),
        Le => Some(lhs <= rhs),
        Gt => Some(lhs > rhs),
        Ge => Some(lhs >= rhs),
    }
}

/// Extract field accesses from assignment statements
fn extract_assignment_fields(
    node_str: &str,
    accesses: &mut std::collections::HashMap<String, AccessType>,
) {
    use AccessType::*;

    // Determine if this is a compound assignment (+=, -=, *=, etc.)
    let is_compound = node_str.contains("PlusAssign")
        || node_str.contains("MinusAssign")
        || node_str.contains("MultiplyAssign")
        || node_str.contains("DivideAssign")
        || node_str.contains("ModuloAssign")
        || node_str.contains("PowerOfAssign")
        || node_str.contains("ShiftLeftAssign")
        || node_str.contains("ShiftRightAssign")
        || node_str.contains("AndAssign")
        || node_str.contains("OrAssign")
        || node_str.contains("XOrAssign");

    // For assignments, Rhai uses: Stmt(Assignment((op, BinaryExpr { lhs: ..., rhs: ... })))
    // Find the BinaryExpr within the assignment
    if let Some(binary_start) = node_str.find("BinaryExpr {") {
        let binary_section = &node_str[binary_start..];

        // Extract LHS fields (target of assignment)
        if let Some(lhs_start) = binary_section.find("lhs:") {
            // Find the end of the lhs section (before "rhs:")
            let lhs_section = if let Some(rhs_pos) = binary_section[lhs_start..].find(", rhs:") {
                &binary_section[lhs_start..lhs_start + rhs_pos]
            } else {
                &binary_section[lhs_start..]
            };

            let lhs_fields = extract_fields_from_section(lhs_section);
            for field in lhs_fields {
                if is_compound {
                    // Compound assignment: field is both read and written
                    merge_access_type(accesses, field, ReadWrite);
                } else {
                    // Regular assignment: field is only written
                    merge_access_type(accesses, field, Write);
                }
            }
        }

        // Extract RHS fields (value being assigned)
        if let Some(rhs_start) = binary_section.find("rhs:") {
            let rhs_section = &binary_section[rhs_start..];
            let rhs_fields = extract_fields_from_section(rhs_section);

            for field in rhs_fields {
                // RHS fields are always reads
                merge_access_type(accesses, field, Read);
            }
        }
    }
}

/// Extract field accesses from non-assignment contexts (all reads)
fn extract_read_fields(
    node_str: &str,
    accesses: &mut std::collections::HashMap<String, AccessType>,
) {
    // Non-assignment context - all fields are reads
    let fields = extract_fields_from_section(node_str);
    for field in fields {
        merge_access_type(accesses, field, AccessType::Read);
    }
}

/// Helper function to extract field names from AST node section
fn extract_fields_from_section(section: &str) -> Vec<String> {
    let mut fields = Vec::new();

    // Pattern 1: Direct property access - Variable(e) ... Property(field_name)
    if let Ok(re) = regex::Regex::new(r"Variable\(e\)[^}]*Property\((\w+)\)") {
        for cap in re.captures_iter(section) {
            if let Some(field_name) = cap.get(1) {
                fields.push(field_name.as_str().to_string());
            }
        }
    }

    // Pattern 2: Nested case for method calls - rhs: Dot { lhs: Property(field)
    if section.contains("lhs: Variable(e)") {
        if let Ok(nested_re) = regex::Regex::new(r"rhs: Dot \{ lhs: Property\((\w+)\)") {
            for cap in nested_re.captures_iter(section) {
                if let Some(field_name) = cap.get(1) {
                    fields.push(field_name.as_str().to_string());
                }
            }
        }
    }

    fields
}

/// Merge access types for a field, upgrading to ReadWrite if accessed both ways
fn merge_access_type(
    accesses: &mut std::collections::HashMap<String, AccessType>,
    field: String,
    new_type: AccessType,
) {
    use AccessType::*;

    let current = accesses.entry(field.clone()).or_insert(new_type.clone());

    // Merge logic: if a field is both read and written, mark as ReadWrite
    *current = match (&*current, &new_type) {
        (Read, Write) | (Write, Read) => ReadWrite,
        (Read, ReadWrite) | (ReadWrite, Read) => ReadWrite,
        (Write, ReadWrite) | (ReadWrite, Write) => ReadWrite,
        (ReadWrite, ReadWrite) => ReadWrite,
        _ => new_type,
    };
}

#[derive(Clone)]
pub struct CompiledExpression {
    ast: AST,
    expr: String,
    field_accesses: Vec<FieldAccess>,
    native_predicate: Option<NativePredicate>,
    /// Whether this expression may mutate the `e` event map
    mutates_event: bool,
    meta_usage: MetaUsage,
    /// Whether this expression uses the `meta` variable
    uses_meta: bool,
    /// Whether this expression uses the `conf` variable
    uses_conf: bool,
    /// Whether this expression uses the `line` variable
    uses_line: bool,
}

impl CompiledExpression {
    /// Get the source expression
    pub fn source(&self) -> &str {
        &self.expr
    }

    /// Get fields that are READ (including ReadWrite, excluding pure Write)
    pub fn read_fields(&self) -> std::collections::HashSet<String> {
        self.field_accesses
            .iter()
            .filter(|fa| matches!(fa.access_type, AccessType::Read | AccessType::ReadWrite))
            .map(|fa| fa.field_name.clone())
            .collect()
    }

    /// Get fields that are WRITTEN (including ReadWrite, excluding pure Read)
    pub fn written_fields(&self) -> std::collections::HashSet<String> {
        self.field_accesses
            .iter()
            .filter(|fa| matches!(fa.access_type, AccessType::Write | AccessType::ReadWrite))
            .map(|fa| fa.field_name.clone())
            .collect()
    }

    /// Get all accessed fields (backward compatibility)
    pub fn accessed_fields(&self) -> std::collections::HashSet<String> {
        self.field_accesses
            .iter()
            .map(|fa| fa.field_name.clone())
            .collect()
    }
}

pub struct RhaiEngine {
    engine: Engine,
    compiled_filters: Vec<CompiledExpression>,
    compiled_execs: Vec<CompiledExpression>,
    compiled_begin: Option<CompiledExpression>,
    compiled_end: Option<CompiledExpression>,
    scope_template: Scope<'static>,
    suppress_side_effects: bool,
    conf_map: Option<rhai::Map>,
    state_map: Option<crate::rhai_functions::state::StateMap>,
    state_available: bool,
    debug_tracker: Option<DebugTracker>,
    execution_tracer: Option<ExecutionTracer>,
    use_emoji: bool,
}

impl Clone for RhaiEngine {
    fn clone(&self) -> Self {
        let mut engine = Engine::new();
        // Use Simple optimization, not Full. Full optimization breaks side-effect functions
        // like track_count("key"), print("msg"), emit_each(), etc. by trying to evaluate them at
        // compile time when their arguments are constants. These functions MUST run at runtime.
        engine.set_optimization_level(rhai::OptimizationLevel::Simple);

        // Check for shutdown signal during script execution (cooperative cancellation)
        engine.on_progress(|_| {
            if crate::platform::SHOULD_TERMINATE.load(Ordering::Relaxed) {
                // Abort script execution by returning a termination sentinel
                Some(rhai::Dynamic::UNIT)
            } else {
                None
            }
        });

        // Apply the same on_print override as in new(), respecting suppress_side_effects
        let suppress_side_effects = self.suppress_side_effects;
        engine.on_print(move |text| {
            if suppress_side_effects {
                // Suppress all print output
                return;
            }

            if crate::rhai_functions::strings::is_parallel_mode() {
                crate::rhai_functions::strings::capture_print(text.to_string());
            } else {
                println!("{}", text);
            }
        });

        rhai_functions::register_all_functions(&mut engine);

        Self {
            engine,
            compiled_filters: self.compiled_filters.clone(),
            compiled_execs: self.compiled_execs.clone(),
            compiled_begin: self.compiled_begin.clone(),
            compiled_end: self.compiled_end.clone(),
            scope_template: self.scope_template.clone(),
            suppress_side_effects,
            conf_map: self.conf_map.clone(),
            state_map: self.state_map.clone(),
            state_available: self.state_available,
            debug_tracker: self.debug_tracker.clone(),
            execution_tracer: self.execution_tracer.clone(),
            use_emoji: self.use_emoji,
        }
    }
}

impl RhaiEngine {
    /// Render a short diagnostic with stage/name, position, snippet, and the raw Rhai message.
    fn format_rhai_diagnostic(
        err: Box<EvalAltResult>,
        stage: &str,
        script_name: &str,
        script_text: &str,
        scope: Option<&Scope>,
        debug_tracker: Option<&DebugTracker>,
        use_emoji: bool,
    ) -> String {
        let call_stack = Self::collect_call_stack(err.as_ref());
        let err_display = format!("{}", err);

        if let Some(tracker) = debug_tracker {
            let enhancer = ErrorEnhancer::new(tracker.config.clone());
            let context = tracker.get_context();
            if let Some(scope) = scope {
                return enhancer.enhance_error(&err, scope, script_text, stage, &context);
            }
        }

        // Basic header
        let mut output = String::new();
        output.push_str(&format!("{} error\n", stage));

        // Position + snippet
        let pos = err.position();
        if let Some(line_num) = pos.line() {
            let col_num = pos.position().unwrap_or(1);
            output.push_str(&format!(
                "  At {}:{} in {}\n",
                line_num, col_num, script_name
            ));
            if let Some(snippet) = Self::render_snippet(
                script_text,
                line_num.saturating_sub(1),
                col_num.saturating_sub(1),
            ) {
                output.push_str(&snippet);
            }
        } else if pos.is_none() {
            output.push_str(&format!("  In {}\n", script_name));
        } else {
            output.push_str(&format!("  At {} in {}\n", pos, script_name));
        }

        // Raw message from Rhai
        output.push_str(&format!("  Rhai: {}\n", err_display));

        if !call_stack.is_empty() {
            output.push_str("  Call stack (most recent first):\n");
            for (func, pos) in call_stack.iter().rev().take(3) {
                output.push_str(&format!("    • {} @ {}\n", func, pos));
            }
        }

        // Generate suggestions even without debug mode (helps users fix errors)
        let config = DebugConfig::new(0); // Minimal config for suggestion generation
        let enhancer = ErrorEnhancer::new(config);
        let suggestion = if let Some(scope) = scope {
            enhancer.generate_suggestions(&err, scope, Some(script_text))
        } else {
            ErrorEnhancer::raw_string_hint(&err, script_text)
        };
        if let Some(suggestion) = suggestion {
            if use_emoji {
                output.push_str(&format!("  💡 {}\n", suggestion));
            } else {
                output.push_str(&format!("  Hint: {}\n", suggestion));
            }
        }

        output
    }

    /// Collect nested function call frames from Rhai errors.
    fn collect_call_stack(err: &EvalAltResult) -> Vec<(String, rhai::Position)> {
        match err {
            EvalAltResult::ErrorInFunctionCall(func, _src, inner, pos) => {
                let mut frames = vec![(func.clone(), *pos)];
                frames.extend(Self::collect_call_stack(inner.as_ref()));
                frames
            }
            EvalAltResult::ErrorInModule(module, inner, pos) => {
                let mut frames = vec![(format!("module {}", module), *pos)];
                frames.extend(Self::collect_call_stack(inner.as_ref()));
                frames
            }
            _ => Vec::new(),
        }
    }

    /// Build a small two-line snippet with a caret under the offending column.
    fn render_snippet(
        script: &str,
        zero_based_line: usize,
        zero_based_col: usize,
    ) -> Option<String> {
        let lines: Vec<&str> = script.lines().collect();
        let line_content = lines.get(zero_based_line)?.trim_end_matches('\r');
        let line_num = zero_based_line + 1;
        let col_num = zero_based_col + 1;
        let gutter_width = line_num.to_string().len();
        let mut snippet = String::new();
        snippet.push_str(&format!(
            "  {line_num:>width$} | {line_content}\n",
            width = gutter_width
        ));
        let caret_padding = " ".repeat(col_num.saturating_sub(1));
        snippet.push_str(&format!(
            "  {empty:>width$} | {caret_padding}^\n",
            empty = "",
            width = gutter_width
        ));
        Some(snippet)
    }

    // Thread-local state management functions
    pub fn set_thread_tracking_state(
        metrics: &HashMap<String, Dynamic>,
        internal: &HashMap<String, Dynamic>,
    ) {
        rhai_functions::tracking::set_thread_tracking_state(metrics);
        rhai_functions::tracking::set_thread_internal_state(internal);
    }

    pub fn get_thread_tracking_state() -> HashMap<String, Dynamic> {
        rhai_functions::tracking::get_thread_tracking_state()
    }

    pub fn get_thread_internal_state() -> HashMap<String, Dynamic> {
        rhai_functions::tracking::get_thread_internal_state()
    }

    fn format_rhai_error(err: Box<EvalAltResult>, script_name: &str, _script_text: &str) -> String {
        match *err {
            EvalAltResult::ErrorParsing(parse_err, pos) => {
                format!("Syntax error in {} at {}: {}", script_name, pos, parse_err)
            }
            EvalAltResult::ErrorRuntime(runtime_err, pos) => {
                format!(
                    "Runtime error in {} at {}: {}",
                    script_name, pos, runtime_err
                )
            }
            EvalAltResult::ErrorVariableNotFound(var, pos) => {
                format!("Variable '{}' not found in {} at {}", var, script_name, pos)
            }
            EvalAltResult::ErrorFunctionNotFound(func, pos) => {
                Self::format_function_not_found_error(func, script_name, pos)
            }
            EvalAltResult::ErrorMismatchDataType(expected, actual, pos) => {
                format!("Type mismatch in {} at {}: expected {}, got {} (this often indicates a function was called with incorrect argument types)", 
                        script_name, pos, expected, actual)
            }
            EvalAltResult::ErrorInFunctionCall(func, _source, inner_err, pos) => {
                let inner_msg = Self::format_rhai_error(inner_err, "function", "");
                format!(
                    "Error in function '{}' in {} at {}: {}",
                    func, script_name, pos, inner_msg
                )
            }
            EvalAltResult::ErrorPropertyNotFound(prop, pos) => {
                format!(
                    "Property '{}' not found in {} at {}",
                    prop, script_name, pos
                )
            }
            EvalAltResult::ErrorIndexNotFound(index, pos) => {
                format!("Index '{}' not found in {} at {}", index, script_name, pos)
            }
            EvalAltResult::ErrorDotExpr(msg, pos) => {
                format!(
                    "Property access error in {} at {}: {}",
                    script_name, pos, msg
                )
            }
            EvalAltResult::ErrorArithmetic(msg, pos) => {
                format!("Arithmetic error in {} at {}: {}", script_name, pos, msg)
            }
            EvalAltResult::ErrorTooManyOperations(pos) => {
                format!("Too many operations in {} at {}", script_name, pos)
            }
            EvalAltResult::ErrorStackOverflow(pos) => {
                format!("Stack overflow in {} at {}", script_name, pos)
            }
            EvalAltResult::ErrorDataTooLarge(msg, pos) => {
                format!("Data too large in {} at {}: {}", script_name, pos, msg)
            }
            EvalAltResult::ErrorTerminated(val, pos) => {
                format!("Script terminated in {} at {}: {}", script_name, pos, val)
            }
            _ => format!("Error in {}: {}", script_name, err),
        }
    }

    fn format_function_not_found_error(
        func_signature: String,
        script_name: &str,
        pos: rhai::Position,
    ) -> String {
        // Extract function name from signature (before the first '(' or space)
        let func_name = if let Some(paren_pos) = func_signature.find('(') {
            &func_signature[..paren_pos]
        } else if let Some(space_pos) = func_signature.find(' ') {
            &func_signature[..space_pos]
        } else {
            &func_signature
        }
        .trim();

        // Check if this looks like a type mismatch rather than a missing function
        let called_types = Self::extract_called_types(&func_signature);
        if Self::is_likely_type_mismatch(&func_signature, func_name) {
            let expected_types = Self::get_expected_function_signature(func_name);
            if !expected_types.is_empty() {
                return format!(
                    "Wrong argument types for '{}' in {} at {}: got {}, expected {}. Note: x.{}() = {}(x)",
                    func_name,
                    script_name,
                    pos,
                    called_types,
                    expected_types,
                    func_name,
                    func_name
                );
            }
        }

        // Fall back to "function not found" with suggestions
        let base_msg = format!(
            "Function '{}' not found in {} at {}",
            func_signature, script_name, pos
        );
        let suggestions = Self::get_function_suggestions(func_name);
        let mut notes = Vec::new();

        if called_types != "unknown types" {
            notes.push(format!("Called with: {}", called_types));
        }

        if Self::signature_has_unit(&called_types) {
            notes.push(
                "One of the arguments is '()' (missing field?). Use e.has(\"field\") or e.get(\"field\", default) before chaining."
                    .to_string(),
            );
        }

        if suggestions.is_empty() {
            notes.push(format!(
                "Note: method calls are sugar—x.{}(y) == {}(x, y)",
                func_name, func_name
            ));
            format!("{}. {}", base_msg, notes.join(" "))
        } else {
            let mut msg = format!("{}. Did you mean: {}", base_msg, suggestions.join(", "));
            if !notes.is_empty() {
                msg.push_str(&format!(" {}", notes.join(" ")));
            }
            msg
        }
    }

    fn is_likely_type_mismatch(func_signature: &str, func_name: &str) -> bool {
        !Self::get_expected_function_signature(func_name).is_empty() && func_signature.contains('(')
    }

    fn extract_called_types(func_signature: &str) -> String {
        if let Some(start) = func_signature.find('(') {
            if let Some(end) = func_signature.rfind(')') {
                return func_signature[start + 1..end].to_string();
            }
        }
        "unknown types".to_string()
    }

    fn signature_has_unit(called_types: &str) -> bool {
        called_types.contains("()")
    }

    fn get_expected_function_signature(func_name: &str) -> String {
        match func_name {
            "extract_regex" => "string, regex_pattern, optional_group_index".to_string(),
            "extract_regexes" => "string, regex_pattern, optional_group_index".to_string(),
            "extract_regex_maps" | "extract_re_maps" => "string, regex_pattern, field".to_string(),
            "split_regex" | "split_re" => "string, regex_pattern".to_string(),
            "replace_regex" | "replace_re" => "string, regex_pattern, replacement".to_string(),
            "before" | "after" => "string, delimiter".to_string(),
            "between" => "string, start_delimiter, end_delimiter".to_string(),
            "starting_with" | "ending_with" => "string, prefix_or_suffix".to_string(),
            "strip" => "string, optional_characters_to_strip".to_string(),
            "join" => "string_separator, array OR array, string_separator".to_string(),
            "extract_ip" | "extract_ips" | "extract_url" | "extract_domain" => "string".to_string(),
            "mask_ip" => "string, optional_octets_to_mask".to_string(),
            "is_private_ip" | "is_digit" => "string".to_string(),
            "parse_json" => "json_string".to_string(),
            "parse_kv" => "string, optional_separator, optional_kv_separator".to_string(),
            "col" => "string, column_selector".to_string(),
            "cols" => "string, column_selectors...".to_string(),
            "status_class" => "status_code_number".to_string(),
            "track_count" => "key".to_string(),
            "track_sum" | "track_min" | "track_max" | "track_avg" => "key, value".to_string(),
            "track_unique" => "key, value".to_string(),
            "track_bucket" => "key, value, bucket_size".to_string(),
            "count" => "string, substring".to_string(),
            // Common string functions that expect strings
            "len" | "trim" => "string".to_string(),
            "contains" | "starts_with" | "ends_with" => "string, substring".to_string(),
            "split" | "replace" => "string, delimiter_or_pattern, optional_replacement".to_string(),
            _ => "".to_string(),
        }
    }

    fn function_catalog() -> Vec<String> {
        // List of common Rhai built-in functions and our custom functions
        vec![
            // String functions
            "lower".to_string(),
            "upper".to_string(),
            "trim".to_string(),
            "len".to_string(),
            "contains".to_string(),
            "starts_with".to_string(),
            "ends_with".to_string(),
            "split".to_string(),
            "replace".to_string(),
            "substring".to_string(),
            "to_string".to_string(),
            "parse".to_string(),
            // Our custom string functions
            "extract_regex".to_string(),
            "extract_regexes".to_string(),
            "extract_regex_maps".to_string(),
            "extract_re_maps".to_string(),
            "split_regex".to_string(),
            "split_re".to_string(),
            "replace_regex".to_string(),
            "replace_re".to_string(),
            "count".to_string(),
            "strip".to_string(),
            "before".to_string(),
            "after".to_string(),
            "between".to_string(),
            "starting_with".to_string(),
            "ending_with".to_string(),
            "is_digit".to_string(),
            "join".to_string(),
            "extract_ip".to_string(),
            "extract_ips".to_string(),
            "mask_ip".to_string(),
            "is_private_ip".to_string(),
            "extract_url".to_string(),
            "extract_domain".to_string(),
            // Math functions
            "abs".to_string(),
            "floor".to_string(),
            "ceil".to_string(),
            "round".to_string(),
            "min".to_string(),
            "max".to_string(),
            "pow".to_string(),
            "sqrt".to_string(),
            // Array functions
            "push".to_string(),
            "pop".to_string(),
            "shift".to_string(),
            "unshift".to_string(),
            "reverse".to_string(),
            "sort".to_string(),
            "clear".to_string(),
            // Map functions
            "keys".to_string(),
            "values".to_string(),
            "remove".to_string(),
            "contains".to_string(),
            // Our custom functions
            "parse_json".to_string(),
            "parse_kv".to_string(),
            "col".to_string(),
            "cols".to_string(),
            "status_class".to_string(),
            "track_count".to_string(),
            "track_sum".to_string(),
            "track_min".to_string(),
            "track_max".to_string(),
            "track_avg".to_string(),
            "track_unique".to_string(),
            "track_bucket".to_string(),
            // Utility functions
            "print".to_string(),
            "debug".to_string(),
            "type_of".to_string(),
            "is_def_fn".to_string(),
        ]
    }

    fn get_function_suggestions(func_name: &str) -> Vec<String> {
        let available_functions = Self::function_catalog();

        // Find functions that are similar to the requested one
        let suggestions: Vec<String> = available_functions
            .iter()
            .filter(|&f| {
                // Check for starts with or contains
                f.starts_with(func_name) || (func_name.len() > 1 && f.contains(func_name))
            })
            .take(3) // Limit to 3 suggestions
            .map(|s| s.to_string())
            .collect();

        // For debugging: always include at least one suggestion if the function name contains common patterns
        if suggestions.is_empty() && func_name.len() > 2 {
            if func_name.contains("extract") {
                return vec![
                    "extract_regex".to_string(),
                    "extract_ip".to_string(),
                    "extract_url".to_string(),
                ];
            } else if func_name.contains("track") {
                return vec![
                    "track_count".to_string(),
                    "track_sum".to_string(),
                    "track_unique".to_string(),
                ];
            } else if func_name.contains("pars") {
                return vec!["parse_json".to_string(), "parse_kv".to_string()];
            }
        }

        suggestions
    }

    pub fn new() -> Self {
        let mut engine = Engine::new();

        // Use Simple optimization, not Full. Full optimization breaks side-effect functions
        // like track_count("key"), print("msg"), emit_each(), etc. by trying to evaluate them at
        // compile time when their arguments are constants. These functions MUST run at runtime.
        engine.set_optimization_level(rhai::OptimizationLevel::Simple);

        // Check for shutdown signal during script execution (cooperative cancellation)
        engine.on_progress(|_| {
            if crate::platform::SHOULD_TERMINATE.load(Ordering::Relaxed) {
                // Abort script execution by returning a termination sentinel
                Some(rhai::Dynamic::UNIT)
            } else {
                None
            }
        });

        // Override the built-in print function to support capture in parallel mode
        // Note: suppress_side_effects is false by default in new()
        engine.on_print(|text| {
            if crate::rhai_functions::strings::is_parallel_mode() {
                // Use both old capture system (for compatibility) and new ordered system
                crate::rhai_functions::strings::capture_print(text.to_string());
                crate::rhai_functions::strings::capture_stdout(text.to_string());
            } else {
                println!("{}", text);
            }
        });

        // Register custom functions for log analysis (includes eprint() for stderr output)
        rhai_functions::register_all_functions(&mut engine);

        // Register variable access callback for tracking functions
        Self::register_variable_resolver(&mut engine);

        let mut scope_template = Scope::new();
        scope_template.push("line", "");
        scope_template.push("e", rhai::Map::new());
        scope_template.push("meta", rhai::Map::new());
        scope_template.push("conf", rhai::Map::new());

        Self {
            engine,
            compiled_filters: Vec::new(),
            compiled_execs: Vec::new(),
            compiled_begin: None,
            compiled_end: None,
            scope_template,
            suppress_side_effects: false,
            conf_map: None,
            state_map: Some(crate::rhai_functions::state::StateMap::new()),
            state_available: true,
            debug_tracker: None,
            execution_tracer: None,
            use_emoji: true,
        }
    }

    pub fn set_use_emoji(&mut self, use_emoji: bool) {
        self.use_emoji = use_emoji;
    }

    pub fn get_execution_tracer(&self) -> &Option<ExecutionTracer> {
        &self.execution_tracer
    }

    /// Get the compiled filters (for benchmarking)
    pub fn compiled_filters(&self) -> &[CompiledExpression] {
        &self.compiled_filters
    }

    pub fn set_state_available(&mut self, available: bool) {
        self.state_available = available;
    }

    fn push_state_to_scope(&self, scope: &mut Scope) {
        if !self.state_available || crate::rhai_functions::strings::is_parallel_mode() {
            scope.push("state", crate::rhai_functions::state::StateNotAvailable);
        } else if let Some(ref state_map) = self.state_map {
            scope.push("state", state_map.clone());
        }
    }

    /// Set up debugging with the provided configuration
    pub fn setup_debugging(&mut self, debug_config: DebugConfig) {
        if !debug_config.is_enabled() {
            return;
        }

        self.debug_tracker = Some(DebugTracker::new(debug_config.clone()));
        self.execution_tracer = Some(ExecutionTracer::new(debug_config.clone()));

        // These unwraps are safe because we just created the debug components above
        let debug_tracker = self
            .debug_tracker
            .as_ref()
            .expect("debug_tracker should be initialized")
            .clone();
        let execution_tracer = self
            .execution_tracer
            .as_ref()
            .expect("execution_tracer should be initialized")
            .clone();
        // Allow deprecated API: register_debugger is marked as volatile/experimental but is the
        // only way to access Rhai's debugging functionality. The API is stable in practice and
        // essential for our debugging features. We'll update when a stable replacement is available.
        #[allow(deprecated)]
        self.engine.register_debugger(
            move |_engine, debugger| {
                // Set up breakpoint-based tracing for enhanced debugging
                if debug_config.trace_events {
                    // Enable step-by-step debugging mode for detailed tracing
                    // Note: Specific breakpoint methods may not be available in current Rhai version
                    // The debugger will still trigger Step events for detailed tracing
                }
                debugger
            },
            move |_context, event, node, source, pos| {
                // Update execution context
                debug_tracker.update_context(Some(pos), source);

                // Enhanced event logging with verbosity levels
                match event {
                    DebuggerEvent::Start => {
                        debug_tracker.log_basic("Script execution started");
                        if debug_tracker.config.verbosity >= 3 {
                            if let Some(src) = source {
                                debug_tracker
                                    .log_step("Starting script", &format!("\"{}\"", src.trim()));
                            }
                        }
                    }
                    DebuggerEvent::End => {
                        debug_tracker.log_basic("Script execution completed");
                    }
                    DebuggerEvent::Step => {
                        // Enhanced step-by-step tracing
                        if debug_tracker.config.verbosity >= 2 {
                            // Use execution tracer for step-level tracing
                            let step_info = format!("Step at {}", pos);
                            if let Some(src) = source {
                                execution_tracer.trace_step(0, &step_info, src);
                            } else {
                                execution_tracer.trace_step(0, &step_info, "unknown");
                            }
                        }

                        if debug_tracker.config.verbosity >= 3 {
                            // Even more detailed tracing for -vvv
                            let step_info = format!("Step at {}", pos);
                            let node_info = format!("{:?}", node);
                            debug_tracker.log_step(&step_info, &node_info);

                            // Use execution tracer for expression-level details
                            if let Some(src) = source {
                                execution_tracer.trace_expression_evaluation(src, "evaluating");
                            }
                        }
                    }
                    DebuggerEvent::BreakPoint(_) => {
                        if debug_tracker.config.verbosity >= 2 {
                            debug_tracker.log_detailed("breakpoint", 0, &format!("hit at {}", pos));
                            // Use execution tracer for breakpoint details
                            if let Some(src) = source {
                                execution_tracer.trace_step(0, "Breakpoint hit", src);
                            }
                        }
                    }
                    // Note: Specific function call events may not be available in current Rhai version
                    // We handle function call tracing through Step events and other mechanisms
                    _ => {
                        // Enhanced event tracing
                        if debug_tracker.config.verbosity >= 3 {
                            let event_name = format!("{:?}", event);
                            debug_tracker.log_step("Debug event", &event_name);

                            // Use execution tracer for detailed event tracking
                            if let Some(src) = source {
                                execution_tracer.trace_step(0, &event_name, src);
                            }
                        }
                    }
                }

                // Update execution context with more details
                if debug_tracker.config.verbosity >= 2 {
                    if let Ok(mut ctx) = debug_tracker.context.lock() {
                        ctx.last_operation = Some(format!("{:?}", event));
                        if let Some(src) = source {
                            ctx.source_snippet = Some(src.to_string());
                        }
                    }
                }

                Ok(DebuggerCommand::Continue)
            },
        );
    }

    /// Set whether to suppress side effects (print, eprint, etc.)
    pub fn set_suppress_side_effects(&mut self, suppress: bool) {
        self.suppress_side_effects = suppress;

        // Set the thread-local flag for eprint and other functions
        crate::rhai_functions::strings::set_suppress_side_effects(suppress);

        // Re-register the print handler with the new suppression setting
        let suppress_copy = suppress;
        self.engine.on_print(move |text| {
            if suppress_copy {
                // Suppress all print output
                return;
            }

            if crate::rhai_functions::strings::is_parallel_mode() {
                crate::rhai_functions::strings::capture_print(text.to_string());
            } else {
                println!("{}", text);
            }
        });
    }

    // Individual compilation methods for pipeline stages
    pub fn compile_filter(&mut self, filter: &str) -> Result<CompiledExpression> {
        self.compile_filter_with_includes(filter, &[])
    }

    pub fn compile_filter_with_includes(
        &mut self,
        filter: &str,
        includes: &[crate::config::IncludeFile],
    ) -> Result<CompiledExpression> {
        let mut ast = self.engine.compile_expression(filter).map_err(|e| {
            let msg = Self::format_rhai_diagnostic(
                e.into(),
                "filter compilation",
                "filter expression",
                filter,
                None,
                None,
                self.use_emoji,
            );
            anyhow::anyhow!(msg)
        })?;

        for include in includes {
            let mut include_ast = self.engine.compile(&include.content).map_err(|e| {
                let msg = Self::format_rhai_diagnostic(
                    e.into(),
                    "filter include compilation",
                    "include script",
                    &include.content,
                    None,
                    None,
                    self.use_emoji,
                );
                anyhow::anyhow!("{} (in {})", msg, include.path)
            })?;

            include_ast.set_source(include.path.clone());

            if !include_ast.statements().is_empty() {
                return Err(anyhow::anyhow!(
                    "--include file '{}' cannot contain statements when used with --filter; only function definitions are allowed",
                    include.path
                ));
            }

            let include_functions = include_ast.clone_functions_only();
            ast = ast.merge(&include_functions);
        }

        let native_predicate = build_native_predicate(&ast);
        let field_accesses = extract_field_accesses(&ast);
        let var_usage = detect_variable_usage(&ast);
        Ok(CompiledExpression {
            ast,
            expr: filter.to_string(),
            field_accesses,
            native_predicate,
            mutates_event: false,
            meta_usage: var_usage.meta_usage,
            uses_meta: var_usage.uses_meta,
            uses_conf: var_usage.uses_conf,
            uses_line: var_usage.uses_line,
        })
    }

    pub fn compile_exec(&mut self, exec: &str) -> Result<CompiledExpression> {
        let ast = self.engine.compile(exec).map_err(|e| {
            let msg = Self::format_rhai_diagnostic(
                e.into(),
                "exec compilation",
                "exec script",
                exec,
                None,
                None,
                self.use_emoji,
            );
            anyhow::anyhow!(msg)
        })?;
        let field_accesses = extract_field_accesses(&ast);
        let var_usage = detect_variable_usage(&ast);
        let mutates_event = expression_mutates_event(&ast, &field_accesses);
        Ok(CompiledExpression {
            ast,
            expr: exec.to_string(),
            field_accesses,
            native_predicate: None,
            mutates_event,
            meta_usage: var_usage.meta_usage,
            uses_meta: var_usage.uses_meta,
            uses_conf: var_usage.uses_conf,
            uses_line: var_usage.uses_line,
        })
    }

    pub fn compile_begin(&mut self, begin: &str) -> Result<CompiledExpression> {
        let ast = self.engine.compile(begin).map_err(|e| {
            let msg = Self::format_rhai_diagnostic(
                e.into(),
                "begin compilation",
                "begin script",
                begin,
                None,
                None,
                self.use_emoji,
            );
            anyhow::anyhow!(msg)
        })?;
        let field_accesses = extract_field_accesses(&ast);
        let var_usage = detect_variable_usage(&ast);
        Ok(CompiledExpression {
            ast,
            expr: begin.to_string(),
            field_accesses,
            native_predicate: None,
            mutates_event: false,
            meta_usage: var_usage.meta_usage,
            uses_meta: var_usage.uses_meta,
            uses_conf: var_usage.uses_conf,
            uses_line: var_usage.uses_line,
        })
    }

    pub fn compile_end(&mut self, end: &str) -> Result<CompiledExpression> {
        let ast = self.engine.compile(end).map_err(|e| {
            let msg = Self::format_rhai_diagnostic(
                e.into(),
                "end compilation",
                "end script",
                end,
                None,
                None,
                self.use_emoji,
            );
            anyhow::anyhow!(msg)
        })?;
        let field_accesses = extract_field_accesses(&ast);
        let var_usage = detect_variable_usage(&ast);
        Ok(CompiledExpression {
            ast,
            expr: end.to_string(),
            field_accesses,
            native_predicate: None,
            mutates_event: false,
            meta_usage: var_usage.meta_usage,
            uses_meta: var_usage.uses_meta,
            uses_conf: var_usage.uses_conf,
            uses_line: var_usage.uses_line,
        })
    }

    pub fn compile_span_close(&mut self, script: &str) -> Result<CompiledExpression> {
        let ast = self.engine.compile(script).map_err(|e| {
            let msg = Self::format_rhai_diagnostic(
                e.into(),
                "span-close compilation",
                "span-close script",
                script,
                None,
                None,
                self.use_emoji,
            );
            anyhow::anyhow!(msg)
        })?;
        let field_accesses = extract_field_accesses(&ast);
        let var_usage = detect_variable_usage(&ast);
        Ok(CompiledExpression {
            ast,
            expr: script.to_string(),
            field_accesses,
            native_predicate: None,
            mutates_event: false,
            meta_usage: var_usage.meta_usage,
            uses_meta: var_usage.uses_meta,
            uses_conf: var_usage.uses_conf,
            uses_line: var_usage.uses_line,
        })
    }

    // Individual execution methods for pipeline stages
    pub fn execute_compiled_filter(
        &mut self,
        compiled: &CompiledExpression,
        event: &Event,
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
    ) -> Result<bool> {
        let mut stats_recorded = false;

        if let Some(native) = &compiled.native_predicate {
            Self::set_thread_tracking_state(metrics, internal);
            debug_stats_increment_events_processed();
            debug_stats_increment_script_executions();
            stats_recorded = true;

            if let Some(result) = native.evaluate(event) {
                if result {
                    debug_stats_increment_events_passed();
                }
                *metrics = Self::get_thread_tracking_state();
                *internal = Self::get_thread_internal_state();
                return Ok(result);
            }
        }

        Self::set_thread_tracking_state(metrics, internal);
        let mut scope = self.create_scope_for_event_optimized(
            event,
            compiled.uses_line,
            compiled.meta_usage,
            compiled.uses_conf,
        );

        // Debug statistics tracking
        if !stats_recorded {
            debug_stats_increment_events_processed();
            debug_stats_increment_script_executions();
        }

        // Add execution tracing for filter execution
        if let Some(ref tracer) = self.execution_tracer {
            let event_num = tracer.next_event();
            let event_data = format!("{:?}", event.fields);

            // Level 2+: Detailed execution tracing
            if tracer.config.verbosity >= 2 {
                tracer.trace_event_start(event_num, &event_data);
                eprintln!("  Script: {}", compiled.expr.trim());
            }

            // Enhanced detailed tracing for -vvv
            if tracer.config.verbosity >= 3 {
                tracer.trace_scope_inspection(&scope);
                tracer.trace_detailed_step(
                    "filter",
                    "evaluation",
                    &compiled.expr,
                    "starting",
                    "script",
                );
            }
        }

        let result = self
            .engine
            .eval_ast_with_scope::<bool>(&mut scope, &compiled.ast)
            .map_err(|e| {
                // Track errors in debug statistics
                debug_stats_increment_errors();

                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "filter",
                    "filter expression",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        // Add execution result tracing
        if let Some(ref tracer) = self.execution_tracer {
            let action = if result { "passed" } else { "filtered out" };
            tracer.trace_event_result(result, action);

            // Enhanced detailed result tracing
            if tracer.config.verbosity >= 3 {
                let result_str = if result { "true" } else { "false" };
                tracer.trace_detailed_step(
                    "filter",
                    "result",
                    &compiled.expr,
                    result_str,
                    "boolean",
                );
            }
        }

        // Track successful events in debug statistics
        if result {
            debug_stats_increment_events_passed();
        }

        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();
        Ok(result)
    }

    pub fn execute_compiled_exec(
        &mut self,
        compiled: &CompiledExpression,
        event: &mut Event,
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
    ) -> Result<()> {
        Self::set_thread_tracking_state(metrics, internal);
        let mut scope = self.create_scope_for_event_optimized(
            event,
            compiled.uses_line,
            compiled.meta_usage,
            compiled.uses_conf,
        );

        // Debug statistics tracking
        debug_stats_increment_script_executions();

        // Add execution tracing for exec execution
        if let Some(ref tracer) = self.execution_tracer {
            let event_num = tracer.next_event();
            let event_data = format!("{:?}", event.fields);

            // Level 2+: Detailed execution tracing
            if tracer.config.verbosity >= 2 {
                tracer.trace_event_start(event_num, &event_data);
                eprintln!("  Script: {}", compiled.expr.trim());
            }

            // Enhanced detailed tracing for -vvv
            if tracer.config.verbosity >= 3 {
                tracer.trace_scope_inspection(&scope);
                tracer.trace_detailed_step(
                    "exec",
                    "transformation",
                    &compiled.expr,
                    "starting",
                    "script",
                );
            }
        }

        let _ = self
            .engine
            .eval_ast_with_scope::<Dynamic>(&mut scope, &compiled.ast)
            .map_err(|e| {
                // Track errors in debug statistics
                debug_stats_increment_errors();

                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "exec",
                    "exec script",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        // Add execution result tracing
        if let Some(ref tracer) = self.execution_tracer {
            tracer.trace_event_result(true, "executed successfully");

            // Enhanced detailed result tracing
            if tracer.config.verbosity >= 3 {
                tracer.trace_detailed_step(
                    "exec",
                    "result",
                    &compiled.expr,
                    "success",
                    "execution",
                );
            }
        }

        if compiled.mutates_event {
            self.update_event_from_scope(event, &scope);
        }
        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();
        Ok(())
    }

    pub fn execute_compiled_begin(
        &mut self,
        compiled: &CompiledExpression,
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
    ) -> Result<rhai::Map> {
        Self::set_thread_tracking_state(metrics, internal);

        // Set begin phase flag to allow read_file/read_lines
        crate::rhai_functions::conf::set_begin_phase(true);

        let mut scope = self.scope_template.clone();

        self.push_state_to_scope(&mut scope);

        let _ = self
            .engine
            .eval_ast_with_scope::<Dynamic>(&mut scope, &compiled.ast)
            .map_err(|e| {
                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "begin",
                    "begin expression",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        // Reset begin phase flag
        crate::rhai_functions::conf::set_begin_phase(false);

        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();

        // Extract the conf map from scope and store it
        let mut conf_map = scope.get_value::<rhai::Map>("conf").unwrap_or_default();

        // Deep freeze the conf map to make it read-only
        crate::rhai_functions::conf::deep_freeze_map(&mut conf_map);

        // Store the frozen conf map
        self.conf_map = Some(conf_map.clone());

        Ok(conf_map)
    }

    pub fn execute_compiled_end(
        &self,
        compiled: &CompiledExpression,
        metrics: &HashMap<String, Dynamic>,
    ) -> Result<()> {
        let mut scope = self.scope_template.clone();
        let mut tracked_map = rhai::Map::new();

        // Convert HashMap to Rhai Map (read-only)
        for (k, v) in metrics.iter() {
            tracked_map.insert(k.clone().into(), v.clone());
        }
        scope.set_value("metrics", tracked_map);

        // Set the frozen conf map (read-only)
        if let Some(ref conf_map) = self.conf_map {
            scope.set_value("conf", conf_map.clone());
        }

        self.push_state_to_scope(&mut scope);

        let _ = self
            .engine
            .eval_ast_with_scope::<Dynamic>(&mut scope, &compiled.ast)
            .map_err(|e| {
                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "end",
                    "end expression",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        Ok(())
    }

    pub fn execute_compiled_span_close(
        &mut self,
        compiled: &CompiledExpression,
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
        span: crate::rhai_functions::span::SpanBinding,
    ) -> Result<()> {
        Self::set_thread_tracking_state(metrics, internal);

        let mut scope = self.scope_template.clone();
        let mut metrics_map = rhai::Map::new();

        for (k, v) in metrics.iter() {
            metrics_map.insert(k.clone().into(), v.clone());
        }
        scope.set_value("metrics", metrics_map);
        scope.push_constant("span", Dynamic::from(span));

        // Set the frozen conf map (read-only)
        if let Some(ref conf_map) = self.conf_map {
            scope.set_value("conf", conf_map.clone());
        }

        self.push_state_to_scope(&mut scope);

        crate::rhai_functions::file_ops::clear_pending_ops();

        let _ = self
            .engine
            .eval_ast_with_scope::<Dynamic>(&mut scope, &compiled.ast)
            .map_err(|e| {
                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "span-close",
                    "span-close script",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        let ops = crate::rhai_functions::file_ops::take_pending_ops();
        crate::rhai_functions::file_ops::execute_ops(&ops)?;

        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();

        Ok(())
    }

    fn register_variable_resolver(_engine: &mut Engine) {
        // For now, keep this empty - we'll implement proper function-based approach
        // Variable resolver is not the right tool for function calls
    }

    // Window-aware execution methods
    pub fn execute_compiled_filter_with_window(
        &mut self,
        compiled: &CompiledExpression,
        event: &Event,
        window: &[Event],
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
    ) -> Result<bool> {
        Self::set_thread_tracking_state(metrics, internal);
        let mut scope = self.create_scope_for_event_with_window(event, window, compiled.meta_usage);

        // Debug statistics tracking
        debug_stats_increment_events_processed();
        debug_stats_increment_script_executions();

        // Add execution tracing for windowed filter execution
        if let Some(ref tracer) = self.execution_tracer {
            let event_num = tracer.next_event();
            let event_data = format!("{:?}", event.fields);

            // Level 2+: Detailed execution tracing
            if tracer.config.verbosity >= 2 {
                tracer.trace_event_start(event_num, &event_data);
                eprintln!(
                    "  Script (windowed, size {}): {}",
                    window.len(),
                    compiled.expr.trim()
                );
            }

            // Enhanced detailed tracing for -vvv
            if tracer.config.verbosity >= 3 {
                tracer.trace_scope_inspection(&scope);
                tracer.trace_detailed_step(
                    "windowed-filter",
                    "evaluation",
                    &compiled.expr,
                    "starting",
                    "script",
                );
                tracer.trace_detailed_step(
                    "windowed-filter",
                    "window-size",
                    &window.len().to_string(),
                    &window.len().to_string(),
                    "size",
                );
            }
        }

        let result = self
            .engine
            .eval_ast_with_scope::<bool>(&mut scope, &compiled.ast)
            .map_err(|e| {
                // Track errors in debug statistics
                debug_stats_increment_errors();

                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "filter",
                    "filter expression",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        // Add execution result tracing
        if let Some(ref tracer) = self.execution_tracer {
            let action = if result { "passed" } else { "filtered out" };
            tracer.trace_event_result(result, action);

            // Enhanced detailed result tracing
            if tracer.config.verbosity >= 3 {
                let result_str = if result { "true" } else { "false" };
                tracer.trace_detailed_step(
                    "windowed-filter",
                    "result",
                    &compiled.expr,
                    result_str,
                    "boolean",
                );
            }
        }

        // Track successful events in debug statistics
        if result {
            debug_stats_increment_events_passed();
        }

        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();
        Ok(result)
    }

    pub fn execute_compiled_exec_with_window(
        &mut self,
        compiled: &CompiledExpression,
        event: &mut Event,
        window: &[Event],
        metrics: &mut HashMap<String, Dynamic>,
        internal: &mut HashMap<String, Dynamic>,
    ) -> Result<()> {
        Self::set_thread_tracking_state(metrics, internal);
        let mut scope = self.create_scope_for_event_with_window(event, window, compiled.meta_usage);

        // Debug statistics tracking
        debug_stats_increment_script_executions();

        // Add execution tracing for windowed exec execution
        if let Some(ref tracer) = self.execution_tracer {
            let event_num = tracer.next_event();
            let event_data = format!("{:?}", event.fields);

            // Level 2+: Detailed execution tracing
            if tracer.config.verbosity >= 2 {
                tracer.trace_event_start(event_num, &event_data);
                eprintln!(
                    "  Script (windowed, size {}): {}",
                    window.len(),
                    compiled.expr.trim()
                );
            }

            // Enhanced detailed tracing for -vvv
            if tracer.config.verbosity >= 3 {
                tracer.trace_scope_inspection(&scope);
                tracer.trace_detailed_step(
                    "windowed-exec",
                    "transformation",
                    &compiled.expr,
                    "starting",
                    "script",
                );
                tracer.trace_detailed_step(
                    "windowed-exec",
                    "window-size",
                    &window.len().to_string(),
                    &window.len().to_string(),
                    "size",
                );
            }
        }

        let _ = self
            .engine
            .eval_ast_with_scope::<Dynamic>(&mut scope, &compiled.ast)
            .map_err(|e| {
                // Track errors in debug statistics
                debug_stats_increment_errors();

                let detailed_msg = Self::format_rhai_diagnostic(
                    e,
                    "exec",
                    "exec script",
                    &compiled.expr,
                    Some(&scope),
                    self.debug_tracker.as_ref(),
                    self.use_emoji,
                );
                anyhow::anyhow!("{}", detailed_msg)
            })?;

        self.assert_conf_not_mutated(&scope)
            .map_err(anyhow::Error::from)?;

        // Add execution result tracing
        if let Some(ref tracer) = self.execution_tracer {
            tracer.trace_event_result(true, "executed successfully");

            // Enhanced detailed result tracing
            if tracer.config.verbosity >= 3 {
                tracer.trace_detailed_step(
                    "windowed-exec",
                    "result",
                    &compiled.expr,
                    "success",
                    "execution",
                );
            }
        }

        if compiled.mutates_event {
            self.update_event_from_scope(event, &scope);
        }
        *metrics = Self::get_thread_tracking_state();
        *internal = Self::get_thread_internal_state();
        Ok(())
    }

    fn assert_conf_not_mutated(&self, scope: &Scope) -> Result<(), ConfMutationError> {
        if let Some(original) = &self.conf_map {
            match scope.get_value::<Map>("conf") {
                Some(conf) if maps_equal(&conf, original) => Ok(()),
                _ => Err(ConfMutationError),
            }
        } else {
            Ok(())
        }
    }

    fn create_scope_for_event(&self, event: &Event) -> Scope<'_> {
        // Full scope creation - includes all variables
        self.create_scope_for_event_optimized(
            event,
            true,
            MetaUsage {
                populate_all: true,
                ..MetaUsage::default()
            },
            true,
        )
    }

    /// Create a scope for event evaluation, optionally skipping unused variables
    fn create_scope_for_event_optimized(
        &self,
        event: &Event,
        needs_line: bool,
        meta_usage: MetaUsage,
        needs_conf: bool,
    ) -> Scope<'_> {
        let mut scope = self.scope_template.clone();

        // Update built-in variables - only if needed
        if needs_line {
            scope.set_value("line", event.original_line.clone());
        }

        // Update event map for fields with invalid identifiers
        // Note: We always build the event map since most scripts use `e.*`
        let mut event_map = rhai::Map::new();
        for (k, v) in &event.fields {
            event_map.insert(k.clone().into(), v.clone());
        }
        scope.set_value("e", event_map);

        // Update metadata - only if needed
        if meta_usage.populate_all || meta_usage.any() {
            let mut meta_map = rhai::Map::new();
            if let Some(line_num) = (meta_usage.populate_all || meta_usage.line_num)
                .then_some(event.line_num)
                .flatten()
            {
                meta_map.insert("line_num".into(), Dynamic::from(line_num as i64));
            }
            if let Some(filename) = (meta_usage.populate_all || meta_usage.filename)
                .then_some(event.filename.as_ref())
                .flatten()
            {
                meta_map.insert("filename".into(), Dynamic::from(filename.clone()));
            }
            if let Some(status) = (meta_usage.populate_all || meta_usage.span_status)
                .then_some(event.span.status)
                .flatten()
            {
                meta_map.insert("span_status".into(), Dynamic::from(status.as_str()));
            }
            if let Some(span_id) = (meta_usage.populate_all || meta_usage.span_id)
                .then_some(event.span.span_id.as_ref())
                .flatten()
            {
                meta_map.insert("span_id".into(), Dynamic::from(span_id.clone()));
            }
            if let Some(span_start) = (meta_usage.populate_all || meta_usage.span_start)
                .then_some(event.span.span_start)
                .flatten()
            {
                meta_map.insert(
                    "span_start".into(),
                    Dynamic::from(DateTimeWrapper::from_utc(span_start)),
                );
            }
            if let Some(span_end) = (meta_usage.populate_all || meta_usage.span_end)
                .then_some(event.span.span_end)
                .flatten()
            {
                meta_map.insert(
                    "span_end".into(),
                    Dynamic::from(DateTimeWrapper::from_utc(span_end)),
                );
            }
            if let Some(parsed_ts) = (meta_usage.populate_all || meta_usage.parsed_ts)
                .then_some(event.parsed_ts)
                .flatten()
            {
                meta_map.insert(
                    "parsed_ts".into(),
                    Dynamic::from(DateTimeWrapper::from_utc(parsed_ts)),
                );
            }

            if meta_usage.populate_all || meta_usage.line {
                meta_map.insert("line".into(), Dynamic::from(event.original_line.clone()));
            }

            scope.set_value("meta", meta_map);
        }

        // Set the frozen conf map - only if needed
        if needs_conf {
            if let Some(ref conf_map) = self.conf_map {
                scope.set_value("conf", conf_map.clone());
            }
        }

        self.push_state_to_scope(&mut scope);

        scope
    }

    fn create_scope_for_event_with_window(
        &self,
        event: &Event,
        window: &[Event],
        meta_usage: MetaUsage,
    ) -> Scope<'_> {
        let mut scope = self.create_scope_for_event_optimized(event, true, meta_usage, true);

        // Add window array to scope
        let window_array: rhai::Array = window
            .iter()
            .map(|event| {
                let mut event_map = rhai::Map::new();
                // Add all event fields to the map
                for (k, v) in &event.fields {
                    event_map.insert(k.clone().into(), v.clone());
                }
                // Add built-in fields
                event_map.insert("line".into(), Dynamic::from(event.original_line.clone()));
                if let Some(line_num) = event.line_num {
                    event_map.insert("line_num".into(), Dynamic::from(line_num as i64));
                }
                if let Some(filename) = &event.filename {
                    event_map.insert("filename".into(), Dynamic::from(filename.clone()));
                }
                if let Some(status) = event.span.status {
                    event_map.insert("span_status".into(), Dynamic::from(status.as_str()));
                }
                if let Some(span_id) = &event.span.span_id {
                    event_map.insert("span_id".into(), Dynamic::from(span_id.clone()));
                }
                if let Some(span_start) = event.span.span_start {
                    event_map.insert(
                        "span_start".into(),
                        Dynamic::from(DateTimeWrapper::from_utc(span_start)),
                    );
                }
                if let Some(span_end) = event.span.span_end {
                    event_map.insert(
                        "span_end".into(),
                        Dynamic::from(DateTimeWrapper::from_utc(span_end)),
                    );
                }
                Dynamic::from(event_map)
            })
            .collect();

        scope.set_value("window", window_array);
        scope
    }

    fn update_event_from_scope(&self, event: &mut Event, scope: &Scope) {
        // Check if entire event 'e' was set to unit () - clear all fields
        if scope.get_value::<()>("e").is_some() {
            event.fields.clear();
            return;
        }

        // Capture mutations made directly to the `e` event map
        if let Some(obj) = scope.get_value::<Map>("e") {
            let original_order: Vec<String> = event.fields.keys().cloned().collect();
            let mut remaining_entries: Vec<(String, Dynamic)> =
                obj.into_iter().map(|(k, v)| (k.into(), v)).collect();

            let mut reordered_fields = IndexMap::with_capacity(remaining_entries.len());

            for key in &original_order {
                if let Some(pos) = remaining_entries.iter().position(|(k, _)| k == key) {
                    let (_, value) = remaining_entries.remove(pos);
                    if value.is::<()>() {
                        continue;
                    }
                    reordered_fields.insert(key.clone(), value);
                }
            }

            for (key, value) in remaining_entries {
                if value.is::<()>() {
                    continue;
                }
                reordered_fields.insert(key, value);
            }

            event.fields = reordered_fields;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{TimeZone, Utc};

    fn build_event_with_line(line: &str) -> Event {
        let mut event = Event::with_capacity(line.to_string(), 1);
        event.set_field("line".to_string(), Dynamic::from(line.to_string()));
        event
    }

    #[test]
    fn assignment_replaces_entire_event_map() {
        let engine = RhaiEngine::new();
        let mut event = build_event_with_line("orig line");
        event.set_field("keep".to_string(), Dynamic::from("value"));

        let mut scope = engine.create_scope_for_event(&event);

        let mut new_map = rhai::Map::new();
        new_map.insert("ts".into(), Dynamic::from("2025-09-22"));
        scope.set_value("e", new_map);

        let mut event_clone = event.clone();
        engine.update_event_from_scope(&mut event_clone, &scope);

        assert!(event_clone.fields.get("line").is_none());
        assert!(event_clone.fields.get("keep").is_none());
        assert_eq!(
            event_clone
                .fields
                .get("ts")
                .and_then(|v| v.clone().try_cast::<String>())
                .as_deref(),
            Some("2025-09-22")
        );
        assert_eq!(event_clone.fields.len(), 1);
    }

    #[test]
    fn unit_values_still_remove_fields() {
        let engine = RhaiEngine::new();
        let mut event = build_event_with_line("orig line");
        event.set_field("msg".to_string(), Dynamic::from("hello"));

        let mut scope = engine.create_scope_for_event(&event);

        let mut updated_map = rhai::Map::new();
        updated_map.insert("msg".into(), Dynamic::from("world"));
        updated_map.insert("line".into(), Dynamic::UNIT);
        scope.set_value("e", updated_map);

        let mut event_clone = event.clone();
        engine.update_event_from_scope(&mut event_clone, &scope);

        assert!(event_clone.fields.get("line").is_none());
        assert_eq!(
            event_clone
                .fields
                .get("msg")
                .and_then(|v| v.clone().try_cast::<String>())
                .as_deref(),
            Some("world")
        );
    }

    #[test]
    fn in_place_mutations_preserve_unchanged_fields() {
        let engine = RhaiEngine::new();
        let mut event = build_event_with_line("orig line");
        event.set_field("level".to_string(), Dynamic::from("INFO"));

        let mut scope = engine.create_scope_for_event(&event);

        // Simulate in-place mutation by starting from the existing map values
        let mut mutated_map = scope.get_value::<Map>("e").unwrap();
        mutated_map.insert("level".into(), Dynamic::from("ERROR"));
        scope.set_value("e", mutated_map);

        let mut event_clone = event.clone();
        engine.update_event_from_scope(&mut event_clone, &scope);

        assert!(event_clone.fields.get("line").is_some());
        assert_eq!(
            event_clone
                .fields
                .get("level")
                .and_then(|v| v.clone().try_cast::<String>())
                .as_deref(),
            Some("ERROR")
        );
    }

    #[test]
    fn update_event_preserves_field_order_and_appends_new_keys() {
        let engine = RhaiEngine::new();
        let mut event = build_event_with_line("orig line");
        event.set_field("z".to_string(), Dynamic::from(1_i64));
        event.set_field("a".to_string(), Dynamic::from(2_i64));
        event.set_field("b".to_string(), Dynamic::from(3_i64));

        let mut scope = engine.create_scope_for_event(&event);

        let mut mutated_map = scope.get_value::<Map>("e").unwrap();
        mutated_map.insert("foo".into(), Dynamic::from(42_i64));
        scope.set_value("e", mutated_map);

        let mut event_clone = event.clone();
        engine.update_event_from_scope(&mut event_clone, &scope);

        let keys: Vec<String> = event_clone.fields.keys().cloned().collect();
        assert_eq!(keys, vec!["line", "z", "a", "b", "foo"]);
    }

    #[test]
    fn meta_includes_parsed_timestamp_before_scripts() {
        let engine = RhaiEngine::new();
        let mut event = build_event_with_line("orig line");
        let ts = Utc.timestamp_opt(1_700_000_000, 123_000_000).unwrap();
        event.parsed_ts = Some(ts);

        let scope = engine.create_scope_for_event(&event);
        let meta = scope.get_value::<Map>("meta").expect("meta map");

        let parsed_ts = meta
            .get("parsed_ts")
            .cloned()
            .expect("parsed_ts should be present in meta");
        let dt = parsed_ts
            .try_cast::<crate::rhai_functions::datetime::DateTimeWrapper>()
            .expect("parsed_ts should be a DateTimeWrapper");
        assert_eq!(dt.inner.to_rfc3339(), ts.to_rfc3339());
    }

    #[test]
    fn meta_omits_parsed_timestamp_when_missing() {
        let engine = RhaiEngine::new();
        let event = build_event_with_line("orig line");

        let scope = engine.create_scope_for_event(&event);
        let meta = scope.get_value::<Map>("meta").expect("meta map");
        assert!(
            !meta.contains_key("parsed_ts"),
            "meta.parsed_ts should be absent when event has no parsed timestamp"
        );
    }

    #[test]
    fn compile_exec_tracks_event_mutation() {
        let mut engine = RhaiEngine::new();

        let read_only = engine
            .compile_exec(r#"track_sum("status_codes", e.status)"#)
            .expect("read-only exec should compile");
        assert!(
            !read_only.mutates_event,
            "read-only exec should skip event write-back"
        );

        let mutating = engine
            .compile_exec(r#"e.level = "ERROR""#)
            .expect("mutating exec should compile");
        assert!(
            mutating.mutates_event,
            "field assignment should require event write-back"
        );

        let replace_map = engine
            .compile_exec("e = ()")
            .expect("whole-map assignment should compile");
        assert!(
            replace_map.mutates_event,
            "whole-map assignment should require event write-back"
        );
    }

    #[test]
    fn compile_filter_tracks_specific_meta_fields() {
        let mut engine = RhaiEngine::new();

        let compiled = engine
            .compile_filter(r#"meta.filename == "app.log""#)
            .expect("meta filter should compile");

        assert!(compiled.uses_meta, "meta filter should use meta");
        assert!(compiled.meta_usage.filename, "filename should be requested");
        assert!(
            !compiled.meta_usage.line,
            "unreferenced meta.line should not be requested"
        );
        assert!(
            !compiled.meta_usage.parsed_ts,
            "unreferenced meta.parsed_ts should not be requested"
        );
    }

    #[test]
    fn render_snippet_marks_correct_line_and_col() {
        let script = "let x = 1;\nlet y = foo(x);\nlet z = y + 1;";
        let snippet = RhaiEngine::render_snippet(script, 1, 7).expect("snippet");
        assert!(snippet.contains("2 | let y = foo(x);"));
        assert!(snippet.contains("^"));
        // caret should land under the 8th character (zero-based 7) of line 2
        let caret_line = snippet.lines().nth(1).unwrap_or_default();
        assert!(caret_line.ends_with("^"));
        assert!(caret_line.contains("|        ^"));
    }

    #[test]
    fn parse_error_suggests_rhai_raw_string_syntax() {
        let mut engine = RhaiEngine::new();
        let err = engine
            .compile_exec("e.line.extract_regex(r\"User (\\\\d+)\")")
            .err()
            .expect("r\"...\" should be invalid in Rhai");
        let msg = err.to_string();
        assert!(
            msg.contains("Rhai raw strings use #\"...\"#"),
            "raw string hint should mention Rhai syntax; got: {msg}"
        );
    }

    #[test]
    fn property_suggestion_shows_available_fields_without_verbose() {
        let config = DebugConfig::new(0);
        let enhancer = ErrorEnhancer::new(config);
        let mut scope = Scope::new();
        let mut e_map = Map::new();
        e_map.insert("status".into(), Dynamic::from("OK"));
        e_map.insert("status_code".into(), Dynamic::from(200_i64));
        scope.push("e", e_map);

        let err = EvalAltResult::ErrorPropertyNotFound("statsu".into(), rhai::Position::NONE);
        let ctx = ExecutionContext::default();
        let out = enhancer.enhance_error(&err, &scope, "e.statsu", "filter", &ctx);

        eprintln!("enhanced error:\n{}", out);
        assert!(
            out.contains("status"),
            "output should surface available fields even when verbosity is zero"
        );
    }

    #[test]
    fn runtime_error_with_unit_type_suggests_get_path() {
        let config = DebugConfig::new(0);
        let enhancer = ErrorEnhancer::new(config);
        let scope = Scope::new();

        let err = EvalAltResult::ErrorRuntime(
            "track_count requires a string key; got ()".into(),
            rhai::Position::NONE,
        );
        let ctx = ExecutionContext::default();
        let out = enhancer.enhance_error(&err, &scope, "track_count(e.endpoint)", "exec", &ctx);

        eprintln!("enhanced error:\n{}", out);
        assert!(
            out.contains("field is missing"),
            "output should explain that () means a missing field"
        );
        assert!(
            out.contains("get_path"),
            "output should suggest get_path for handling missing fields"
        );
        assert!(
            out.contains("has_path"),
            "output should suggest has_path for checking field existence"
        );
    }

    #[test]
    fn native_filter_evaluates_simple_comparisons() {
        let mut engine = RhaiEngine::new();
        let compiled = engine
            .compile_filter("e.level == \"ERROR\" && e.status >= 500")
            .expect("filter should compile");

        assert!(compiled.native_predicate.is_some());

        let mut event = build_event_with_line("line");
        event.set_field("level".to_string(), Dynamic::from("ERROR"));
        event.set_field("status".to_string(), Dynamic::from(500_i64));

        let mut metrics = HashMap::new();
        let mut internal = HashMap::new();
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(result);

        let mut event_nonmatch = event.clone();
        event_nonmatch.set_field("status".to_string(), Dynamic::from(200_i64));
        let result = engine
            .execute_compiled_filter(&compiled, &event_nonmatch, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(!result);
    }

    #[test]
    fn native_filter_handles_string_methods() {
        let mut engine = RhaiEngine::new();

        // String methods now have native predicate support
        let compiled = engine
            .compile_filter("e.level.starts_with(\"ERR\")")
            .expect("filter should compile");
        assert!(
            compiled.native_predicate.is_some(),
            "starts_with should have native predicate"
        );

        // Test contains
        let compiled = engine
            .compile_filter("e.message.contains(\"error\")")
            .expect("filter should compile");
        assert!(
            compiled.native_predicate.is_some(),
            "contains should have native predicate"
        );

        // Test ends_with
        let compiled = engine
            .compile_filter("e.path.ends_with(\".log\")")
            .expect("filter should compile");
        assert!(
            compiled.native_predicate.is_some(),
            "ends_with should have native predicate"
        );

        // Unsupported methods should still fall back to Rhai
        let compiled = engine
            .compile_filter("e.message.to_upper()")
            .expect("filter should compile");
        assert!(
            compiled.native_predicate.is_none(),
            "to_upper should NOT have native predicate"
        );
    }

    #[test]
    fn native_string_methods_evaluate_correctly() {
        let mut engine = RhaiEngine::new();

        // Test starts_with
        let compiled = engine
            .compile_filter("e.level.starts_with(\"ERR\")")
            .expect("filter should compile");

        let mut event = build_event_with_line("test");
        event.set_field("level".to_string(), Dynamic::from("ERROR"));
        let mut metrics = HashMap::new();
        let mut internal = HashMap::new();
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(result, "ERROR should start with ERR");

        event.set_field("level".to_string(), Dynamic::from("WARNING"));
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(!result, "WARNING should not start with ERR");

        // Test contains
        let compiled = engine
            .compile_filter("e.message.contains(\"fail\")")
            .expect("filter should compile");

        event.set_field("message".to_string(), Dynamic::from("connection failed"));
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(result, "'connection failed' should contain 'fail'");

        event.set_field("message".to_string(), Dynamic::from("success"));
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(!result, "'success' should not contain 'fail'");

        // Test ends_with
        let compiled = engine
            .compile_filter("e.path.ends_with(\".log\")")
            .expect("filter should compile");

        event.set_field("path".to_string(), Dynamic::from("/var/log/app.log"));
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(result, "'/var/log/app.log' should end with '.log'");

        event.set_field("path".to_string(), Dynamic::from("/var/log/app.txt"));
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("native filter should succeed");
        assert!(!result, "'/var/log/app.txt' should not end with '.log'");
    }

    #[test]
    fn variable_usage_detection() {
        let mut engine = RhaiEngine::new();

        // Filter using only e.* - should not need meta, conf, or line
        let compiled = engine
            .compile_filter("e.level == \"ERROR\"")
            .expect("filter should compile");
        assert!(!compiled.uses_meta, "simple filter should not use meta");
        assert!(!compiled.uses_conf, "simple filter should not use conf");
        assert!(!compiled.uses_line, "simple filter should not use line");

        // Filter using meta
        let compiled = engine
            .compile_filter("meta.line_num > 100")
            .expect("filter should compile");
        assert!(compiled.uses_meta, "filter with meta.* should use meta");
        assert!(!compiled.uses_conf, "filter should not use conf");

        // Filter using conf
        let compiled = engine
            .compile_filter("conf.threshold > 5")
            .expect("filter should compile");
        assert!(compiled.uses_conf, "filter with conf.* should use conf");
        assert!(!compiled.uses_meta, "filter should not use meta");

        // Filter using line
        let compiled = engine
            .compile_filter("line.len() > 100")
            .expect("filter should compile");
        assert!(compiled.uses_line, "filter with line should use line");

        // Combined usage
        let compiled = engine
            .compile_filter("e.level == \"ERROR\" && meta.line_num > 0")
            .expect("filter should compile");
        assert!(compiled.uses_meta, "combined filter should use meta");
        assert!(!compiled.uses_conf, "combined filter should not use conf");
    }

    #[test]
    fn filter_includes_can_define_helpers() {
        let mut engine = RhaiEngine::new();
        let includes = vec![crate::config::IncludeFile {
            path: "helpers.rhai".to_string(),
            content: "fn is_error(level) { level == \"ERROR\" }".to_string(),
        }];

        let compiled = engine
            .compile_filter_with_includes("is_error(e.level)", &includes)
            .expect("filter should compile with includes");

        let mut event = build_event_with_line("line");
        event.set_field("level".to_string(), Dynamic::from("ERROR"));

        let mut metrics = HashMap::new();
        let mut internal = HashMap::new();
        let result = engine
            .execute_compiled_filter(&compiled, &event, &mut metrics, &mut internal)
            .expect("filter should execute");
        assert!(result);
    }

    #[test]
    fn filter_includes_reject_statements() {
        let mut engine = RhaiEngine::new();
        let includes = vec![crate::config::IncludeFile {
            path: "helpers.rhai".to_string(),
            content: "let x = 1;".to_string(),
        }];

        let err = engine.compile_filter_with_includes("e.level == \"ERROR\"", &includes);
        match err {
            Ok(_) => panic!("filters should reject include statements"),
            Err(err) => {
                assert!(err.to_string().contains("cannot contain statements"));
            }
        }
    }

    #[test]
    fn function_suggestion_offers_len_for_length_typo() {
        let config = DebugConfig::new(0);
        let enhancer = ErrorEnhancer::new(config);
        let scope = Scope::new();
        let err =
            EvalAltResult::ErrorFunctionNotFound("length(string)".into(), rhai::Position::NONE);
        let ctx = ExecutionContext::default();
        let out = enhancer.enhance_error(&err, &scope, "length(s)", "filter", &ctx);
        assert!(
            out.contains("len"),
            "function suggestion should offer len() for length typo; got: {out}"
        );
    }

    #[test]
    fn nested_function_errors_show_call_stack() {
        let inner = Box::new(EvalAltResult::ErrorRuntime(
            "boom".into(),
            rhai::Position::new(3, 1),
        ));
        let mid = Box::new(EvalAltResult::ErrorInFunctionCall(
            "child".into(),
            "".into(),
            inner,
            rhai::Position::new(2, 1),
        ));
        let outer = Box::new(EvalAltResult::ErrorInFunctionCall(
            "parent".into(),
            "".into(),
            mid,
            rhai::Position::new(1, 1),
        ));

        let msg = RhaiEngine::format_rhai_diagnostic(
            outer, "filter", "script", "child()", None, None, true,
        );

        assert!(
            msg.contains("Call stack") && msg.contains("parent") && msg.contains("child"),
            "call stack should include nested function frames; got: {msg}"
        );
    }

    #[test]
    fn unit_arg_suggestion_points_to_missing_field() {
        let msg = RhaiEngine::format_function_not_found_error(
            "foo((), string)".to_string(),
            "script",
            rhai::Position::NONE,
        );
        assert!(
            (msg.contains("missing field") || msg.contains("e.has")) && msg.contains("Called with"),
            "unit arg hint should mention missing field guards and show called types; got: {msg}"
        );
    }

    #[test]
    fn type_mismatch_hints_bool_in_filter() {
        let config = DebugConfig::new(0);
        let enhancer = ErrorEnhancer::new(config);
        let scope = Scope::new();
        let err = EvalAltResult::ErrorMismatchDataType(
            "bool".into(),
            "string".into(),
            rhai::Position::NONE,
        );
        let ctx = ExecutionContext::default();
        let out = enhancer.enhance_error(&err, &scope, "e.level", "filter", &ctx);
        assert!(
            out.contains("Filters must return true/false"),
            "type mismatch in filter should remind about boolean return; got: {out}"
        );
    }
}