rust-rule-engine 1.20.1

A blazing-fast Rust rule engine with RETE algorithm, backward chaining inference, and GRL (Grule Rule Language) syntax. Features: forward/backward chaining, pattern matching, unification, O(1) rule indexing, TMS, expression evaluation, method calls, streaming with Redis state backend, watermarking, and custom functions. Production-ready for business rules, expert systems, real-time stream processing, and decision automation.
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
/// GRL Parser without regex dependency
///
/// This module provides full GRL parsing using only memchr and manual string parsing.
/// It's 4-60x faster than the regex-based GRLParser and has no regex dependency.
use crate::engine::module::{ExportItem, ExportList, ImportType, ItemType, ModuleManager};
use crate::engine::rule::{Condition, ConditionGroup, Rule};
use crate::errors::{Result, RuleEngineError};
use crate::types::{ActionType, Operator, Value};
use chrono::{DateTime, Utc};
use std::collections::HashMap;

use super::literal_search;

/// GRL Parser - No Regex Version
///
/// Parses Grule-like syntax into Rule objects without using regex.
/// This is the recommended parser for new code - it's faster and has fewer dependencies.
pub struct GRLParserNoRegex;

/// Result from parsing GRL with modules
#[derive(Debug, Clone)]
pub struct ParsedGRL {
    /// Parsed rules
    pub rules: Vec<Rule>,
    /// Module manager with configured modules
    pub module_manager: ModuleManager,
    /// Map of rule name to module name
    pub rule_modules: HashMap<String, String>,
}

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

impl ParsedGRL {
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            module_manager: ModuleManager::new(),
            rule_modules: HashMap::new(),
        }
    }
}

/// Parsed rule attributes
#[derive(Debug, Default)]
struct RuleAttributes {
    pub salience: i32,
    pub no_loop: bool,
    pub lock_on_active: bool,
    pub agenda_group: Option<String>,
    pub activation_group: Option<String>,
    pub date_effective: Option<DateTime<Utc>>,
    pub date_expires: Option<DateTime<Utc>>,
}

impl GRLParserNoRegex {
    /// Parse multiple rules from GRL text
    pub fn parse_rules(grl_text: &str) -> Result<Vec<Rule>> {
        let rule_texts = split_into_rules(grl_text);
        let mut rules = Vec::with_capacity(rule_texts.len());

        for rule_text in rule_texts {
            let rule = Self::parse_single_rule(&rule_text)?;
            rules.push(rule);
        }

        Ok(rules)
    }

    /// Parse a single rule from GRL syntax
    pub fn parse_rule(grl_text: &str) -> Result<Rule> {
        Self::parse_single_rule(grl_text)
    }

    /// Parse GRL text with module support
    pub fn parse_with_modules(grl_text: &str) -> Result<ParsedGRL> {
        let mut result = ParsedGRL::new();

        // Split modules and rules
        let (module_texts, rules_text) = split_modules_and_rules(grl_text);

        // Parse modules
        for module_text in module_texts {
            Self::parse_and_register_module(&module_text, &mut result.module_manager)?;
        }

        // Parse rules
        let rules = Self::parse_rules(&rules_text)?;

        // Assign rules to modules
        for rule in rules {
            let module_name = extract_module_from_context(grl_text, &rule.name);
            result
                .rule_modules
                .insert(rule.name.clone(), module_name.clone());

            if let Ok(module) = result.module_manager.get_module_mut(&module_name) {
                module.add_rule(&rule.name);
            }

            result.rules.push(rule);
        }

        Ok(result)
    }

    fn parse_single_rule(grl_text: &str) -> Result<Rule> {
        let cleaned = clean_text(grl_text);

        // Find "rule" keyword
        let rule_pos =
            find_keyword(&cleaned, "rule").ok_or_else(|| RuleEngineError::ParseError {
                message: "Missing 'rule' keyword".to_string(),
            })?;

        let after_rule = cleaned[rule_pos + 4..].trim_start();

        // Extract rule name (quoted or unquoted)
        let (rule_name, after_name) = extract_rule_name(after_rule)?;

        // Find opening brace
        let brace_pos = after_name
            .find('{')
            .ok_or_else(|| RuleEngineError::ParseError {
                message: "Missing opening brace".to_string(),
            })?;

        let attributes_section = &after_name[..brace_pos];
        let body_start = brace_pos + 1;

        // Find matching closing brace
        let body_with_brace = &after_name[brace_pos..];
        let close_pos =
            literal_search::find_matching_brace(body_with_brace, 0).ok_or_else(|| {
                RuleEngineError::ParseError {
                    message: "Missing closing brace".to_string(),
                }
            })?;

        let rule_body = &after_name[body_start..brace_pos + close_pos];

        // Parse attributes
        let attributes = parse_rule_attributes(attributes_section)?;

        // Parse when-then
        let (when_clause, then_clause) = parse_when_then(rule_body)?;

        // Parse conditions and actions
        let conditions = parse_when_clause(&when_clause)?;
        let actions = parse_then_clause(&then_clause)?;

        // Build rule
        let mut rule = Rule::new(rule_name, conditions, actions);
        rule = rule.with_priority(attributes.salience);

        if attributes.no_loop {
            rule = rule.with_no_loop(true);
        }
        if attributes.lock_on_active {
            rule = rule.with_lock_on_active(true);
        }
        if let Some(agenda_group) = attributes.agenda_group {
            rule = rule.with_agenda_group(agenda_group);
        }
        if let Some(activation_group) = attributes.activation_group {
            rule = rule.with_activation_group(activation_group);
        }
        if let Some(date_effective) = attributes.date_effective {
            rule = rule.with_date_effective(date_effective);
        }
        if let Some(date_expires) = attributes.date_expires {
            rule = rule.with_date_expires(date_expires);
        }

        Ok(rule)
    }

    fn parse_and_register_module(module_def: &str, manager: &mut ModuleManager) -> Result<()> {
        let (name, body, _) = parse_defmodule(module_def)?;

        let _ = manager.create_module(&name);
        let module = manager.get_module_mut(&name)?;

        // Parse export directive
        if let Some(export_type) = extract_directive(&body, "export:") {
            let exports = if export_type.trim() == "all" {
                ExportList::All
            } else if export_type.trim() == "none" {
                ExportList::None
            } else {
                ExportList::Specific(vec![ExportItem {
                    item_type: ItemType::All,
                    pattern: export_type.trim().to_string(),
                }])
            };
            module.set_exports(exports);
        }

        // Parse import directives
        for line in body.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("import:") {
                if let Some(import_spec) = extract_directive(trimmed, "import:") {
                    Self::parse_import_spec(&name, &import_spec, manager)?;
                }
            }
        }

        Ok(())
    }

    fn parse_import_spec(
        importing_module: &str,
        spec: &str,
        manager: &mut ModuleManager,
    ) -> Result<()> {
        let parts: Vec<&str> = spec.splitn(2, '(').collect();
        if parts.is_empty() {
            return Ok(());
        }

        let source_module = parts[0].trim().to_string();
        let rest = if parts.len() > 1 { parts[1] } else { "" };

        if rest.contains("rules") {
            manager.import_from(importing_module, &source_module, ImportType::AllRules, "*")?;
        }

        if rest.contains("templates") {
            manager.import_from(
                importing_module,
                &source_module,
                ImportType::AllTemplates,
                "*",
            )?;
        }

        Ok(())
    }
}

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

/// Split GRL text into individual rules
fn split_into_rules(grl_text: &str) -> Vec<String> {
    let mut rules = Vec::new();
    let bytes = grl_text.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        if let Some(rule_pos) = memchr::memmem::find(&bytes[i..], b"rule ") {
            let abs_pos = i + rule_pos;

            // Check word boundary before "rule"
            if abs_pos > 0 && bytes[abs_pos - 1].is_ascii_alphanumeric() {
                i = abs_pos + 1;
                continue;
            }

            // Check if "rule " is inside a comment (look back for // on same line)
            if is_inside_comment(grl_text, abs_pos) {
                i = abs_pos + 5;
                continue;
            }

            if let Some(brace_pos) = memchr::memchr(b'{', &bytes[abs_pos..]) {
                let brace_abs = abs_pos + brace_pos;

                if let Some(close_pos) = literal_search::find_matching_brace(grl_text, brace_abs) {
                    let rule_text = &grl_text[abs_pos..=close_pos];
                    rules.push(rule_text.to_string());
                    i = close_pos + 1;
                    continue;
                }
            }
        }
        break;
    }

    rules
}

/// Check if a position is inside a single-line comment
fn is_inside_comment(text: &str, pos: usize) -> bool {
    // Find the start of the current line
    let bytes = text.as_bytes();
    let mut line_start = pos;
    while line_start > 0 && bytes[line_start - 1] != b'\n' {
        line_start -= 1;
    }

    // Check if there's a // between line_start and pos
    let line_prefix = &text[line_start..pos];
    line_prefix.contains("//")
}

/// Split modules and rules from GRL text
fn split_modules_and_rules(grl_text: &str) -> (Vec<String>, String) {
    let mut modules = Vec::new();
    let mut rules_text = String::new();
    let bytes = grl_text.as_bytes();
    let mut i = 0;
    let mut last_copy = 0;

    while i < bytes.len() {
        if let Some(offset) = memchr::memmem::find(&bytes[i..], b"defmodule ") {
            let abs_pos = i + offset;

            if abs_pos > last_copy {
                rules_text.push_str(&grl_text[last_copy..abs_pos]);
            }

            if let Some(brace_offset) = memchr::memchr(b'{', &bytes[abs_pos..]) {
                let brace_abs = abs_pos + brace_offset;

                if let Some(close_pos) = literal_search::find_matching_brace(grl_text, brace_abs) {
                    let module_text = &grl_text[abs_pos..=close_pos];
                    modules.push(module_text.to_string());
                    i = close_pos + 1;
                    last_copy = i;
                    continue;
                }
            }
        }
        i += 1;
    }

    if last_copy < grl_text.len() {
        rules_text.push_str(&grl_text[last_copy..]);
    }

    (modules, rules_text)
}

/// Clean text by removing comments and joining lines
fn clean_text(text: &str) -> String {
    text.lines()
        .map(|line| {
            // Remove single-line comments
            if let Some(comment_pos) = line.find("//") {
                line[..comment_pos].trim()
            } else {
                line.trim()
            }
        })
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Find keyword at word boundary
fn find_keyword(text: &str, keyword: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let keyword_bytes = keyword.as_bytes();
    let mut pos = 0;

    while let Some(offset) = memchr::memmem::find(&bytes[pos..], keyword_bytes) {
        let abs_pos = pos + offset;

        // Check word boundaries
        let before_ok = abs_pos == 0 || !bytes[abs_pos - 1].is_ascii_alphanumeric();
        let after_pos = abs_pos + keyword_bytes.len();
        let after_ok = after_pos >= bytes.len() || !bytes[after_pos].is_ascii_alphanumeric();

        if before_ok && after_ok {
            return Some(abs_pos);
        }

        pos = abs_pos + 1;
    }

    None
}

/// Extract rule name (quoted or unquoted)
fn extract_rule_name(text: &str) -> Result<(String, &str)> {
    let trimmed = text.trim_start();

    // Try quoted name first
    if trimmed.starts_with('"') {
        if let Some(end_quote) = memchr::memchr(b'"', &trimmed.as_bytes()[1..]) {
            let name = trimmed[1..end_quote + 1].to_string();
            let remaining = &trimmed[end_quote + 2..];
            return Ok((name, remaining));
        }
        return Err(RuleEngineError::ParseError {
            message: "Unclosed quote in rule name".to_string(),
        });
    }

    // Try identifier
    let name_end = trimmed
        .find(|c: char| !c.is_alphanumeric() && c != '_')
        .unwrap_or(trimmed.len());

    if name_end == 0 {
        return Err(RuleEngineError::ParseError {
            message: "Missing rule name".to_string(),
        });
    }

    let name = trimmed[..name_end].to_string();
    let remaining = &trimmed[name_end..];

    Ok((name, remaining))
}

/// Parse rule attributes from the attributes section
fn parse_rule_attributes(attrs: &str) -> Result<RuleAttributes> {
    let mut result = RuleAttributes::default();

    // Remove quoted strings to avoid false matches
    let cleaned = remove_quoted_strings(attrs);

    // Parse salience
    if let Some(salience_pos) = find_keyword(&cleaned, "salience") {
        let after_salience = cleaned[salience_pos + 8..].trim_start();
        let digits: String = after_salience
            .chars()
            .take_while(|c| c.is_ascii_digit() || *c == '-')
            .collect();
        if let Ok(val) = digits.parse::<i32>() {
            result.salience = val;
        }
    }

    // Parse boolean flags
    result.no_loop = has_keyword(&cleaned, "no-loop");
    result.lock_on_active = has_keyword(&cleaned, "lock-on-active");

    // Parse quoted attributes from original (not cleaned)
    result.agenda_group = extract_quoted_attribute(attrs, "agenda-group");
    result.activation_group = extract_quoted_attribute(attrs, "activation-group");

    if let Some(date_str) = extract_quoted_attribute(attrs, "date-effective") {
        result.date_effective = parse_date_string(&date_str).ok();
    }

    if let Some(date_str) = extract_quoted_attribute(attrs, "date-expires") {
        result.date_expires = parse_date_string(&date_str).ok();
    }

    Ok(result)
}

/// Remove quoted strings from text
fn remove_quoted_strings(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut in_string = false;
    let mut escape_next = false;

    for ch in text.chars() {
        if escape_next {
            escape_next = false;
            continue;
        }

        match ch {
            '\\' if in_string => escape_next = true,
            '"' => in_string = !in_string,
            _ if !in_string => result.push(ch),
            _ => {}
        }
    }

    result
}

/// Check if keyword exists at word boundary
fn has_keyword(text: &str, keyword: &str) -> bool {
    find_keyword(text, keyword).is_some()
}

/// Extract quoted attribute value
fn extract_quoted_attribute(text: &str, attr_name: &str) -> Option<String> {
    let attr_pos = find_keyword(text, attr_name)?;
    let after_attr = text[attr_pos + attr_name.len()..].trim_start();

    if after_attr.starts_with('"') {
        let end_quote = memchr::memchr(b'"', &after_attr.as_bytes()[1..])?;
        Some(after_attr[1..end_quote + 1].to_string())
    } else {
        None
    }
}

/// Parse date string
fn parse_date_string(date_str: &str) -> Result<DateTime<Utc>> {
    if let Ok(date) = DateTime::parse_from_rfc3339(date_str) {
        return Ok(date.with_timezone(&Utc));
    }

    let formats = ["%Y-%m-%d", "%Y-%m-%dT%H:%M:%S", "%d-%b-%Y", "%d-%m-%Y"];

    for format in &formats {
        if let Ok(naive_date) = chrono::NaiveDateTime::parse_from_str(date_str, format) {
            return Ok(naive_date.and_utc());
        }
        if let Ok(naive_date) = chrono::NaiveDate::parse_from_str(date_str, format) {
            let datetime =
                naive_date
                    .and_hms_opt(0, 0, 0)
                    .ok_or_else(|| RuleEngineError::ParseError {
                        message: format!("Invalid time for date: {}", naive_date),
                    })?;
            return Ok(datetime.and_utc());
        }
    }

    Err(RuleEngineError::ParseError {
        message: format!("Unable to parse date: {}", date_str),
    })
}

/// Parse when-then sections
fn parse_when_then(body: &str) -> Result<(String, String)> {
    let when_pos = find_keyword(body, "when").ok_or_else(|| RuleEngineError::ParseError {
        message: "Missing 'when' clause".to_string(),
    })?;

    let after_when = &body[when_pos + 4..];

    // Find "then" at the correct nesting level
    let then_pos = find_then_keyword(after_when).ok_or_else(|| RuleEngineError::ParseError {
        message: "Missing 'then' clause".to_string(),
    })?;

    let when_clause = after_when[..then_pos].trim().to_string();
    let then_clause = after_when[then_pos + 4..].trim().to_string();

    Ok((when_clause, then_clause))
}

/// Find "then" keyword at the correct nesting level
fn find_then_keyword(text: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut in_string = false;
    let mut escape_next = false;
    let mut paren_depth: i32 = 0;
    let mut brace_depth: i32 = 0;

    let mut i = 0;
    while i < bytes.len() {
        if escape_next {
            escape_next = false;
            i += 1;
            continue;
        }

        match bytes[i] {
            b'\\' if in_string => escape_next = true,
            b'"' => in_string = !in_string,
            b'(' if !in_string => paren_depth += 1,
            b')' if !in_string => paren_depth = paren_depth.saturating_sub(1),
            b'{' if !in_string => brace_depth += 1,
            b'}' if !in_string => brace_depth = brace_depth.saturating_sub(1),
            b't' if !in_string && paren_depth == 0 && brace_depth == 0 => {
                if i + 4 <= bytes.len() && &bytes[i..i + 4] == b"then" {
                    let before_ok = i == 0 || !bytes[i - 1].is_ascii_alphanumeric();
                    let after_ok = i + 4 >= bytes.len() || !bytes[i + 4].is_ascii_alphanumeric();
                    if before_ok && after_ok {
                        return Some(i);
                    }
                }
            }
            _ => {}
        }
        i += 1;
    }

    None
}

/// Parse defmodule declaration
fn parse_defmodule(text: &str) -> Result<(String, String, usize)> {
    let trimmed = text.trim_start();

    if !trimmed.starts_with("defmodule") {
        return Err(RuleEngineError::ParseError {
            message: "Expected 'defmodule'".to_string(),
        });
    }

    let after_defmodule = trimmed[9..].trim_start();

    let name_end = after_defmodule
        .chars()
        .position(|c| !c.is_alphanumeric() && c != '_')
        .unwrap_or(after_defmodule.len());

    if name_end == 0 {
        return Err(RuleEngineError::ParseError {
            message: "Missing module name".to_string(),
        });
    }

    let name = after_defmodule[..name_end].to_string();

    if !name
        .chars()
        .next()
        .map(|c| c.is_uppercase())
        .unwrap_or(false)
    {
        return Err(RuleEngineError::ParseError {
            message: "Module name must start with uppercase".to_string(),
        });
    }

    let rest = after_defmodule[name_end..].trim_start();
    if !rest.starts_with('{') {
        return Err(RuleEngineError::ParseError {
            message: "Expected '{' after module name".to_string(),
        });
    }

    let brace_pos = trimmed.len() - rest.len();
    let close_pos = literal_search::find_matching_brace(trimmed, brace_pos).ok_or_else(|| {
        RuleEngineError::ParseError {
            message: "Missing closing brace for module".to_string(),
        }
    })?;

    let body = trimmed[brace_pos + 1..close_pos].to_string();

    Ok((name, body, close_pos + 1))
}

/// Extract directive value
fn extract_directive(text: &str, directive: &str) -> Option<String> {
    let pos = text.find(directive)?;
    let after_directive = &text[pos + directive.len()..];

    let end = after_directive
        .find("import:")
        .or_else(|| after_directive.find("export:"))
        .unwrap_or(after_directive.len());

    Some(after_directive[..end].trim().to_string())
}

/// Extract module name from context
fn extract_module_from_context(grl_text: &str, rule_name: &str) -> String {
    let rule_patterns = [
        format!("rule \"{}\"", rule_name),
        format!("rule {}", rule_name),
    ];

    for pattern in &rule_patterns {
        if let Some(rule_pos) = grl_text.find(pattern) {
            let before = &grl_text[..rule_pos];
            if let Some(module_pos) = before.rfind(";; MODULE:") {
                let after_marker = &before[module_pos + 10..];
                if let Some(end_line) = after_marker.find('\n') {
                    let module_line = after_marker[..end_line].trim();
                    if let Some(first_word) = module_line.split_whitespace().next() {
                        return first_word.to_string();
                    }
                }
            }
        }
    }

    "MAIN".to_string()
}

// ============================================================================
// Condition Parsing
// ============================================================================

/// Parse the when clause into a ConditionGroup
fn parse_when_clause(when_clause: &str) -> Result<ConditionGroup> {
    let trimmed = when_clause.trim();

    // Strip outer parentheses if balanced
    let clause = strip_outer_parens(trimmed);

    // Parse OR (lowest precedence)
    if let Some(parts) = split_logical_operator(clause, "||") {
        return parse_or_parts(parts);
    }

    // Parse AND
    if let Some(parts) = split_logical_operator(clause, "&&") {
        return parse_and_parts(parts);
    }

    // Handle NOT
    if clause.trim_start().starts_with('!') {
        let inner = clause.trim_start()[1..].trim();
        let inner_condition = parse_when_clause(inner)?;
        return Ok(ConditionGroup::not(inner_condition));
    }

    // Handle EXISTS
    if clause.trim_start().starts_with("exists(") && clause.trim_end().ends_with(')') {
        let inner = &clause.trim()[7..clause.trim().len() - 1];
        let inner_condition = parse_when_clause(inner)?;
        return Ok(ConditionGroup::exists(inner_condition));
    }

    // Handle FORALL
    if clause.trim_start().starts_with("forall(") && clause.trim_end().ends_with(')') {
        let inner = &clause.trim()[7..clause.trim().len() - 1];
        let inner_condition = parse_when_clause(inner)?;
        return Ok(ConditionGroup::forall(inner_condition));
    }

    // Handle ACCUMULATE
    if clause.trim_start().starts_with("accumulate(") && clause.trim_end().ends_with(')') {
        return parse_accumulate_condition(clause);
    }

    // Handle TEST
    if clause.trim_start().starts_with("test(") && clause.trim_end().ends_with(')') {
        return parse_test_condition(clause);
    }

    // Single condition
    parse_single_condition(clause)
}

/// Strip outer parentheses if they are balanced
fn strip_outer_parens(text: &str) -> &str {
    let trimmed = text.trim();
    if trimmed.starts_with('(') && trimmed.ends_with(')') {
        let inner = &trimmed[1..trimmed.len() - 1];
        if is_balanced_parens(inner) {
            return inner;
        }
    }
    trimmed
}

/// Check if parentheses are balanced
fn is_balanced_parens(text: &str) -> bool {
    let mut count = 0;
    for ch in text.chars() {
        match ch {
            '(' => count += 1,
            ')' => {
                count -= 1;
                if count < 0 {
                    return false;
                }
            }
            _ => {}
        }
    }
    count == 0
}

/// Split by logical operator at top level
fn split_logical_operator(clause: &str, operator: &str) -> Option<Vec<String>> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut paren_count = 0;
    let mut in_string = false;
    let mut chars = clause.chars().peekable();

    let op_chars: Vec<char> = operator.chars().collect();

    while let Some(ch) = chars.next() {
        match ch {
            '"' => {
                in_string = !in_string;
                current.push(ch);
            }
            '(' if !in_string => {
                paren_count += 1;
                current.push(ch);
            }
            ')' if !in_string => {
                paren_count -= 1;
                current.push(ch);
            }
            _ if !in_string && paren_count == 0 => {
                // Check for operator
                if op_chars.len() == 2 && ch == op_chars[0] && chars.peek() == Some(&op_chars[1]) {
                    chars.next();
                    parts.push(current.trim().to_string());
                    current.clear();
                    continue;
                }
                current.push(ch);
            }
            _ => {
                current.push(ch);
            }
        }
    }

    if !current.trim().is_empty() {
        parts.push(current.trim().to_string());
    }

    if parts.len() > 1 {
        Some(parts)
    } else {
        None
    }
}

/// Parse OR parts
fn parse_or_parts(parts: Vec<String>) -> Result<ConditionGroup> {
    let mut conditions = Vec::new();
    for part in parts {
        conditions.push(parse_when_clause(&part)?);
    }

    if conditions.is_empty() {
        return Err(RuleEngineError::ParseError {
            message: "No conditions in OR".to_string(),
        });
    }

    let mut iter = conditions.into_iter();
    let mut result = iter
        .next()
        .expect("Iterator cannot be empty after empty check");
    for condition in iter {
        result = ConditionGroup::or(result, condition);
    }

    Ok(result)
}

/// Parse AND parts
fn parse_and_parts(parts: Vec<String>) -> Result<ConditionGroup> {
    let mut conditions = Vec::new();
    for part in parts {
        conditions.push(parse_when_clause(&part)?);
    }

    if conditions.is_empty() {
        return Err(RuleEngineError::ParseError {
            message: "No conditions in AND".to_string(),
        });
    }

    let mut iter = conditions.into_iter();
    let mut result = iter
        .next()
        .expect("Iterator cannot be empty after empty check");
    for condition in iter {
        result = ConditionGroup::and(result, condition);
    }

    Ok(result)
}

/// Parse single condition like "User.Age >= 18"
fn parse_single_condition(clause: &str) -> Result<ConditionGroup> {
    let trimmed = strip_outer_parens(clause.trim());

    // Check for multifield patterns first
    if let Some(cond) = try_parse_multifield(trimmed)? {
        return Ok(ConditionGroup::single(cond));
    }

    // Check for function call pattern: func(args) op value
    if let Some(cond) = try_parse_function_call(trimmed)? {
        return Ok(ConditionGroup::single(cond));
    }

    // Parse standard condition: field op value
    let (field, op_str, value_str) = split_condition(trimmed)?;

    let operator = Operator::from_str(op_str).ok_or_else(|| RuleEngineError::InvalidOperator {
        operator: op_str.to_string(),
    })?;

    let value = parse_value(value_str)?;

    // Check if field contains arithmetic
    if contains_arithmetic(field) {
        let test_expr = format!("{} {} {}", field, op_str, value_str);
        let condition = Condition::with_test(test_expr, vec![]);
        return Ok(ConditionGroup::single(condition));
    }

    let condition = Condition::new(field.to_string(), operator, value);
    Ok(ConditionGroup::single(condition))
}

/// Try to parse multifield patterns
fn try_parse_multifield(clause: &str) -> Result<Option<Condition>> {
    // Pattern: field.array $?var (collect)
    if clause.contains(" $?") {
        let parts: Vec<&str> = clause.splitn(2, " $?").collect();
        if parts.len() == 2 {
            let field = parts[0].trim().to_string();
            let variable = format!("$?{}", parts[1].trim());
            return Ok(Some(Condition::with_multifield_collect(field, variable)));
        }
    }

    // Pattern: field.array count op value
    if let Some(count_pos) = clause.find(" count ") {
        let field = clause[..count_pos].trim().to_string();
        let rest = clause[count_pos + 7..].trim();

        let (_, op_str, value_str) = split_condition_from_start(rest)?;
        let operator =
            Operator::from_str(op_str).ok_or_else(|| RuleEngineError::InvalidOperator {
                operator: op_str.to_string(),
            })?;
        let value = parse_value(value_str)?;

        return Ok(Some(Condition::with_multifield_count(
            field, operator, value,
        )));
    }

    // Pattern: field.array first [$var]
    if let Some(first_pos) = clause.find(" first") {
        let field = clause[..first_pos].trim().to_string();
        let rest = clause[first_pos + 6..].trim();
        let variable = if rest.starts_with('$') {
            Some(rest.split_whitespace().next().unwrap_or(rest).to_string())
        } else {
            None
        };
        return Ok(Some(Condition::with_multifield_first(field, variable)));
    }

    // Pattern: field.array last [$var]
    if let Some(last_pos) = clause.find(" last") {
        let field = clause[..last_pos].trim().to_string();
        let rest = clause[last_pos + 5..].trim();
        let variable = if rest.starts_with('$') {
            Some(rest.split_whitespace().next().unwrap_or(rest).to_string())
        } else {
            None
        };
        return Ok(Some(Condition::with_multifield_last(field, variable)));
    }

    // Pattern: field.array empty
    if let Some(stripped) = clause.strip_suffix(" empty") {
        let field = stripped.trim().to_string();
        return Ok(Some(Condition::with_multifield_empty(field)));
    }

    // Pattern: field.array not_empty
    if let Some(stripped) = clause.strip_suffix(" not_empty") {
        let field = stripped.trim().to_string();
        return Ok(Some(Condition::with_multifield_not_empty(field)));
    }

    Ok(None)
}

/// Try to parse function call condition
fn try_parse_function_call(clause: &str) -> Result<Option<Condition>> {
    // Look for pattern: identifier(args) operator value
    if let Some(paren_start) = clause.find('(') {
        if paren_start > 0 {
            let func_name = clause[..paren_start].trim();

            // Check it's a valid identifier
            if func_name.chars().all(|c| c.is_alphanumeric() || c == '_')
                && func_name
                    .chars()
                    .next()
                    .map(|c| c.is_alphabetic())
                    .unwrap_or(false)
            {
                // Find matching close paren
                if let Some(paren_end) = find_matching_paren(clause, paren_start) {
                    let args_str = &clause[paren_start + 1..paren_end];
                    let after_paren = clause[paren_end + 1..].trim();

                    // Check if there's an operator after
                    if let Ok((_, op_str, value_str)) = split_condition_from_start(after_paren) {
                        let args: Vec<String> = if args_str.trim().is_empty() {
                            Vec::new()
                        } else {
                            args_str.split(',').map(|s| s.trim().to_string()).collect()
                        };

                        let operator = Operator::from_str(op_str).ok_or_else(|| {
                            RuleEngineError::InvalidOperator {
                                operator: op_str.to_string(),
                            }
                        })?;

                        let value = parse_value(value_str)?;

                        return Ok(Some(Condition::with_function(
                            func_name.to_string(),
                            args,
                            operator,
                            value,
                        )));
                    }
                }
            }
        }
    }

    Ok(None)
}

/// Find matching closing parenthesis
fn find_matching_paren(text: &str, open_pos: usize) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut depth = 1;
    let mut i = open_pos + 1;
    let mut in_string = false;

    while i < bytes.len() {
        match bytes[i] {
            b'"' => in_string = !in_string,
            b'(' if !in_string => depth += 1,
            b')' if !in_string => {
                depth -= 1;
                if depth == 0 {
                    return Some(i);
                }
            }
            _ => {}
        }
        i += 1;
    }

    None
}

/// Split condition into field, operator, value
fn split_condition(clause: &str) -> Result<(&str, &str, &str)> {
    let operators = [
        ">=", "<=", "==", "!=", ">", "<", "contains", "matches", "in",
    ];

    for op in &operators {
        if let Some(op_pos) = find_operator(clause, op) {
            let field = clause[..op_pos].trim();
            let value = clause[op_pos + op.len()..].trim();
            return Ok((field, op, value));
        }
    }

    Err(RuleEngineError::ParseError {
        message: format!("Invalid condition format: {}", clause),
    })
}

/// Split condition starting from the beginning (for partial parsing)
fn split_condition_from_start(text: &str) -> Result<(&str, &str, &str)> {
    let operators = [">=", "<=", "==", "!=", ">", "<", "contains", "matches"];

    for op in &operators {
        if let Some(stripped) = text.strip_prefix(op) {
            let value = stripped.trim();
            return Ok(("", op, value));
        }
    }

    // Try to find operator in text
    split_condition(text)
}

/// Find operator position (not inside strings or brackets)
fn find_operator(text: &str, op: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let op_bytes = op.as_bytes();
    let mut in_string = false;
    let mut bracket_depth = 0;
    let mut i = 0;

    while i + op_bytes.len() <= bytes.len() {
        if bytes[i] == b'"' {
            in_string = !in_string;
            i += 1;
            continue;
        }

        if !in_string {
            if bytes[i] == b'[' {
                bracket_depth += 1;
            } else if bytes[i] == b']' {
                bracket_depth = bracket_depth.saturating_sub(1);
            }
        }

        if !in_string && bracket_depth == 0 && &bytes[i..i + op_bytes.len()] == op_bytes {
            // For keyword operators, check word boundaries
            if let Some(first_char) = op.chars().next() {
                if first_char.is_alphabetic() {
                    let before_ok = i == 0 || !bytes[i - 1].is_ascii_alphanumeric();
                    let after_ok = i + op_bytes.len() >= bytes.len()
                        || !bytes[i + op_bytes.len()].is_ascii_alphanumeric();
                    if before_ok && after_ok {
                        return Some(i);
                    }
                } else {
                    return Some(i);
                }
            } else {
                // Empty operator string - shouldn't happen but handle gracefully
                return Some(i);
            }
        }

        i += 1;
    }

    None
}

/// Check if string contains arithmetic operators
fn contains_arithmetic(s: &str) -> bool {
    s.contains('+') || s.contains('-') || s.contains('*') || s.contains('/') || s.contains('%')
}

/// Parse test condition
fn parse_test_condition(clause: &str) -> Result<ConditionGroup> {
    let trimmed = clause.trim();
    let inner = &trimmed[5..trimmed.len() - 1]; // Remove "test(" and ")"

    // Check if it's a function call: test(funcName(args))
    if let Some(paren_pos) = inner.find('(') {
        if let Some(close_paren) = find_matching_paren(inner, paren_pos) {
            let func_name = inner[..paren_pos].trim().to_string();
            let args_str = &inner[paren_pos + 1..close_paren];

            let args: Vec<String> = if args_str.trim().is_empty() {
                Vec::new()
            } else {
                args_str.split(',').map(|s| s.trim().to_string()).collect()
            };

            let condition = Condition::with_test(func_name, args);
            return Ok(ConditionGroup::single(condition));
        }
    }

    // Otherwise treat the whole thing as an expression
    let condition = Condition::with_test(inner.trim().to_string(), vec![]);
    Ok(ConditionGroup::single(condition))
}

/// Parse accumulate condition
fn parse_accumulate_condition(clause: &str) -> Result<ConditionGroup> {
    let trimmed = clause.trim();
    let inner = &trimmed[11..trimmed.len() - 1]; // Remove "accumulate(" and ")"

    // Split by comma at top level
    let parts = split_top_level_comma(inner)?;

    if parts.len() != 2 {
        return Err(RuleEngineError::ParseError {
            message: format!("Expected 2 parts in accumulate, got {}", parts.len()),
        });
    }

    let (source_pattern, extract_field, source_conditions) = parse_accumulate_pattern(&parts[0])?;
    let (function, function_arg) = parse_accumulate_function(&parts[1])?;

    Ok(ConditionGroup::accumulate(
        "$result".to_string(),
        source_pattern,
        extract_field,
        source_conditions,
        function,
        function_arg,
    ))
}

/// Split by comma at top level
fn split_top_level_comma(text: &str) -> Result<Vec<String>> {
    let mut parts = Vec::new();
    let mut current = String::new();
    let mut paren_depth = 0;
    let mut in_string = false;

    for ch in text.chars() {
        match ch {
            '"' => {
                in_string = !in_string;
                current.push(ch);
            }
            '(' if !in_string => {
                paren_depth += 1;
                current.push(ch);
            }
            ')' if !in_string => {
                paren_depth -= 1;
                current.push(ch);
            }
            ',' if !in_string && paren_depth == 0 => {
                parts.push(current.trim().to_string());
                current.clear();
            }
            _ => {
                current.push(ch);
            }
        }
    }

    if !current.trim().is_empty() {
        parts.push(current.trim().to_string());
    }

    Ok(parts)
}

/// Parse accumulate pattern
fn parse_accumulate_pattern(pattern: &str) -> Result<(String, String, Vec<String>)> {
    let pattern = pattern.trim();

    let paren_pos = pattern
        .find('(')
        .ok_or_else(|| RuleEngineError::ParseError {
            message: format!("Missing '(' in accumulate pattern: {}", pattern),
        })?;

    let source_pattern = pattern[..paren_pos].trim().to_string();

    if !pattern.ends_with(')') {
        return Err(RuleEngineError::ParseError {
            message: format!("Missing ')' in accumulate pattern: {}", pattern),
        });
    }

    let inner = &pattern[paren_pos + 1..pattern.len() - 1];
    let parts = split_top_level_comma(inner)?;

    let mut extract_field = String::new();
    let mut source_conditions = Vec::new();

    for part in parts {
        let part = part.trim();

        if part.contains(':') && part.starts_with('$') {
            if let Some(colon_pos) = part.find(':') {
                extract_field = part[colon_pos + 1..].trim().to_string();
            }
        } else if part.contains("==")
            || part.contains("!=")
            || part.contains(">=")
            || part.contains("<=")
            || part.contains('>')
            || part.contains('<')
        {
            source_conditions.push(part.to_string());
        }
    }

    Ok((source_pattern, extract_field, source_conditions))
}

/// Parse accumulate function
fn parse_accumulate_function(func_str: &str) -> Result<(String, String)> {
    let func_str = func_str.trim();

    let paren_pos = func_str
        .find('(')
        .ok_or_else(|| RuleEngineError::ParseError {
            message: format!("Missing '(' in accumulate function: {}", func_str),
        })?;

    let function_name = func_str[..paren_pos].trim().to_string();

    if !func_str.ends_with(')') {
        return Err(RuleEngineError::ParseError {
            message: format!("Missing ')' in accumulate function: {}", func_str),
        });
    }

    let args = func_str[paren_pos + 1..func_str.len() - 1]
        .trim()
        .to_string();

    Ok((function_name, args))
}

// ============================================================================
// Value Parsing
// ============================================================================

/// Parse array literal: ["value1", "value2", 123, true]
fn parse_array_literal(array_str: &str) -> Result<Value> {
    let trimmed = array_str.trim();

    // Remove surrounding brackets
    if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
        return Err(RuleEngineError::ParseError {
            message: format!("Invalid array literal: {}", array_str),
        });
    }

    let inner = &trimmed[1..trimmed.len() - 1].trim();

    // Empty array
    if inner.is_empty() {
        return Ok(Value::Array(Vec::new()));
    }

    // Split by comma at top level
    let elements = split_top_level_comma(inner)?;

    let mut array = Vec::new();
    for element in elements {
        let value = parse_value(element.trim())?;
        array.push(value);
    }

    Ok(Value::Array(array))
}

/// Parse a value string into a Value
fn parse_value(value_str: &str) -> Result<Value> {
    let trimmed = value_str.trim();

    // Array literal: ["value1", "value2", ...]
    if trimmed.starts_with('[') && trimmed.ends_with(']') {
        return parse_array_literal(trimmed);
    }

    // String literal
    if (trimmed.starts_with('"') && trimmed.ends_with('"'))
        || (trimmed.starts_with('\'') && trimmed.ends_with('\''))
    {
        let unquoted = &trimmed[1..trimmed.len() - 1];
        return Ok(Value::String(unquoted.to_string()));
    }

    // Boolean
    if trimmed.eq_ignore_ascii_case("true") {
        return Ok(Value::Boolean(true));
    }
    if trimmed.eq_ignore_ascii_case("false") {
        return Ok(Value::Boolean(false));
    }

    // Null
    if trimmed.eq_ignore_ascii_case("null") {
        return Ok(Value::Null);
    }

    // Integer
    if let Ok(int_val) = trimmed.parse::<i64>() {
        return Ok(Value::Integer(int_val));
    }

    // Float
    if let Ok(float_val) = trimmed.parse::<f64>() {
        return Ok(Value::Number(float_val));
    }

    // Expression (contains arithmetic or field reference)
    if is_expression(trimmed) {
        return Ok(Value::Expression(trimmed.to_string()));
    }

    // Field reference
    if trimmed.contains('.') {
        return Ok(Value::String(trimmed.to_string()));
    }

    // Variable/identifier
    if is_identifier(trimmed) {
        return Ok(Value::Expression(trimmed.to_string()));
    }

    // Default to string
    Ok(Value::String(trimmed.to_string()))
}

/// Check if string is a valid identifier
fn is_identifier(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }

    let first = s.chars().next().expect("Cannot be empty after empty check");
    if !first.is_alphabetic() && first != '_' {
        return false;
    }

    s.chars().all(|c| c.is_alphanumeric() || c == '_')
}

/// Check if string is an expression
fn is_expression(s: &str) -> bool {
    let has_operator =
        s.contains('+') || s.contains('-') || s.contains('*') || s.contains('/') || s.contains('%');
    let has_field_ref = s.contains('.');
    let has_spaces = s.contains(' ');

    has_operator && (has_field_ref || has_spaces)
}

// ============================================================================
// Action Parsing
// ============================================================================

/// Parse the then clause into actions
fn parse_then_clause(then_clause: &str) -> Result<Vec<ActionType>> {
    let statements: Vec<&str> = then_clause
        .split(';')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .collect();

    let mut actions = Vec::new();

    for statement in statements {
        let action = parse_action_statement(statement)?;
        actions.push(action);
    }

    Ok(actions)
}

/// Parse a single action statement
fn parse_action_statement(statement: &str) -> Result<ActionType> {
    let trimmed = statement.trim();

    // Method call: $Object.method(args)
    if trimmed.starts_with('$') && trimmed.contains('.') {
        if let Some(action) = try_parse_method_call(trimmed)? {
            return Ok(action);
        }
    }

    // Compound assignment: field += value
    if let Some(pos) = trimmed.find("+=") {
        let field = trimmed[..pos].trim().to_string();
        let value_str = trimmed[pos + 2..].trim();
        let value = parse_value(value_str)?;
        return Ok(ActionType::Append { field, value });
    }

    // Assignment: field = value
    if let Some(eq_pos) = find_assignment_operator(trimmed) {
        let field = trimmed[..eq_pos].trim().to_string();
        let value_str = trimmed[eq_pos + 1..].trim();
        let value = parse_value(value_str)?;
        return Ok(ActionType::Set { field, value });
    }

    // Function call: funcName(args)
    if let Some(paren_pos) = trimmed.find('(') {
        if trimmed.ends_with(')') {
            let func_name = trimmed[..paren_pos].trim();
            let args_str = &trimmed[paren_pos + 1..trimmed.len() - 1];

            return parse_function_action(func_name, args_str);
        }
    }

    // Unknown statement
    Ok(ActionType::Custom {
        action_type: "statement".to_string(),
        params: {
            let mut params = HashMap::new();
            params.insert("statement".to_string(), Value::String(trimmed.to_string()));
            params
        },
    })
}

/// Find assignment operator (=) but not == or !=
fn find_assignment_operator(text: &str) -> Option<usize> {
    let bytes = text.as_bytes();
    let mut in_string = false;
    let mut i = 0;

    while i < bytes.len() {
        if bytes[i] == b'"' {
            in_string = !in_string;
            i += 1;
            continue;
        }

        if !in_string && bytes[i] == b'=' {
            // Check it's not == or !=
            let is_double = i + 1 < bytes.len() && bytes[i + 1] == b'=';
            let is_not_eq = i > 0 && bytes[i - 1] == b'!';
            let is_compound = i > 0
                && (bytes[i - 1] == b'+'
                    || bytes[i - 1] == b'-'
                    || bytes[i - 1] == b'*'
                    || bytes[i - 1] == b'/'
                    || bytes[i - 1] == b'%');

            if !is_double && !is_not_eq && !is_compound {
                return Some(i);
            }
        }

        i += 1;
    }

    None
}

/// Try to parse method call
fn try_parse_method_call(text: &str) -> Result<Option<ActionType>> {
    // Pattern: $Object.method(args)
    let dot_pos = match text.find('.') {
        Some(pos) => pos,
        None => return Ok(None),
    };
    let object = text[1..dot_pos].to_string(); // Skip $

    let rest = &text[dot_pos + 1..];
    let paren_pos = match rest.find('(') {
        Some(pos) => pos,
        None => return Ok(None),
    };
    let method = rest[..paren_pos].to_string();

    if !rest.ends_with(')') {
        return Ok(None);
    }

    let args_str = &rest[paren_pos + 1..rest.len() - 1];
    let args = parse_method_args(args_str)?;

    Ok(Some(ActionType::MethodCall {
        object,
        method,
        args,
    }))
}

/// Parse method arguments
fn parse_method_args(args_str: &str) -> Result<Vec<Value>> {
    if args_str.trim().is_empty() {
        return Ok(Vec::new());
    }

    let parts = split_top_level_comma(args_str)?;
    let mut args = Vec::new();

    for part in parts {
        let trimmed = part.trim();

        // Handle arithmetic expressions
        if contains_arithmetic(trimmed) {
            args.push(Value::String(trimmed.to_string()));
        } else {
            args.push(parse_value(trimmed)?);
        }
    }

    Ok(args)
}

/// Parse function-style action
fn parse_function_action(func_name: &str, args_str: &str) -> Result<ActionType> {
    match func_name.to_lowercase().as_str() {
        "retract" => {
            let object = args_str.trim().trim_start_matches('$').to_string();
            Ok(ActionType::Retract { object })
        }
        "log" => {
            let message = if args_str.is_empty() {
                "Log message".to_string()
            } else {
                let value = parse_value(args_str.trim())?;
                value.to_string()
            };
            Ok(ActionType::Log { message })
        }
        "activateagendagroup" | "activate_agenda_group" => {
            if args_str.is_empty() {
                return Err(RuleEngineError::ParseError {
                    message: "ActivateAgendaGroup requires agenda group name".to_string(),
                });
            }
            let value = parse_value(args_str.trim())?;
            let group = match value {
                Value::String(s) => s,
                _ => value.to_string(),
            };
            Ok(ActionType::ActivateAgendaGroup { group })
        }
        "schedulerule" | "schedule_rule" => {
            let parts = split_top_level_comma(args_str)?;
            if parts.len() != 2 {
                return Err(RuleEngineError::ParseError {
                    message: "ScheduleRule requires delay_ms and rule_name".to_string(),
                });
            }

            let delay_ms = parse_value(parts[0].trim())?;
            let rule_name = parse_value(parts[1].trim())?;

            let delay_ms = match delay_ms {
                Value::Integer(i) => i as u64,
                Value::Number(f) => f as u64,
                _ => {
                    return Err(RuleEngineError::ParseError {
                        message: "ScheduleRule delay_ms must be a number".to_string(),
                    })
                }
            };

            let rule_name = match rule_name {
                Value::String(s) => s,
                _ => rule_name.to_string(),
            };

            Ok(ActionType::ScheduleRule {
                delay_ms,
                rule_name,
            })
        }
        "completeworkflow" | "complete_workflow" => {
            if args_str.is_empty() {
                return Err(RuleEngineError::ParseError {
                    message: "CompleteWorkflow requires workflow_id".to_string(),
                });
            }
            let value = parse_value(args_str.trim())?;
            let workflow_name = match value {
                Value::String(s) => s,
                _ => value.to_string(),
            };
            Ok(ActionType::CompleteWorkflow { workflow_name })
        }
        "setworkflowdata" | "set_workflow_data" => {
            let data_str = args_str.trim();
            if let Some(eq_pos) = data_str.find('=') {
                let key = data_str[..eq_pos].trim().trim_matches('"').to_string();
                let value_str = data_str[eq_pos + 1..].trim();
                let value = parse_value(value_str)?;
                Ok(ActionType::SetWorkflowData { key, value })
            } else {
                Err(RuleEngineError::ParseError {
                    message: "SetWorkflowData data must be in key=value format".to_string(),
                })
            }
        }
        _ => {
            // Custom function
            let params = if args_str.is_empty() {
                HashMap::new()
            } else {
                let parts = split_top_level_comma(args_str)?;
                let mut params = HashMap::new();
                for (i, part) in parts.iter().enumerate() {
                    let value = parse_value(part.trim())?;
                    params.insert(i.to_string(), value);
                }
                params
            };

            Ok(ActionType::Custom {
                action_type: func_name.to_string(),
                params,
            })
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_parse_simple_rule() {
        let grl = r#"
        rule "CheckAge" salience 10 {
            when
                User.Age >= 18
            then
                log("User is adult");
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules.len(), 1);
        let rule = &rules[0];
        assert_eq!(rule.name, "CheckAge");
        assert_eq!(rule.salience, 10);
        assert_eq!(rule.actions.len(), 1);
    }

    #[test]
    fn test_parse_complex_condition() {
        let grl = r#"
        rule "ComplexRule" {
            when
                User.Age >= 18 && User.Country == "US"
            then
                User.Qualified = true;
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].name, "ComplexRule");
    }

    #[test]
    fn test_parse_no_loop_attribute() {
        let grl = r#"
        rule "NoLoopRule" no-loop salience 15 {
            when
                User.Score < 100
            then
                User.Score = 50;
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert!(rules[0].no_loop);
        assert_eq!(rules[0].salience, 15);
    }

    #[test]
    fn test_parse_or_condition() {
        let grl = r#"
        rule "OrRule" {
            when
                User.Status == "active" || User.Status == "premium"
            then
                User.Valid = true;
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules.len(), 1);
    }

    #[test]
    fn test_parse_exists_pattern() {
        let grl = r#"
        rule "ExistsRule" {
            when
                exists(Customer.tier == "VIP")
            then
                System.premium = true;
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules.len(), 1);

        match &rules[0].conditions {
            ConditionGroup::Exists(_) => {}
            _ => panic!("Expected EXISTS condition"),
        }
    }

    #[test]
    fn test_parse_multiple_rules() {
        let grl = r#"
        rule "Rule1" { when A > 1 then B = 2; }
        rule "Rule2" { when C < 3 then D = 4; }
        rule "Rule3" { when E == 5 then F = 6; }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules.len(), 3);
        assert_eq!(rules[0].name, "Rule1");
        assert_eq!(rules[1].name, "Rule2");
        assert_eq!(rules[2].name, "Rule3");
    }

    #[test]
    fn test_parse_assignment_action() {
        let grl = r#"
        rule "SetRule" {
            when
                X > 0
            then
                Y = 100;
                Z = "hello";
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();
        assert_eq!(rules[0].actions.len(), 2);

        match &rules[0].actions[0] {
            ActionType::Set { field, value } => {
                assert_eq!(field, "Y");
                assert_eq!(*value, Value::Integer(100));
            }
            _ => panic!("Expected Set action"),
        }
    }

    #[test]
    fn test_parse_append_action() {
        let grl = r#"
        rule "AppendRule" {
            when
                X > 0
            then
                Items += "new_item";
        }
        "#;

        let rules = GRLParserNoRegex::parse_rules(grl).unwrap();

        match &rules[0].actions[0] {
            ActionType::Append { field, value } => {
                assert_eq!(field, "Items");
                assert_eq!(*value, Value::String("new_item".to_string()));
            }
            _ => panic!("Expected Append action"),
        }
    }

    #[test]
    fn test_parse_in_operator() {
        let grl = r#"
        rule "TestInOperator" {
            when
                User.role in ["admin", "moderator", "vip"]
            then
                User.access = "granted";
        }
        "#;

        let result = GRLParserNoRegex::parse_rules(grl);
        match result {
            Ok(rules) => {
                assert_eq!(rules.len(), 1);
                assert_eq!(rules[0].name, "TestInOperator");

                // Check the condition
                match &rules[0].conditions {
                    ConditionGroup::Single(cond) => {
                        assert_eq!(cond.field, "User.role");
                        assert_eq!(cond.operator, crate::types::Operator::In);
                        // Value should be an array
                        match &cond.value {
                            Value::Array(arr) => {
                                assert_eq!(arr.len(), 3);
                            }
                            _ => panic!("Expected Array value, got {:?}", cond.value),
                        }
                    }
                    _ => panic!("Expected Single condition"),
                }
            }
            Err(e) => {
                panic!("Failed to parse 'in' operator: {}", e);
            }
        }
    }
}