wellformed 0.1.0

Declarative form validation schemas with transforms, constraints, and codegen
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
//! Date validation predicates.
//!
//! Validates dates, date ranges, and date comparisons for tax forms.

use super::registry::{NamedPredicate, PredicateRegistry};
use serde_json::Value;
use std::sync::Arc;

/// Register all date predicates.
pub fn register_date_predicates(registry: &mut PredicateRegistry) {
    registry.register(Arc::new(IsDatePredicate));
    registry.register(Arc::new(IsTimePredicate));
    registry.register(Arc::new(IsIsoDatetimePredicate));
    registry.register(Arc::new(IsTaxYearPredicate));
    registry.register(Arc::new(DateInRangePredicate));
    registry.register(Arc::new(DateBeforePredicate));
    registry.register(Arc::new(DateAfterPredicate));
    registry.register(Arc::new(TimeBeforePredicate));
    registry.register(Arc::new(TimeAfterPredicate));
    registry.register(Arc::new(TimeInRangePredicate));
}

// ============================================================================
// Date Parsing Helpers
// ============================================================================

/// Parsed date components (year, month, day).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct ParsedDate {
    year: u32,
    month: u32,
    day: u32,
}

impl ParsedDate {
    /// Parse a date string in various formats.
    ///
    /// Supported formats:
    /// - MM/DD/YYYY (US format)
    /// - YYYY-MM-DD (ISO 8601)
    /// - MM-DD-YYYY
    /// - MMDDYYYY (no separators)
    fn parse(s: &str) -> Option<Self> {
        let s = s.trim();

        // Try MM/DD/YYYY or MM-DD-YYYY
        if s.len() == 10 && (s.chars().nth(2) == Some('/') || s.chars().nth(2) == Some('-')) {
            let sep = s.chars().nth(2).unwrap();
            if s.chars().nth(5) == Some(sep) {
                let parts: Vec<&str> = s.split(sep).collect();
                if parts.len() == 3 {
                    let month: u32 = parts[0].parse().ok()?;
                    let day: u32 = parts[1].parse().ok()?;
                    let year: u32 = parts[2].parse().ok()?;
                    return Self::validate(year, month, day);
                }
            }
        }

        // Try YYYY-MM-DD (ISO 8601)
        if s.len() == 10 && s.chars().nth(4) == Some('-') && s.chars().nth(7) == Some('-') {
            let parts: Vec<&str> = s.split('-').collect();
            if parts.len() == 3 {
                let year: u32 = parts[0].parse().ok()?;
                let month: u32 = parts[1].parse().ok()?;
                let day: u32 = parts[2].parse().ok()?;
                return Self::validate(year, month, day);
            }
        }

        // Try MMDDYYYY (no separators)
        if s.len() == 8 && s.chars().all(|c| c.is_ascii_digit()) {
            let month: u32 = s[0..2].parse().ok()?;
            let day: u32 = s[2..4].parse().ok()?;
            let year: u32 = s[4..8].parse().ok()?;
            return Self::validate(year, month, day);
        }

        None
    }

    /// Validate and create a ParsedDate if the date is valid.
    fn validate(year: u32, month: u32, day: u32) -> Option<Self> {
        // Basic range checks
        if !(1..=12).contains(&month) {
            return None;
        }
        if day < 1 {
            return None;
        }

        // Days in month (accounting for leap years)
        let days_in_month = match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => {
                if is_leap_year(year) {
                    29
                } else {
                    28
                }
            }
            _ => return None,
        };

        if day > days_in_month {
            return None;
        }

        Some(Self { year, month, day })
    }
}

fn is_leap_year(year: u32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

// ============================================================================
// Is Date Predicate
// ============================================================================

/// Validate that a string is a valid date.
///
/// Supports formats: MM/DD/YYYY, YYYY-MM-DD, MM-DD-YYYY, MMDDYYYY
struct IsDatePredicate;

impl NamedPredicate for IsDatePredicate {
    fn name(&self) -> &str {
        "is_date"
    }

    fn evaluate(&self, value: &Value, _args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s,
            None => return false,
        };

        ParsedDate::parse(s).is_some()
    }
}

// ============================================================================
// Time Predicate
// ============================================================================

/// Validate a time string.
///
/// Supported formats:
/// - HH:MM (24-hour)
/// - HH:MM:SS (24-hour with seconds)
/// - h:MM AM/PM or hh:MM AM/PM (12-hour)
/// - h:MM:SS AM/PM or hh:MM:SS AM/PM (12-hour with seconds)
///
/// Args: `{ "format": "24h" | "12h" }` (optional, default accepts both)
struct IsTimePredicate;

impl NamedPredicate for IsTimePredicate {
    fn name(&self) -> &str {
        "is_time"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s.trim(),
            None => return false,
        };

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

        match format {
            Some("24h") => parse_time_24h(s),
            Some("12h") => parse_time_12h(s),
            _ => parse_time_24h(s) || parse_time_12h(s),
        }
    }
}

/// Parse 24-hour time: HH:MM or HH:MM:SS
fn parse_time_24h(s: &str) -> bool {
    let parts: Vec<&str> = s.split(':').collect();
    if parts.len() != 2 && parts.len() != 3 {
        return false;
    }

    // All parts must be digits
    if !parts
        .iter()
        .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
    {
        return false;
    }

    let hour: u32 = match parts[0].parse() {
        Ok(h) => h,
        Err(_) => return false,
    };
    let minute: u32 = match parts[1].parse() {
        Ok(m) => m,
        Err(_) => return false,
    };

    if hour > 23 || minute > 59 {
        return false;
    }

    // Hour and minute parts must be exactly 2 digits
    if parts[0].len() != 2 || parts[1].len() != 2 {
        return false;
    }

    if parts.len() == 3 {
        if parts[2].len() != 2 {
            return false;
        }
        let second: u32 = match parts[2].parse() {
            Ok(s) => s,
            Err(_) => return false,
        };
        if second > 59 {
            return false;
        }
    }

    true
}

/// Parse 12-hour time: h:MM AM/PM or hh:MM AM/PM (with optional :SS)
fn parse_time_12h(s: &str) -> bool {
    // Must end with AM or PM (case insensitive)
    let upper = s.to_uppercase();
    let (time_part, period) = if upper.ends_with("AM") || upper.ends_with("PM") {
        let period = &upper[upper.len() - 2..];
        let time_str = s[..s.len() - 2].trim();
        (time_str.to_string(), period.to_string())
    } else {
        return false;
    };

    let _ = period; // validated above

    let parts: Vec<&str> = time_part.split(':').collect();
    if parts.len() != 2 && parts.len() != 3 {
        return false;
    }

    if !parts
        .iter()
        .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
    {
        return false;
    }

    let hour: u32 = match parts[0].parse() {
        Ok(h) => h,
        Err(_) => return false,
    };
    let minute: u32 = match parts[1].parse() {
        Ok(m) => m,
        Err(_) => return false,
    };

    // 12-hour: 1-12
    if !(1..=12).contains(&hour) {
        return false;
    }
    if minute > 59 {
        return false;
    }

    if parts.len() == 3 {
        let second: u32 = match parts[2].parse() {
            Ok(s) => s,
            Err(_) => return false,
        };
        if second > 59 {
            return false;
        }
    }

    true
}

// ============================================================================
// ISO 8601 Datetime Predicate
// ============================================================================

/// Validate an ISO 8601 datetime string.
///
/// Accepted formats:
/// - `YYYY-MM-DDTHH:MM:SSZ`
/// - `YYYY-MM-DDTHH:MM:SS+HH:MM` / `YYYY-MM-DDTHH:MM:SS-HH:MM`
/// - `YYYY-MM-DDTHH:MM:SS.sssZ` (with fractional seconds)
/// - `YYYY-MM-DDTHH:MM:SS` (no timezone — local time)
struct IsIsoDatetimePredicate;

impl NamedPredicate for IsIsoDatetimePredicate {
    fn name(&self) -> &str {
        "is_iso_datetime"
    }

    fn evaluate(&self, value: &Value, _args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s.trim(),
            None => return false,
        };

        // Must have a T separator
        let t_pos = match s.find('T') {
            Some(p) => p,
            None => return false,
        };

        // Date part
        let date_part = &s[..t_pos];
        if ParsedDate::parse(date_part).is_none() {
            // Try direct YYYY-MM-DD parse since ParsedDate::parse expects various formats
            let dparts: Vec<&str> = date_part.split('-').collect();
            if dparts.len() != 3 {
                return false;
            }
            let year: u32 = match dparts[0].parse() {
                Ok(y) => y,
                Err(_) => return false,
            };
            let month: u32 = match dparts[1].parse() {
                Ok(m) => m,
                Err(_) => return false,
            };
            let day: u32 = match dparts[2].parse() {
                Ok(d) => d,
                Err(_) => return false,
            };
            if ParsedDate::validate(year, month, day).is_none() {
                return false;
            }
        }

        // Time part (everything after T)
        let time_rest = &s[t_pos + 1..];

        // Separate timezone suffix
        let (time_part, tz_part) = if let Some(stripped) = time_rest.strip_suffix('Z') {
            (stripped, Some("Z"))
        } else if let Some(plus_pos) = time_rest.rfind('+') {
            // Make sure the + is in the timezone position (after time digits)
            if plus_pos >= 5 {
                (&time_rest[..plus_pos], Some(&time_rest[plus_pos..]))
            } else {
                (time_rest, None)
            }
        } else if let Some(minus_pos) = time_rest.rfind('-') {
            if minus_pos >= 5 {
                (&time_rest[..minus_pos], Some(&time_rest[minus_pos..]))
            } else {
                (time_rest, None)
            }
        } else {
            (time_rest, None)
        };

        // Parse time: HH:MM:SS or HH:MM:SS.sss
        let (time_hms, _frac) = if let Some(dot_pos) = time_part.find('.') {
            let frac = &time_part[dot_pos + 1..];
            if frac.is_empty() || !frac.chars().all(|c| c.is_ascii_digit()) {
                return false;
            }
            (&time_part[..dot_pos], Some(frac))
        } else {
            (time_part, None)
        };

        // Validate HH:MM:SS or HH:MM
        if !parse_time_24h(time_hms) {
            return false;
        }

        // Validate timezone offset if present
        if let Some(tz) = tz_part {
            if tz != "Z" {
                // +HH:MM or -HH:MM
                let tz_sign = &tz[..1];
                if tz_sign != "+" && tz_sign != "-" {
                    return false;
                }
                let tz_time = &tz[1..];
                let tz_parts: Vec<&str> = tz_time.split(':').collect();
                if tz_parts.len() != 2 {
                    return false;
                }
                let tz_hour: u32 = match tz_parts[0].parse() {
                    Ok(h) => h,
                    Err(_) => return false,
                };
                let tz_min: u32 = match tz_parts[1].parse() {
                    Ok(m) => m,
                    Err(_) => return false,
                };
                if tz_hour > 23 || tz_min > 59 {
                    return false;
                }
            }
        }

        true
    }
}

// ============================================================================
// Tax Year Predicate
// ============================================================================

/// Validate that a value is a valid tax year (2020-2100).
///
/// Accepts either a number or a string.
struct IsTaxYearPredicate;

impl NamedPredicate for IsTaxYearPredicate {
    fn name(&self) -> &str {
        "is_tax_year"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let year = match value {
            Value::Number(n) => n.as_u64().map(|n| n as u32),
            Value::String(s) => s.trim().parse::<u32>().ok(),
            _ => return false,
        };

        let year = match year {
            Some(y) => y,
            None => return false,
        };

        let min_year = args.get("min").and_then(|v| v.as_u64()).unwrap_or(2020) as u32;

        let max_year = args.get("max").and_then(|v| v.as_u64()).unwrap_or(2100) as u32;

        year >= min_year && year <= max_year
    }
}

// ============================================================================
// Date In Range Predicate
// ============================================================================

/// Validate that a date falls within a specified range.
///
/// Args:
/// - min: Minimum date (inclusive), format matching input
/// - max: Maximum date (inclusive), format matching input
/// - min_year: Alternative - minimum year only
/// - max_year: Alternative - maximum year only
struct DateInRangePredicate;

impl NamedPredicate for DateInRangePredicate {
    fn name(&self) -> &str {
        "date_in_range"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s,
            None => return false,
        };

        let date = match ParsedDate::parse(s) {
            Some(d) => d,
            None => return false,
        };

        // Check year-only range first
        if let Some(min_year) = args.get("min_year").and_then(|v| v.as_u64()) {
            if date.year < min_year as u32 {
                return false;
            }
        }
        if let Some(max_year) = args.get("max_year").and_then(|v| v.as_u64()) {
            if date.year > max_year as u32 {
                return false;
            }
        }

        // Check full date range
        if let Some(min_str) = args.get("min").and_then(|v| v.as_str()) {
            if let Some(min_date) = ParsedDate::parse(min_str) {
                if date < min_date {
                    return false;
                }
            }
        }
        if let Some(max_str) = args.get("max").and_then(|v| v.as_str()) {
            if let Some(max_date) = ParsedDate::parse(max_str) {
                if date > max_date {
                    return false;
                }
            }
        }

        true
    }
}

// ============================================================================
// Date Before Predicate
// ============================================================================

/// Validate that the value date is before (or equal to) another date at a path.
///
/// Args:
/// - path: JSON Pointer path to the other date field
/// - allow_equal: If true (default), dates can be equal
///
/// Used for 1099-B: date acquired <= date sold
struct DateBeforePredicate;

impl NamedPredicate for DateBeforePredicate {
    fn name(&self) -> &str {
        "date_before"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        // This predicate compares the current value against a fixed date in args
        // For cross-field comparison, use the IR's Implies with path-based predicates

        let s = match value.as_str() {
            Some(s) => s,
            None => return false,
        };

        let date = match ParsedDate::parse(s) {
            Some(d) => d,
            None => return false,
        };

        let other_str = match args.get("date").and_then(|v| v.as_str()) {
            Some(s) => s,
            None => return true, // No comparison date specified, pass
        };

        let other_date = match ParsedDate::parse(other_str) {
            Some(d) => d,
            None => return true, // Can't parse comparison date, pass
        };

        let allow_equal = args
            .get("allow_equal")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);

        if allow_equal {
            date <= other_date
        } else {
            date < other_date
        }
    }
}

// ============================================================================
// Date After Predicate
// ============================================================================

/// Validate that the value date is after (or equal to) another date.
///
/// Args:
/// - date: The comparison date
/// - allow_equal: If true (default), dates can be equal
struct DateAfterPredicate;

impl NamedPredicate for DateAfterPredicate {
    fn name(&self) -> &str {
        "date_after"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s,
            None => return false,
        };

        let date = match ParsedDate::parse(s) {
            Some(d) => d,
            None => return false,
        };

        let other_str = match args.get("date").and_then(|v| v.as_str()) {
            Some(s) => s,
            None => return true,
        };

        let other_date = match ParsedDate::parse(other_str) {
            Some(d) => d,
            None => return true,
        };

        let allow_equal = args
            .get("allow_equal")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);

        if allow_equal {
            date >= other_date
        } else {
            date > other_date
        }
    }
}

// ============================================================================
// Time Parsing Helper (for comparisons)
// ============================================================================

/// Parsed time as total seconds from midnight, for comparison.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct ParsedTime {
    total_seconds: u32,
}

impl ParsedTime {
    /// Parse a time string (24h or 12h) into total seconds from midnight.
    fn parse(s: &str) -> Option<Self> {
        Self::from_24h(s).or_else(|| Self::from_12h(s))
    }

    fn from_24h(s: &str) -> Option<Self> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 && parts.len() != 3 {
            return None;
        }
        if !parts
            .iter()
            .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
        {
            return None;
        }
        if parts[0].len() != 2 || parts[1].len() != 2 {
            return None;
        }

        let hour: u32 = parts[0].parse().ok()?;
        let minute: u32 = parts[1].parse().ok()?;
        if hour > 23 || minute > 59 {
            return None;
        }

        let second = if parts.len() == 3 {
            if parts[2].len() != 2 {
                return None;
            }
            let s: u32 = parts[2].parse().ok()?;
            if s > 59 {
                return None;
            }
            s
        } else {
            0
        };

        Some(Self {
            total_seconds: hour * 3600 + minute * 60 + second,
        })
    }

    fn from_12h(s: &str) -> Option<Self> {
        let upper = s.to_uppercase();
        let (time_part, is_pm) = if upper.ends_with("AM") {
            (s[..s.len() - 2].trim(), false)
        } else if upper.ends_with("PM") {
            (s[..s.len() - 2].trim(), true)
        } else {
            return None;
        };

        let parts: Vec<&str> = time_part.split(':').collect();
        if parts.len() != 2 && parts.len() != 3 {
            return None;
        }
        if !parts
            .iter()
            .all(|p| !p.is_empty() && p.chars().all(|c| c.is_ascii_digit()))
        {
            return None;
        }

        let mut hour: u32 = parts[0].parse().ok()?;
        let minute: u32 = parts[1].parse().ok()?;
        if !(1..=12).contains(&hour) || minute > 59 {
            return None;
        }

        let second = if parts.len() == 3 {
            let s: u32 = parts[2].parse().ok()?;
            if s > 59 {
                return None;
            }
            s
        } else {
            0
        };

        // Convert to 24h
        if is_pm {
            if hour != 12 {
                hour += 12;
            }
        } else if hour == 12 {
            hour = 0;
        }

        Some(Self {
            total_seconds: hour * 3600 + minute * 60 + second,
        })
    }
}

// ============================================================================
// Time Before Predicate
// ============================================================================

/// Validate that a time is before a given time.
///
/// Args:
/// - time: The comparison time (24h or 12h format)
/// - allow_equal: If true (default), times can be equal
struct TimeBeforePredicate;

impl NamedPredicate for TimeBeforePredicate {
    fn name(&self) -> &str {
        "time_before"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s.trim(),
            None => return false,
        };

        let time = match ParsedTime::parse(s) {
            Some(t) => t,
            None => return false,
        };

        let other_str = match args.get("time").and_then(|v| v.as_str()) {
            Some(s) => s,
            None => return true,
        };

        let other = match ParsedTime::parse(other_str.trim()) {
            Some(t) => t,
            None => return true,
        };

        let allow_equal = args
            .get("allow_equal")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);

        if allow_equal {
            time <= other
        } else {
            time < other
        }
    }
}

// ============================================================================
// Time After Predicate
// ============================================================================

/// Validate that a time is after a given time.
///
/// Args:
/// - time: The comparison time (24h or 12h format)
/// - allow_equal: If true (default), times can be equal
struct TimeAfterPredicate;

impl NamedPredicate for TimeAfterPredicate {
    fn name(&self) -> &str {
        "time_after"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s.trim(),
            None => return false,
        };

        let time = match ParsedTime::parse(s) {
            Some(t) => t,
            None => return false,
        };

        let other_str = match args.get("time").and_then(|v| v.as_str()) {
            Some(s) => s,
            None => return true,
        };

        let other = match ParsedTime::parse(other_str.trim()) {
            Some(t) => t,
            None => return true,
        };

        let allow_equal = args
            .get("allow_equal")
            .and_then(|v| v.as_bool())
            .unwrap_or(true);

        if allow_equal {
            time >= other
        } else {
            time > other
        }
    }
}

// ============================================================================
// Time In Range Predicate
// ============================================================================

/// Validate that a time falls within a range.
///
/// Args:
/// - min: Minimum time (inclusive), 24h or 12h format
/// - max: Maximum time (inclusive), 24h or 12h format
///
/// Supports overnight ranges: if min > max, the range wraps around midnight.
/// e.g., min=22:00, max=06:00 means "10 PM to 6 AM".
struct TimeInRangePredicate;

impl NamedPredicate for TimeInRangePredicate {
    fn name(&self) -> &str {
        "time_in_range"
    }

    fn evaluate(&self, value: &Value, args: &Value) -> bool {
        let s = match value.as_str() {
            Some(s) => s.trim(),
            None => return false,
        };

        let time = match ParsedTime::parse(s) {
            Some(t) => t,
            None => return false,
        };

        let min = args
            .get("min")
            .and_then(|v| v.as_str())
            .and_then(|s| ParsedTime::parse(s.trim()));
        let max = args
            .get("max")
            .and_then(|v| v.as_str())
            .and_then(|s| ParsedTime::parse(s.trim()));

        match (min, max) {
            (Some(min_t), Some(max_t)) => {
                if min_t <= max_t {
                    // Normal range: min <= time <= max
                    time >= min_t && time <= max_t
                } else {
                    // Overnight range: time >= min OR time <= max
                    time >= min_t || time <= max_t
                }
            }
            (Some(min_t), None) => time >= min_t,
            (None, Some(max_t)) => time <= max_t,
            (None, None) => true,
        }
    }
}

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

    #[test]
    fn test_parse_date_us_format() {
        assert!(ParsedDate::parse("01/15/2024").is_some());
        assert!(ParsedDate::parse("12/31/2023").is_some());
        assert!(ParsedDate::parse("02/29/2024").is_some()); // Leap year
        assert!(ParsedDate::parse("02/29/2023").is_none()); // Not leap year
    }

    #[test]
    fn test_parse_date_iso_format() {
        assert!(ParsedDate::parse("2024-01-15").is_some());
        assert!(ParsedDate::parse("2023-12-31").is_some());
    }

    #[test]
    fn test_parse_date_no_separators() {
        assert!(ParsedDate::parse("01152024").is_some());
        assert!(ParsedDate::parse("12312023").is_some());
    }

    #[test]
    fn test_parse_date_invalid() {
        assert!(ParsedDate::parse("13/01/2024").is_none()); // Invalid month
        assert!(ParsedDate::parse("01/32/2024").is_none()); // Invalid day
        assert!(ParsedDate::parse("00/15/2024").is_none()); // Month 0
        assert!(ParsedDate::parse("01/00/2024").is_none()); // Day 0
        assert!(ParsedDate::parse("garbage").is_none());
    }

    #[test]
    fn test_is_date() {
        let pred = IsDatePredicate;

        assert!(pred.evaluate(&json!("01/15/2024"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15"), &json!(null)));
        assert!(pred.evaluate(&json!("01152024"), &json!(null)));

        assert!(!pred.evaluate(&json!("not a date"), &json!(null)));
        assert!(!pred.evaluate(&json!(12345), &json!(null)));
    }

    #[test]
    fn test_is_tax_year() {
        let pred = IsTaxYearPredicate;

        // Valid years
        assert!(pred.evaluate(&json!(2024), &json!(null)));
        assert!(pred.evaluate(&json!("2024"), &json!(null)));
        assert!(pred.evaluate(&json!(2020), &json!(null)));
        assert!(pred.evaluate(&json!(2100), &json!(null)));

        // Invalid years
        assert!(!pred.evaluate(&json!(2019), &json!(null)));
        assert!(!pred.evaluate(&json!(2101), &json!(null)));

        // Custom range
        assert!(pred.evaluate(&json!(2023), &json!({"min": 2023, "max": 2025})));
        assert!(!pred.evaluate(&json!(2022), &json!({"min": 2023, "max": 2025})));
    }

    #[test]
    fn test_date_in_range() {
        let pred = DateInRangePredicate;

        // Year-only range
        assert!(pred.evaluate(
            &json!("06/15/2024"),
            &json!({"min_year": 2020, "max_year": 2030})
        ));
        assert!(!pred.evaluate(
            &json!("06/15/2019"),
            &json!({"min_year": 2020, "max_year": 2030})
        ));

        // Full date range
        assert!(pred.evaluate(
            &json!("06/15/2024"),
            &json!({"min": "01/01/2024", "max": "12/31/2024"})
        ));
        assert!(!pred.evaluate(
            &json!("06/15/2023"),
            &json!({"min": "01/01/2024", "max": "12/31/2024"})
        ));
    }

    #[test]
    fn test_date_before() {
        let pred = DateBeforePredicate;

        // Date before
        assert!(pred.evaluate(&json!("01/01/2024"), &json!({"date": "12/31/2024"})));

        // Date equal (allowed by default)
        assert!(pred.evaluate(&json!("06/15/2024"), &json!({"date": "06/15/2024"})));

        // Date equal (not allowed)
        assert!(!pred.evaluate(
            &json!("06/15/2024"),
            &json!({"date": "06/15/2024", "allow_equal": false})
        ));

        // Date after (fail)
        assert!(!pred.evaluate(&json!("12/31/2024"), &json!({"date": "01/01/2024"})));
    }

    #[test]
    fn test_date_after() {
        let pred = DateAfterPredicate;

        // Date after
        assert!(pred.evaluate(&json!("12/31/2024"), &json!({"date": "01/01/2024"})));

        // Date equal (allowed by default)
        assert!(pred.evaluate(&json!("06/15/2024"), &json!({"date": "06/15/2024"})));

        // Date before (fail)
        assert!(!pred.evaluate(&json!("01/01/2024"), &json!({"date": "12/31/2024"})));
    }

    #[test]
    fn test_is_time() {
        let pred = IsTimePredicate;

        // Valid 24-hour times
        assert!(pred.evaluate(&json!("00:00"), &json!(null)));
        assert!(pred.evaluate(&json!("23:59"), &json!(null)));
        assert!(pred.evaluate(&json!("14:30"), &json!(null)));
        assert!(pred.evaluate(&json!("14:30:59"), &json!(null)));

        // Valid 12-hour times
        assert!(pred.evaluate(&json!("12:00 PM"), &json!(null)));
        assert!(pred.evaluate(&json!("1:30 AM"), &json!(null)));
        assert!(pred.evaluate(&json!("11:59:59 PM"), &json!(null)));
        assert!(pred.evaluate(&json!("12:00PM"), &json!(null)));

        // Format filter
        assert!(pred.evaluate(&json!("14:30"), &json!({"format": "24h"})));
        assert!(!pred.evaluate(&json!("2:30 PM"), &json!({"format": "24h"})));
        assert!(pred.evaluate(&json!("2:30 PM"), &json!({"format": "12h"})));
        assert!(!pred.evaluate(&json!("14:30"), &json!({"format": "12h"})));

        // Invalid
        assert!(!pred.evaluate(&json!("24:00"), &json!(null)));
        assert!(!pred.evaluate(&json!("12:60"), &json!(null)));
        assert!(!pred.evaluate(&json!("abc"), &json!(null)));
        assert!(!pred.evaluate(&json!("13:00 PM"), &json!(null))); // 13 not valid in 12h
        assert!(!pred.evaluate(&json!("0:00 AM"), &json!(null))); // 0 not valid in 12h

        // Non-string
        assert!(!pred.evaluate(&json!(1430), &json!(null)));
    }

    #[test]
    fn test_is_iso_datetime() {
        let pred = IsIsoDatetimePredicate;

        // Valid ISO 8601 datetimes
        assert!(pred.evaluate(&json!("2024-01-15T10:30:00Z"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15T10:30:00+05:30"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15T10:30:00-04:00"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15T10:30:00.123Z"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15T10:30:00"), &json!(null))); // No timezone
        assert!(pred.evaluate(&json!("2024-12-31T23:59:59Z"), &json!(null)));
        assert!(pred.evaluate(&json!("2024-01-15T10:30Z"), &json!(null))); // No seconds

        // Invalid
        assert!(!pred.evaluate(&json!("2024-01-15 10:30:00"), &json!(null))); // Space instead of T
        assert!(!pred.evaluate(&json!("2024-13-15T10:30:00Z"), &json!(null))); // Invalid month
        assert!(!pred.evaluate(&json!("2024-01-15T25:00:00Z"), &json!(null))); // Invalid hour
        assert!(!pred.evaluate(&json!("not-a-datetime"), &json!(null)));

        // Non-string
        assert!(!pred.evaluate(&json!(20240115), &json!(null)));
    }

    #[test]
    fn test_time_before() {
        let pred = TimeBeforePredicate;

        // Time before
        assert!(pred.evaluate(&json!("09:00"), &json!({"time": "12:00"})));
        assert!(pred.evaluate(&json!("08:30"), &json!({"time": "17:00"})));

        // Time equal (allowed by default)
        assert!(pred.evaluate(&json!("12:00"), &json!({"time": "12:00"})));

        // Time equal (not allowed)
        assert!(!pred.evaluate(
            &json!("12:00"),
            &json!({"time": "12:00", "allow_equal": false})
        ));

        // Time after (fail)
        assert!(!pred.evaluate(&json!("17:00"), &json!({"time": "12:00"})));

        // 12h format
        assert!(pred.evaluate(&json!("9:00 AM"), &json!({"time": "12:00"})));
        assert!(!pred.evaluate(&json!("1:00 PM"), &json!({"time": "12:00"})));

        // With seconds
        assert!(pred.evaluate(&json!("12:00:00"), &json!({"time": "12:00:01"})));
        assert!(!pred.evaluate(&json!("12:00:01"), &json!({"time": "12:00:00"})));

        // Non-string
        assert!(!pred.evaluate(&json!(1200), &json!({"time": "12:00"})));

        // No comparison time → pass
        assert!(pred.evaluate(&json!("12:00"), &json!(null)));
    }

    #[test]
    fn test_time_after() {
        let pred = TimeAfterPredicate;

        // Time after
        assert!(pred.evaluate(&json!("17:00"), &json!({"time": "12:00"})));
        assert!(pred.evaluate(&json!("14:30"), &json!({"time": "09:00"})));

        // Time equal (allowed by default)
        assert!(pred.evaluate(&json!("12:00"), &json!({"time": "12:00"})));

        // Time equal (not allowed)
        assert!(!pred.evaluate(
            &json!("12:00"),
            &json!({"time": "12:00", "allow_equal": false})
        ));

        // Time before (fail)
        assert!(!pred.evaluate(&json!("09:00"), &json!({"time": "12:00"})));

        // 12h format
        assert!(pred.evaluate(&json!("1:00 PM"), &json!({"time": "12:00"})));

        // Non-string
        assert!(!pred.evaluate(&json!(1700), &json!({"time": "12:00"})));
    }

    #[test]
    fn test_time_in_range() {
        let pred = TimeInRangePredicate;

        // Normal range: 09:00 - 17:00 (business hours)
        assert!(pred.evaluate(&json!("12:00"), &json!({"min": "09:00", "max": "17:00"})));
        assert!(pred.evaluate(&json!("09:00"), &json!({"min": "09:00", "max": "17:00"}))); // inclusive
        assert!(pred.evaluate(&json!("17:00"), &json!({"min": "09:00", "max": "17:00"}))); // inclusive
        assert!(!pred.evaluate(&json!("08:59"), &json!({"min": "09:00", "max": "17:00"})));
        assert!(!pred.evaluate(&json!("17:01"), &json!({"min": "09:00", "max": "17:00"})));

        // Overnight range: 22:00 - 06:00 (night shift)
        assert!(pred.evaluate(&json!("23:00"), &json!({"min": "22:00", "max": "06:00"})));
        assert!(pred.evaluate(&json!("02:00"), &json!({"min": "22:00", "max": "06:00"})));
        assert!(pred.evaluate(&json!("22:00"), &json!({"min": "22:00", "max": "06:00"}))); // inclusive
        assert!(pred.evaluate(&json!("06:00"), &json!({"min": "22:00", "max": "06:00"}))); // inclusive
        assert!(!pred.evaluate(&json!("12:00"), &json!({"min": "22:00", "max": "06:00"})));
        assert!(!pred.evaluate(&json!("21:59"), &json!({"min": "22:00", "max": "06:00"})));

        // Open-ended: min only
        assert!(pred.evaluate(&json!("14:00"), &json!({"min": "09:00"})));
        assert!(!pred.evaluate(&json!("08:00"), &json!({"min": "09:00"})));

        // Open-ended: max only
        assert!(pred.evaluate(&json!("08:00"), &json!({"max": "17:00"})));
        assert!(!pred.evaluate(&json!("18:00"), &json!({"max": "17:00"})));

        // 12h format input
        assert!(pred.evaluate(&json!("10:00 AM"), &json!({"min": "09:00", "max": "17:00"})));
        assert!(!pred.evaluate(&json!("8:00 AM"), &json!({"min": "09:00", "max": "17:00"})));

        // No args → pass
        assert!(pred.evaluate(&json!("12:00"), &json!(null)));

        // Non-string
        assert!(!pred.evaluate(&json!(1200), &json!({"min": "09:00", "max": "17:00"})));
    }
}