a3s-lane 0.5.0

Lane-based priority command queue for async task scheduling with reliability, scalability, and observability features
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
use crate::error::{LaneError, Result};
use crate::retry::RetryPolicy;
use chrono::{DateTime, Utc};
use cron::Schedule;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::str::FromStr;
use std::time::Duration;
use uuid::Uuid;

/// Unique identifier for a generic queue job.
pub type JobId = String;

/// Queue name for a generic job queue.
pub type QueueName = String;

/// Worker identifier used for leased processing.
pub type JobWorkerId = String;

/// Opaque token proving ownership of a claimed job lease.
pub type JobLockToken = String;

/// Job lease ownership tuple used for batch renewal.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobLeaseRenewal {
    pub job_id: JobId,
    pub lock_token: JobLockToken,
}

impl JobLeaseRenewal {
    /// Create a lease renewal request for a claimed job.
    pub fn new(job_id: impl Into<JobId>, lock_token: impl Into<JobLockToken>) -> Self {
        Self {
            job_id: job_id.into(),
            lock_token: lock_token.into(),
        }
    }
}

/// Job priority. Lower values run first.
pub type JobPriority = u32;

/// Default priority for jobs that do not specify one.
pub const DEFAULT_JOB_PRIORITY: JobPriority = 1000;

/// Maximum BullMQ-compatible priority. Lower values run first.
pub const MAX_JOB_PRIORITY: JobPriority = 2_u32.pow(21);

pub(crate) fn validate_job_priority(priority: JobPriority) -> Result<()> {
    if priority > MAX_JOB_PRIORITY {
        return Err(LaneError::ConfigError(format!(
            "priority must be between 0 and {MAX_JOB_PRIORITY}"
        )));
    }

    Ok(())
}

/// Default retained queue event count, matching BullMQ's default stream length.
pub const DEFAULT_JOB_EVENT_RETENTION: usize = 10_000;

/// Default retained Redis job metric data points.
pub const DEFAULT_JOB_METRICS_RETENTION: usize = 10_000;

/// Waiting-job count for a priority value.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobPriorityCount {
    pub priority: JobPriority,
    pub count: usize,
}

/// BullMQ-style terminal job metrics metadata.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobMetricsMeta {
    /// Total number of terminal transitions recorded for the metric type.
    pub count: usize,
    /// Previous closed metric timestamp in milliseconds, or `0` before one exists.
    pub previous_timestamp_millis: i64,
    /// Counter value at the previous closed metric timestamp.
    pub previous_count: usize,
}

/// BullMQ-style per-minute terminal job metrics.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobMetrics {
    pub meta: JobMetricsMeta,
    /// Newest-to-oldest per-minute closed-window counts.
    pub data: Vec<usize>,
    /// Total number of retained data points.
    pub count: usize,
}

/// Retained job queue event.
///
/// Redis-backed queues store these entries in a Redis stream, while memory and
/// local queues keep the same stream-id shape in their snapshots.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobEvent {
    /// Redis-stream-style event id: `<milliseconds>-<sequence>`.
    pub id: String,
    /// Event name, for example `added`, `waiting`, `active`, or `completed`.
    pub event: String,
    /// Event timestamp.
    pub timestamp: DateTime<Utc>,
    /// Job associated with this event, when the event is job-scoped.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub job_id: Option<JobId>,
    /// Previous lifecycle state for state-transition events.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prev: Option<JobState>,
    /// Additional event fields such as job name, progress data, or failure reason.
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub fields: BTreeMap<String, Value>,
}

/// Queue-level rate limit for claiming generic jobs.
///
/// The limit is counted when a job is successfully moved from waiting to
/// active. Workers that hit the limit simply receive no job and can poll again
/// later.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobRateLimit {
    /// Maximum number of claimed jobs in the window.
    pub max_claims: u64,
    /// Window duration.
    pub window: Duration,
}

impl JobRateLimit {
    /// Create a rate limit for claimed jobs.
    pub fn new(max_claims: u64, window: Duration) -> Self {
        Self { max_claims, window }
    }

    /// Limit claimed jobs per second.
    pub fn per_second(max_claims: u64) -> Self {
        Self::new(max_claims, Duration::from_secs(1))
    }

    /// Limit claimed jobs per minute.
    pub fn per_minute(max_claims: u64) -> Self {
        Self::new(max_claims, Duration::from_secs(60))
    }

    /// Validate the rate limit values.
    pub fn validate(&self) -> Result<()> {
        if self.max_claims == 0 {
            return Err(LaneError::ConfigError(
                "job claim rate limit max_claims must be greater than zero".to_string(),
            ));
        }
        if self.window.is_zero() {
            return Err(LaneError::ConfigError(
                "job claim rate limit window must be greater than zero".to_string(),
            ));
        }
        Ok(())
    }
}

/// Lifecycle state for a durable job.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum JobState {
    /// Ready to be claimed by a worker.
    Waiting,
    /// Scheduled for the future.
    Delayed,
    /// Leased to a worker and currently processing.
    Active,
    /// Parent job waiting for children to finish.
    WaitingChildren,
    /// Finished successfully.
    Completed,
    /// Finished with a terminal failure.
    Failed,
}

impl JobState {
    /// All durable lifecycle states in queue-count order.
    pub const ALL: [Self; 6] = [
        Self::Waiting,
        Self::Delayed,
        Self::Active,
        Self::WaitingChildren,
        Self::Completed,
        Self::Failed,
    ];

    /// States counted as pending work, matching BullMQ's queue `count()` shape.
    pub const PENDING: [Self; 3] = [Self::Waiting, Self::Delayed, Self::WaitingChildren];

    /// Whether this state is terminal and should not be claimed again.
    pub fn is_terminal(self) -> bool {
        matches!(self, Self::Completed | Self::Failed)
    }
}

/// Job count for a lifecycle state.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobStateCount {
    pub state: JobState,
    pub count: usize,
}

/// A retained log line for a generic job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobLogEntry {
    pub timestamp: DateTime<Utc>,
    pub line: String,
}

/// A page of retained log entries for a generic job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobLogPage {
    pub logs: Vec<JobLogEntry>,
    pub count: usize,
}

/// Options for listing jobs from a backend.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobListOptions {
    /// Optional legacy single-state filter. `None` lists jobs from all states.
    pub state: Option<JobState>,
    /// Optional multi-state filter. When non-empty, this takes precedence over `state`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub states: Vec<JobState>,
    /// Number of matching jobs to skip.
    pub offset: usize,
    /// Maximum number of jobs to return.
    pub limit: usize,
    /// Return jobs in ascending order when true, descending order when false.
    #[serde(default = "default_list_ascending")]
    pub ascending: bool,
}

impl Default for JobListOptions {
    fn default() -> Self {
        Self {
            state: None,
            states: Vec::new(),
            offset: 0,
            limit: 100,
            ascending: true,
        }
    }
}

impl JobListOptions {
    /// Create default list options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Restrict results to a single state.
    pub fn with_state(mut self, state: JobState) -> Self {
        self.state = Some(state);
        self.states.clear();
        self
    }

    /// Restrict results to one or more states.
    ///
    /// Empty input lists all lifecycle states. Duplicate states are removed while
    /// preserving the caller's order.
    pub fn with_states(mut self, states: impl IntoIterator<Item = JobState>) -> Self {
        self.state = None;
        self.states.clear();
        for state in states {
            if !self.states.contains(&state) {
                self.states.push(state);
            }
        }
        self
    }

    /// Set the pagination offset.
    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }

    /// Set the maximum result count.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Return jobs in ascending order.
    pub fn ascending(mut self) -> Self {
        self.ascending = true;
        self
    }

    /// Return jobs in descending order.
    pub fn descending(mut self) -> Self {
        self.ascending = false;
        self
    }

    /// Configure result direction explicitly.
    pub fn with_ascending(mut self, ascending: bool) -> Self {
        self.ascending = ascending;
        self
    }

    pub(crate) fn selected_states(&self) -> Vec<JobState> {
        let mut states = Vec::new();
        if !self.states.is_empty() {
            for &state in &self.states {
                if !states.contains(&state) {
                    states.push(state);
                }
            }
        } else if let Some(state) = self.state {
            states.push(state);
        } else {
            states.extend_from_slice(JobState::ALL.as_slice());
        }
        states
    }
}

fn default_list_ascending() -> bool {
    true
}

fn default_repeat_list_ascending() -> bool {
    false
}

/// A page of jobs returned by a backend list operation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobListPage {
    pub jobs: Vec<Job>,
    pub total: usize,
    pub offset: usize,
    pub limit: usize,
}

/// Job input used when creating a flow.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobSpec {
    pub name: String,
    pub payload: Value,
    pub options: JobOptions,
}

impl JobSpec {
    /// Create a job specification with default options.
    pub fn new(name: impl Into<String>, payload: Value) -> Self {
        Self {
            name: name.into(),
            payload,
            options: JobOptions::new(),
        }
    }

    /// Attach explicit job options.
    pub fn with_options(mut self, options: JobOptions) -> Self {
        self.options = options;
        self
    }
}

/// Jobs created by a parent-child flow submission.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobFlow {
    pub parent: Job,
    pub children: Vec<Job>,
}

/// Current dependency snapshot for a flow parent.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobFlowDependencies {
    /// Parent job snapshot.
    pub parent: Job,
    /// Existing child job snapshots in the parent's child order.
    pub children: Vec<Job>,
    /// Child ids that still block a `waiting_children` parent.
    pub pending_child_ids: Vec<JobId>,
    /// Child ids recorded on the parent but no longer retained.
    pub missing_child_ids: Vec<JobId>,
}

/// Dependency counts for a flow parent.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencyCounts {
    /// Retained children that completed successfully.
    pub processed: usize,
    /// Retained children that are still waiting, delayed, active, or waiting on children.
    pub unprocessed: usize,
    /// Retained children that failed terminally.
    pub failed: usize,
    /// Retained failed children that no longer block the parent.
    pub ignored: usize,
    /// Child ids recorded on the parent but no longer retained.
    pub missing: usize,
}

/// Flow dependency count buckets to read.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencyCountOptions {
    pub processed: bool,
    pub unprocessed: bool,
    pub ignored: bool,
    pub failed: bool,
}

impl JobFlowDependencyCountOptions {
    /// Create empty count options; empty options default to all buckets when read.
    pub fn new() -> Self {
        Self::default()
    }

    /// Read all BullMQ dependency count buckets.
    pub fn all() -> Self {
        Self {
            processed: true,
            unprocessed: true,
            ignored: true,
            failed: true,
        }
    }

    /// Include the processed bucket.
    pub fn with_processed(mut self, enabled: bool) -> Self {
        self.processed = enabled;
        self
    }

    /// Include the unprocessed bucket.
    pub fn with_unprocessed(mut self, enabled: bool) -> Self {
        self.unprocessed = enabled;
        self
    }

    /// Include the ignored-failure bucket.
    pub fn with_ignored(mut self, enabled: bool) -> Self {
        self.ignored = enabled;
        self
    }

    /// Include the fail-parent bucket.
    pub fn with_failed(mut self, enabled: bool) -> Self {
        self.failed = enabled;
        self
    }

    /// Include one bucket by kind.
    pub fn with_kind(mut self, kind: JobFlowDependencyKind, enabled: bool) -> Self {
        match kind {
            JobFlowDependencyKind::Processed => self.processed = enabled,
            JobFlowDependencyKind::Unprocessed => self.unprocessed = enabled,
            JobFlowDependencyKind::Ignored => self.ignored = enabled,
            JobFlowDependencyKind::Failed => self.failed = enabled,
        }
        self
    }

    pub(crate) fn selected(&self) -> Vec<JobFlowDependencyKind> {
        let mut kinds = Vec::new();
        if self.processed {
            kinds.push(JobFlowDependencyKind::Processed);
        }
        if self.unprocessed {
            kinds.push(JobFlowDependencyKind::Unprocessed);
        }
        if self.ignored {
            kinds.push(JobFlowDependencyKind::Ignored);
        }
        if self.failed {
            kinds.push(JobFlowDependencyKind::Failed);
        }
        if kinds.is_empty() {
            kinds.extend_from_slice(&[
                JobFlowDependencyKind::Processed,
                JobFlowDependencyKind::Unprocessed,
                JobFlowDependencyKind::Ignored,
                JobFlowDependencyKind::Failed,
            ]);
        }
        kinds
    }
}

/// Selected BullMQ-style dependency counts for a flow parent.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencySelectedCounts {
    pub processed: Option<usize>,
    pub unprocessed: Option<usize>,
    pub ignored: Option<usize>,
    pub failed: Option<usize>,
}

impl JobFlowDependencySelectedCounts {
    /// Return the count for one dependency bucket, when it was requested.
    pub fn get(&self, kind: JobFlowDependencyKind) -> Option<usize> {
        match kind {
            JobFlowDependencyKind::Processed => self.processed,
            JobFlowDependencyKind::Unprocessed => self.unprocessed,
            JobFlowDependencyKind::Ignored => self.ignored,
            JobFlowDependencyKind::Failed => self.failed,
        }
    }

    pub(crate) fn insert(&mut self, kind: JobFlowDependencyKind, count: usize) {
        match kind {
            JobFlowDependencyKind::Processed => self.processed = Some(count),
            JobFlowDependencyKind::Unprocessed => self.unprocessed = Some(count),
            JobFlowDependencyKind::Ignored => self.ignored = Some(count),
            JobFlowDependencyKind::Failed => self.failed = Some(count),
        }
    }
}

/// BullMQ-style full dependency buckets for a flow parent.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct JobFlowDependencyValues {
    /// Completed child return values from the parent-scoped `:processed` hash.
    pub processed: BTreeMap<JobId, Value>,
    /// Child ids that still block the parent in the pending dependency set.
    pub unprocessed: Vec<JobId>,
    /// Fail-parent child ids from the parent-scoped `:unsuccessful` zset.
    pub failed: Vec<JobId>,
    /// Ignored or continued failure reasons from the parent-scoped `:failed` hash.
    pub ignored: BTreeMap<JobId, String>,
}

/// Flow dependency bucket used by paginated dependency inspection.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum JobFlowDependencyKind {
    /// Completed children stored in the parent-scoped `:processed` hash.
    Processed,
    /// Children still stored in the parent-scoped pending dependency set.
    Unprocessed,
    /// Ignored or continued failures stored in the parent-scoped `:failed` hash.
    Ignored,
    /// Fail-parent failures stored in the parent-scoped `:unsuccessful` zset.
    Failed,
}

#[cfg(feature = "redis-backend")]
impl JobFlowDependencyKind {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Processed => "processed",
            Self::Unprocessed => "unprocessed",
            Self::Ignored => "ignored",
            Self::Failed => "failed",
        }
    }
}

/// Options for reading one flow dependency bucket incrementally.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencyPageOptions {
    /// Dependency bucket to scan.
    pub kind: JobFlowDependencyKind,
    /// Redis cursor for hash/set buckets, or zset offset for failed dependencies.
    pub cursor: u64,
    /// Scan count hint or zset page size. BullMQ defaults this to 20.
    pub count: usize,
}

impl JobFlowDependencyPageOptions {
    /// Create dependency page options for one bucket.
    pub fn new(kind: JobFlowDependencyKind) -> Self {
        Self {
            kind,
            cursor: 0,
            count: 20,
        }
    }

    /// Set the cursor returned by the previous page.
    pub fn with_cursor(mut self, cursor: u64) -> Self {
        self.cursor = cursor;
        self
    }

    /// Set the scan count hint or zset page size.
    pub fn with_count(mut self, count: usize) -> Self {
        self.count = count;
        self
    }
}

/// Cursor options shared by multi-bucket flow dependency page reads.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencyPageCursor {
    /// Redis cursor for hash/set buckets, or zset offset for failed dependencies.
    pub cursor: u64,
    /// Scan count hint or zset page size. BullMQ defaults this to 20.
    pub count: usize,
}

impl Default for JobFlowDependencyPageCursor {
    fn default() -> Self {
        Self {
            cursor: 0,
            count: 20,
        }
    }
}

impl JobFlowDependencyPageCursor {
    /// Create default cursor options for one bucket.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the cursor returned by the previous page.
    pub fn with_cursor(mut self, cursor: u64) -> Self {
        self.cursor = cursor;
        self
    }

    /// Set the scan count hint or zset page size.
    pub fn with_count(mut self, count: usize) -> Self {
        self.count = count;
        self
    }
}

impl From<JobFlowDependencyPageOptions> for JobFlowDependencyPageCursor {
    fn from(options: JobFlowDependencyPageOptions) -> Self {
        Self {
            cursor: options.cursor,
            count: options.count,
        }
    }
}

/// Options for reading several flow dependency buckets in one backend call.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobFlowDependencyPagesOptions {
    pub processed: Option<JobFlowDependencyPageCursor>,
    pub unprocessed: Option<JobFlowDependencyPageCursor>,
    pub ignored: Option<JobFlowDependencyPageCursor>,
    pub failed: Option<JobFlowDependencyPageCursor>,
}

impl JobFlowDependencyPagesOptions {
    /// Create empty multi-bucket dependency page options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Read all dependency buckets with default cursors and counts.
    pub fn all() -> Self {
        let cursor = JobFlowDependencyPageCursor::default();
        Self {
            processed: Some(cursor),
            unprocessed: Some(cursor),
            ignored: Some(cursor),
            failed: Some(cursor),
        }
    }

    /// Include the processed bucket.
    pub fn with_processed(mut self, cursor: JobFlowDependencyPageCursor) -> Self {
        self.processed = Some(cursor);
        self
    }

    /// Include the unprocessed bucket.
    pub fn with_unprocessed(mut self, cursor: JobFlowDependencyPageCursor) -> Self {
        self.unprocessed = Some(cursor);
        self
    }

    /// Include the ignored-failure bucket.
    pub fn with_ignored(mut self, cursor: JobFlowDependencyPageCursor) -> Self {
        self.ignored = Some(cursor);
        self
    }

    /// Include the fail-parent bucket.
    pub fn with_failed(mut self, cursor: JobFlowDependencyPageCursor) -> Self {
        self.failed = Some(cursor);
        self
    }

    /// Include one bucket by kind.
    pub fn with_kind(
        mut self,
        kind: JobFlowDependencyKind,
        cursor: JobFlowDependencyPageCursor,
    ) -> Self {
        match kind {
            JobFlowDependencyKind::Processed => self.processed = Some(cursor),
            JobFlowDependencyKind::Unprocessed => self.unprocessed = Some(cursor),
            JobFlowDependencyKind::Ignored => self.ignored = Some(cursor),
            JobFlowDependencyKind::Failed => self.failed = Some(cursor),
        }
        self
    }

    pub(crate) fn selected(&self) -> Vec<JobFlowDependencyPageOptions> {
        [
            (JobFlowDependencyKind::Processed, self.processed),
            (JobFlowDependencyKind::Unprocessed, self.unprocessed),
            (JobFlowDependencyKind::Ignored, self.ignored),
            (JobFlowDependencyKind::Failed, self.failed),
        ]
        .into_iter()
        .filter_map(|(kind, cursor)| {
            cursor.map(|cursor| JobFlowDependencyPageOptions {
                kind,
                cursor: cursor.cursor,
                count: cursor.count,
            })
        })
        .collect()
    }
}

/// One entry from a paginated flow dependency bucket.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum JobFlowDependencyPageItem {
    /// Completed child return value.
    Processed { child_id: JobId, value: Value },
    /// Pending child id.
    Unprocessed { child_id: JobId },
    /// Ignored or continued child failure reason.
    Ignored {
        child_id: JobId,
        failed_reason: String,
    },
    /// Fail-parent child id.
    Failed { child_id: JobId },
}

/// A cursor page for one flow dependency bucket.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobFlowDependencyPage {
    pub kind: JobFlowDependencyKind,
    pub items: Vec<JobFlowDependencyPageItem>,
    /// Next cursor to pass back for this same bucket. `0` means the scan is done.
    pub next_cursor: u64,
    /// Requested scan count hint or zset page size.
    pub count: usize,
}

/// Cursor pages for several requested flow dependency buckets.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct JobFlowDependencyPages {
    pub processed: Option<JobFlowDependencyPage>,
    pub unprocessed: Option<JobFlowDependencyPage>,
    pub ignored: Option<JobFlowDependencyPage>,
    pub failed: Option<JobFlowDependencyPage>,
}

impl JobFlowDependencyPages {
    /// Return the page for one dependency bucket, when it was requested.
    pub fn get(&self, kind: JobFlowDependencyKind) -> Option<&JobFlowDependencyPage> {
        match kind {
            JobFlowDependencyKind::Processed => self.processed.as_ref(),
            JobFlowDependencyKind::Unprocessed => self.unprocessed.as_ref(),
            JobFlowDependencyKind::Ignored => self.ignored.as_ref(),
            JobFlowDependencyKind::Failed => self.failed.as_ref(),
        }
    }

    pub(crate) fn insert(&mut self, page: JobFlowDependencyPage) {
        match page.kind {
            JobFlowDependencyKind::Processed => self.processed = Some(page),
            JobFlowDependencyKind::Unprocessed => self.unprocessed = Some(page),
            JobFlowDependencyKind::Ignored => self.ignored = Some(page),
            JobFlowDependencyKind::Failed => self.failed = Some(page),
        }
    }
}

/// Finished status for a retained job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum JobFinishedResult {
    /// The job exists but has not reached a terminal state yet.
    NotFinished,
    /// The job completed successfully.
    Completed {
        /// Retained completion value, when the job record still stores one.
        return_value: Option<Value>,
    },
    /// The job failed terminally.
    Failed {
        /// Retained failure reason, when the job record still stores one.
        failed_reason: Option<String>,
    },
}

/// Completed child return values keyed by child job id.
pub type JobFlowChildValues = BTreeMap<JobId, Value>;

/// Ignored child failure reasons keyed by child job id.
pub type JobFlowIgnoredFailures = BTreeMap<JobId, String>;

/// Repeat schedule used by a generic job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum RepeatSchedule {
    /// Repeat at a fixed interval after each successful completion.
    Every {
        /// Delay between completed occurrence and the next scheduled occurrence.
        interval: Duration,
    },
    /// Repeat on a cron expression in UTC.
    Cron {
        /// Seven-field cron expression: second, minute, hour, day of month,
        /// month, day of week, and year.
        #[serde(rename = "cron")]
        expression: String,
    },
}

/// Repeat settings for a generic job.
///
/// `limit` counts total executions, including the first job. For example,
/// `limit = 3` allows the original job plus two automatically scheduled
/// successors.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RepeatOptions {
    /// The repeat schedule. This is flattened in JSON so legacy interval
    /// snapshots keep using the `{ "interval": ... }` shape.
    #[serde(flatten)]
    pub schedule: RepeatSchedule,
    /// Optional maximum total execution count for this repeat series.
    pub limit: Option<u32>,
    /// Optional latest scheduled time for a new occurrence.
    pub end_at: Option<DateTime<Utc>>,
    /// Optional stable key that groups occurrences from the same series.
    pub key: Option<String>,
}

impl RepeatOptions {
    /// Repeat at a fixed interval after each successful completion.
    pub fn every(interval: Duration) -> Self {
        Self {
            schedule: RepeatSchedule::Every { interval },
            limit: None,
            end_at: None,
            key: None,
        }
    }

    /// Repeat according to a UTC cron expression.
    ///
    /// The expression uses seven fields: second, minute, hour, day of month,
    /// month, day of week, and year.
    pub fn cron(expression: impl Into<String>) -> Self {
        Self {
            schedule: RepeatSchedule::Cron {
                expression: expression.into(),
            },
            limit: None,
            end_at: None,
            key: None,
        }
    }

    /// Return the fixed interval when this repeat uses interval scheduling.
    pub fn interval(&self) -> Option<Duration> {
        match &self.schedule {
            RepeatSchedule::Every { interval } => Some(*interval),
            RepeatSchedule::Cron { .. } => None,
        }
    }

    /// Return the cron expression when this repeat uses cron scheduling.
    pub fn cron_expression(&self) -> Option<&str> {
        match &self.schedule {
            RepeatSchedule::Every { .. } => None,
            RepeatSchedule::Cron { expression } => Some(expression),
        }
    }

    /// Limit total executions, including the first occurrence.
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Stop scheduling new occurrences after this timestamp.
    pub fn until(mut self, end_at: DateTime<Utc>) -> Self {
        self.end_at = Some(end_at);
        self
    }

    /// Set a stable repeat-series key.
    pub fn with_key(mut self, key: impl Into<String>) -> Self {
        self.key = Some(key.into());
        self
    }

    pub(crate) fn validate(&self) -> Result<()> {
        match &self.schedule {
            RepeatSchedule::Every { interval } => {
                if interval.is_zero() {
                    return Err(LaneError::ConfigError(
                        "repeat interval must be greater than zero".to_string(),
                    ));
                }
            }
            RepeatSchedule::Cron { expression } => {
                parse_cron_expression(expression)?;
            }
        }

        if self.limit == Some(0) {
            return Err(LaneError::ConfigError(
                "repeat limit must be greater than zero".to_string(),
            ));
        }

        Ok(())
    }

    pub(crate) fn next_scheduled_at(&self, after: DateTime<Utc>) -> Result<Option<DateTime<Utc>>> {
        let scheduled_at = match &self.schedule {
            RepeatSchedule::Every { interval } => Some(add_duration(after, *interval)),
            RepeatSchedule::Cron { expression } => {
                let schedule = parse_cron_expression(expression)?;
                schedule.after(&after).next()
            }
        };

        Ok(scheduled_at)
    }
}

/// Current owner snapshot for a repeat series.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobRepeatEntry {
    /// Queue-local repeat series key.
    pub key: String,
    /// Current non-terminal owner job id for the series.
    pub job_id: JobId,
    /// Job name used by the current owner.
    pub name: String,
    /// Current lifecycle state of the owner job.
    pub state: JobState,
    /// Scheduled time of the current owner occurrence.
    pub scheduled_at: DateTime<Utc>,
    /// Completed occurrence count carried by the current owner.
    pub repeat_count: u32,
    /// Repeat schedule and limits for the series.
    pub options: RepeatOptions,
}

/// Options for listing repeat series / job schedulers.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobRepeatListOptions {
    /// Number of matching repeat series to skip.
    pub offset: usize,
    /// Maximum number of repeat series to return.
    pub limit: usize,
    /// Return repeat series by next scheduled time ascending when true.
    ///
    /// The default is descending to match BullMQ's `getJobSchedulers()`.
    #[serde(default = "default_repeat_list_ascending")]
    pub ascending: bool,
}

impl Default for JobRepeatListOptions {
    fn default() -> Self {
        Self {
            offset: 0,
            limit: 100,
            ascending: false,
        }
    }
}

impl JobRepeatListOptions {
    /// Create default repeat-list options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the pagination offset.
    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }

    /// Set the maximum result count.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Return repeat series by next scheduled time ascending.
    pub fn ascending(mut self) -> Self {
        self.ascending = true;
        self
    }

    /// Return repeat series by next scheduled time descending.
    pub fn descending(mut self) -> Self {
        self.ascending = false;
        self
    }
}

/// A page of repeat series / job schedulers returned by a backend.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobRepeatPage {
    pub repeats: Vec<JobRepeatEntry>,
    pub total: usize,
    pub offset: usize,
    pub limit: usize,
}

pub(crate) fn page_repeat_entries(
    mut repeats: Vec<JobRepeatEntry>,
    options: JobRepeatListOptions,
) -> JobRepeatPage {
    repeats.sort_by(|a, b| {
        let order = a
            .scheduled_at
            .cmp(&b.scheduled_at)
            .then_with(|| a.key.cmp(&b.key))
            .then_with(|| a.job_id.cmp(&b.job_id));
        if options.ascending {
            order
        } else {
            order.reverse()
        }
    });
    let total = repeats.len();
    let page = repeats
        .into_iter()
        .skip(options.offset)
        .take(options.limit)
        .collect();
    JobRepeatPage {
        repeats: page,
        total,
        offset: options.offset,
        limit: options.limit,
    }
}

/// Simple deduplication settings for a generic job.
///
/// Jobs with the same deduplication id are coalesced while the first job is
/// still in a non-terminal state. The deduplication id is released when that
/// job completes, fails terminally, is removed, or its optional TTL expires.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DeduplicationOptions {
    /// Queue-local id used to coalesce duplicate submissions.
    pub id: String,
    /// Optional owner-key TTL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl: Option<Duration>,
    /// Replace an existing delayed owner with the new job.
    #[serde(default)]
    pub replace: bool,
    /// Keep the latest duplicate while the current owner is active.
    #[serde(default)]
    pub keep_last_if_active: bool,
    /// Refresh the deduplication TTL when a duplicate is added.
    #[serde(default)]
    pub extend: bool,
}

impl DeduplicationOptions {
    /// Create simple deduplication options.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            ttl: None,
            replace: false,
            keep_last_if_active: false,
            extend: false,
        }
    }

    /// Set how long this job owns its deduplication id.
    pub fn with_ttl(mut self, ttl: Duration) -> Self {
        self.ttl = Some(ttl);
        self
    }

    /// Replace the current delayed owner instead of returning it.
    ///
    /// This mirrors BullMQ's replace path for delayed deduplicated jobs. Active
    /// keep-last-if-active behavior is a separate mode and is not enabled here.
    pub fn replace_delayed(mut self, replace: bool) -> Self {
        self.replace = replace;
        self
    }

    /// Store the latest duplicate while the current owner is active.
    ///
    /// This mirrors BullMQ's `keepLastIfActive` mechanism for standalone jobs:
    /// duplicate adds return the active owner, but the latest duplicate is
    /// materialized as a new job when the owner finishes terminally.
    pub fn keep_last_if_active(mut self, keep: bool) -> Self {
        self.keep_last_if_active = keep;
        self
    }

    /// Refresh this deduplication id's TTL when a duplicate is added.
    ///
    /// This mirrors BullMQ's `extend` debounce option. It only has an effect
    /// when a positive TTL is configured and keep-last-if-active is disabled.
    pub fn extend_ttl(mut self, extend: bool) -> Self {
        self.extend = extend;
        self
    }

    pub(crate) fn validate(&self) -> Result<()> {
        if self.id.trim().is_empty() {
            return Err(LaneError::ConfigError(
                "deduplication id must not be empty".to_string(),
            ));
        }
        if matches!(self.ttl, Some(ttl) if ttl.is_zero()) {
            return Err(LaneError::ConfigError(
                "deduplication ttl must be greater than zero".to_string(),
            ));
        }

        Ok(())
    }
}

/// Retention policy for finished jobs.
///
/// This mirrors BullMQ's `KeepJobs` shape for `removeOnComplete` and
/// `removeOnFail`: `count` keeps the newest N jobs in a terminal set, `age`
/// keeps jobs younger than the configured duration, and `limit` bounds how many
/// aged jobs are removed per terminal transition.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobRetention {
    /// Maximum age of jobs to keep in a terminal set.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub age: Option<Duration>,
    /// Maximum number of jobs to keep in a terminal set. `0` removes the
    /// currently finished job immediately, matching BullMQ's `{ count: 0 }`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub count: Option<usize>,
    /// Maximum number of aged jobs removed per terminal transition.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
}

impl JobRetention {
    /// Keep at most `count` newest finished jobs.
    pub fn count(count: usize) -> Self {
        Self {
            age: None,
            count: Some(count),
            limit: None,
        }
    }

    /// Keep finished jobs younger than `age`.
    pub fn age(age: Duration) -> Self {
        Self {
            age: Some(age),
            count: None,
            limit: None,
        }
    }

    /// Keep finished jobs that satisfy both `age` and `count`.
    pub fn age_and_count(age: Duration, count: usize) -> Self {
        Self {
            age: Some(age),
            count: Some(count),
            limit: None,
        }
    }

    /// Set or replace the maximum age.
    pub fn with_age(mut self, age: Duration) -> Self {
        self.age = Some(age);
        self
    }

    /// Set or replace the maximum count.
    pub fn with_count(mut self, count: usize) -> Self {
        self.count = Some(count);
        self
    }

    /// Limit how many aged jobs are removed per terminal transition.
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = Some(limit);
        self
    }

    pub(crate) fn removes_current(&self) -> bool {
        self.count == Some(0)
    }

    pub(crate) fn validate(&self, field: &str) -> Result<()> {
        if self.age.is_none() && self.count.is_none() {
            return Err(LaneError::ConfigError(format!(
                "{field} must specify an age or count"
            )));
        }
        if matches!(self.age, Some(age) if age.is_zero()) {
            return Err(LaneError::ConfigError(format!(
                "{field} age must be greater than zero"
            )));
        }
        if self.limit == Some(0) {
            return Err(LaneError::ConfigError(format!(
                "{field} limit must be greater than zero"
            )));
        }
        Ok(())
    }
}

/// Options used when adding a generic queue job.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobOptions {
    /// Optional caller-assigned id used for idempotent job submission.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub job_id: Option<JobId>,
    /// Lower values run before higher values.
    pub priority: JobPriority,
    /// Insert ready jobs at the front of their same-priority waiting group.
    #[serde(default, skip_serializing_if = "is_false")]
    pub lifo: bool,
    /// Optional delay before the job becomes claimable.
    pub delay: Option<Duration>,
    /// Retry policy used after processing failure.
    pub retry_policy: RetryPolicy,
    /// Optional execution timeout hint for workers.
    pub timeout: Option<Duration>,
    /// Remove the job record after successful completion.
    pub remove_on_complete: bool,
    /// BullMQ-style completed-job retention policy. The legacy
    /// `remove_on_complete` bool takes precedence when set to `true`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completion_retention: Option<JobRetention>,
    /// Remove the job record after terminal failure.
    pub remove_on_fail: bool,
    /// BullMQ-style failed-job retention policy. The legacy `remove_on_fail`
    /// bool takes precedence when set to `true`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub failure_retention: Option<JobRetention>,
    /// Number of lease expirations tolerated before terminal failure.
    pub max_stalled_count: u32,
    /// Optional repeat schedule.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repeat: Option<RepeatOptions>,
    /// Optional simple deduplication settings.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deduplication: Option<DeduplicationOptions>,
    /// Do not fail the parent flow when this child reaches terminal failure.
    #[serde(default, skip_serializing_if = "is_false")]
    pub ignore_dependency_on_failure: bool,
    /// Remove this child from its parent dependencies when it reaches terminal failure.
    #[serde(default, skip_serializing_if = "is_false")]
    pub remove_dependency_on_failure: bool,
    /// Continue the parent flow when this child reaches terminal failure.
    #[serde(default, skip_serializing_if = "is_false")]
    pub continue_parent_on_failure: bool,
    /// Defer parent failure when this child reaches terminal failure.
    #[serde(default, skip_serializing_if = "is_false")]
    pub fail_parent_on_failure: bool,
}

impl Default for JobOptions {
    fn default() -> Self {
        Self {
            job_id: None,
            priority: DEFAULT_JOB_PRIORITY,
            lifo: false,
            delay: None,
            retry_policy: RetryPolicy::none(),
            timeout: None,
            remove_on_complete: false,
            completion_retention: None,
            remove_on_fail: false,
            failure_retention: None,
            max_stalled_count: 1,
            repeat: None,
            deduplication: None,
            ignore_dependency_on_failure: false,
            remove_dependency_on_failure: false,
            continue_parent_on_failure: false,
            fail_parent_on_failure: false,
        }
    }
}

impl JobOptions {
    /// Create default job options.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set job priority. Lower values run first.
    pub fn with_priority(mut self, priority: JobPriority) -> Self {
        self.priority = priority;
        self
    }

    /// Configure same-priority waiting order. `true` claims newest ready jobs first.
    pub fn with_lifo(mut self, lifo: bool) -> Self {
        self.lifo = lifo;
        self
    }

    /// Set a caller-assigned id for idempotent job submission.
    pub fn with_job_id(mut self, job_id: impl Into<String>) -> Self {
        self.job_id = Some(job_id.into());
        self
    }

    /// Delay the job before it can be claimed.
    pub fn with_delay(mut self, delay: Duration) -> Self {
        self.delay = Some(delay);
        self
    }

    /// Set retry behavior for processing failures.
    pub fn with_retry_policy(mut self, retry_policy: RetryPolicy) -> Self {
        self.retry_policy = retry_policy;
        self
    }

    /// Set an execution timeout hint for workers.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Configure whether completed jobs are retained.
    pub fn remove_on_complete(mut self, remove: bool) -> Self {
        self.remove_on_complete = remove;
        self.completion_retention = None;
        self
    }

    /// Configure completed-job retention by age and/or count.
    pub fn with_completion_retention(mut self, retention: JobRetention) -> Self {
        self.remove_on_complete = false;
        self.completion_retention = Some(retention);
        self
    }

    /// Configure whether failed jobs are retained.
    pub fn remove_on_fail(mut self, remove: bool) -> Self {
        self.remove_on_fail = remove;
        self.failure_retention = None;
        self
    }

    /// Configure failed-job retention by age and/or count.
    pub fn with_failure_retention(mut self, retention: JobRetention) -> Self {
        self.remove_on_fail = false;
        self.failure_retention = Some(retention);
        self
    }

    /// Configure stalled-job tolerance.
    pub fn with_max_stalled_count(mut self, count: u32) -> Self {
        self.max_stalled_count = count;
        self
    }

    /// Configure repeat scheduling.
    pub fn with_repeat(mut self, repeat: RepeatOptions) -> Self {
        self.repeat = Some(repeat);
        self
    }

    /// Coalesce duplicate submissions while a matching job is still non-terminal.
    pub fn with_deduplication_id(mut self, id: impl Into<String>) -> Self {
        self.deduplication = Some(DeduplicationOptions::new(id));
        self
    }

    /// Configure deduplication with explicit options such as TTL.
    pub fn with_deduplication(mut self, deduplication: DeduplicationOptions) -> Self {
        self.deduplication = Some(deduplication);
        self
    }

    /// Configure BullMQ-style `ignoreDependencyOnFailure` for flow children.
    pub fn with_ignore_dependency_on_failure(mut self, ignore: bool) -> Self {
        self.ignore_dependency_on_failure = ignore;
        self
    }

    /// Configure BullMQ-style `removeDependencyOnFailure` for flow children.
    pub fn with_remove_dependency_on_failure(mut self, remove: bool) -> Self {
        self.remove_dependency_on_failure = remove;
        self
    }

    /// Configure BullMQ-style `continueParentOnFailure` for flow children.
    pub fn with_continue_parent_on_failure(mut self, continue_parent: bool) -> Self {
        self.continue_parent_on_failure = continue_parent;
        self
    }

    /// Configure BullMQ-style `failParentOnFailure` for flow children.
    pub fn with_fail_parent_on_failure(mut self, fail_parent: bool) -> Self {
        self.fail_parent_on_failure = fail_parent;
        self
    }

    pub(crate) fn validate(&self) -> Result<()> {
        if let Some(job_id) = self.job_id.as_deref() {
            validate_job_id(job_id)?;
        }

        validate_job_priority(self.priority)?;

        if let Some(repeat) = &self.repeat {
            repeat.validate()?;
        }

        if let Some(deduplication) = &self.deduplication {
            deduplication.validate()?;
        }

        if let Some(retention) = &self.completion_retention {
            retention.validate("completion retention")?;
        }

        if let Some(retention) = &self.failure_retention {
            retention.validate("failure retention")?;
        }

        let flow_failure_policies = [
            self.ignore_dependency_on_failure,
            self.remove_dependency_on_failure,
            self.continue_parent_on_failure,
            self.fail_parent_on_failure,
        ]
        .into_iter()
        .filter(|enabled| *enabled)
        .count();
        if flow_failure_policies > 1 {
            return Err(LaneError::ConfigError(
                "flow child failure policies are mutually exclusive".to_string(),
            ));
        }

        Ok(())
    }

    pub(crate) fn removes_completed_immediately(&self) -> bool {
        self.remove_on_complete
            || self
                .completion_retention
                .as_ref()
                .is_some_and(JobRetention::removes_current)
    }

    pub(crate) fn completed_retention(&self) -> Option<&JobRetention> {
        if self.remove_on_complete {
            return None;
        }
        self.completion_retention
            .as_ref()
            .filter(|retention| !retention.removes_current())
    }

    pub(crate) fn removes_failed_immediately(&self) -> bool {
        self.remove_on_fail
            || self
                .failure_retention
                .as_ref()
                .is_some_and(JobRetention::removes_current)
    }

    pub(crate) fn failed_retention(&self) -> Option<&JobRetention> {
        if self.remove_on_fail {
            return None;
        }
        self.failure_retention
            .as_ref()
            .filter(|retention| !retention.removes_current())
    }
}

fn validate_job_id(job_id: &str) -> Result<()> {
    if job_id.trim().is_empty() {
        return Err(LaneError::ConfigError(
            "job id must not be empty".to_string(),
        ));
    }
    if job_id == "0" || job_id.starts_with("0:") {
        return Err(LaneError::ConfigError(
            "job id cannot be `0` or start with `0:`".to_string(),
        ));
    }
    if is_bullmq_integer_job_id(job_id) {
        return Err(LaneError::ConfigError(
            "custom job id cannot be an integer".to_string(),
        ));
    }
    Ok(())
}

fn is_bullmq_integer_job_id(job_id: &str) -> bool {
    let mut chars = job_id.chars();
    match chars.next() {
        Some('-') => matches!(chars.next(), Some('1'..='9')) && chars.all(|ch| ch.is_ascii_digit()),
        Some('1'..='9') => chars.all(|ch| ch.is_ascii_digit()),
        Some('0') => false,
        _ => false,
    }
}

/// Durable generic job record.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Job {
    pub id: JobId,
    pub queue: QueueName,
    pub name: String,
    pub payload: Value,
    pub options: JobOptions,
    pub priority: JobPriority,
    pub state: JobState,
    pub attempts_made: u32,
    pub stalled_count: u32,
    pub created_at: DateTime<Utc>,
    pub scheduled_at: DateTime<Utc>,
    /// Monotonic sequence assigned each time the job enters the waiting set.
    #[serde(default)]
    pub enqueued_seq: u64,
    pub processed_at: Option<DateTime<Utc>>,
    pub finished_at: Option<DateTime<Utc>>,
    pub worker_id: Option<JobWorkerId>,
    #[serde(default, skip)]
    pub lock_token: Option<JobLockToken>,
    pub lease_expires_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deferred_failure: Option<String>,
    pub failed_reason: Option<String>,
    /// Retained failure stack traces, matching BullMQ's JSON stacktrace array shape.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stacktrace: Vec<String>,
    pub return_value: Option<Value>,
    pub progress: Option<Value>,
    pub logs: Vec<JobLogEntry>,
    pub parent_id: Option<JobId>,
    pub child_ids: Vec<JobId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repeat_key: Option<String>,
    #[serde(default)]
    pub repeat_count: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub deduplication_expires_at: Option<DateTime<Utc>>,
}

impl Job {
    pub(crate) fn new(
        queue: QueueName,
        name: String,
        payload: Value,
        options: JobOptions,
        now: DateTime<Utc>,
    ) -> Self {
        let scheduled_at = options
            .delay
            .map(|delay| add_duration(now, delay))
            .unwrap_or(now);
        let state = if scheduled_at > now {
            JobState::Delayed
        } else {
            JobState::Waiting
        };
        let repeat_key = options.repeat.as_ref().map(|repeat| {
            repeat
                .key
                .clone()
                .unwrap_or_else(|| format!("{queue}:{name}"))
        });
        let deduplication_expires_at = deduplication_expiration(&options, now);
        let id = options
            .job_id
            .clone()
            .unwrap_or_else(|| Uuid::new_v4().to_string());

        Self {
            id,
            queue,
            name,
            payload,
            priority: options.priority,
            options,
            state,
            attempts_made: 0,
            stalled_count: 0,
            created_at: now,
            scheduled_at,
            enqueued_seq: 0,
            processed_at: None,
            finished_at: None,
            worker_id: None,
            lock_token: None,
            lease_expires_at: None,
            deferred_failure: None,
            failed_reason: None,
            stacktrace: Vec::new(),
            return_value: None,
            progress: None,
            logs: Vec::new(),
            parent_id: None,
            child_ids: Vec::new(),
            repeat_key,
            repeat_count: 0,
            deduplication_expires_at,
        }
    }
}

fn is_false(value: &bool) -> bool {
    !*value
}

fn is_zero(value: &u64) -> bool {
    *value == 0
}

pub(crate) fn deduplication_expiration(
    options: &JobOptions,
    now: DateTime<Utc>,
) -> Option<DateTime<Utc>> {
    options
        .deduplication
        .as_ref()
        .filter(|deduplication| !deduplication.keep_last_if_active)
        .and_then(|deduplication| deduplication.ttl)
        .map(|ttl| add_duration(now, ttl))
}

pub(crate) fn add_duration(at: DateTime<Utc>, duration: Duration) -> DateTime<Utc> {
    match chrono::Duration::from_std(duration) {
        Ok(delta) => at.checked_add_signed(delta).unwrap_or(at),
        Err(_) => at,
    }
}

fn parse_cron_expression(expression: &str) -> Result<Schedule> {
    let expression = expression.trim();
    if expression.is_empty() {
        return Err(LaneError::ConfigError(
            "repeat cron expression must not be empty".to_string(),
        ));
    }

    Schedule::from_str(expression).map_err(|error| {
        LaneError::ConfigError(format!(
            "invalid repeat cron expression `{expression}`: {error}"
        ))
    })
}

/// Queue state counts for generic jobs.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct JobQueueStats {
    pub total: usize,
    pub waiting: usize,
    pub delayed: usize,
    pub active: usize,
    pub waiting_children: usize,
    pub completed: usize,
    pub failed: usize,
    pub paused: bool,
}

/// Serializable snapshot used by durable job backends.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct JobQueueSnapshot {
    pub queue: QueueName,
    pub paused: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub events: Vec<JobEvent>,
    #[serde(default, skip_serializing_if = "is_zero")]
    pub event_sequence: u64,
    pub jobs: Vec<Job>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub deduplication_next_jobs: Vec<Job>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub deduplication_next_flows: Vec<JobFlow>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub released_deduplication_owners: Vec<(String, JobId)>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub flow_dependency_indexes: Vec<JobFlowDependencyIndex>,
}

/// Serializable parent-scoped flow dependency side index.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct JobFlowDependencyIndex {
    pub parent_id: JobId,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub processed: BTreeMap<JobId, Value>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub ignored: BTreeMap<JobId, String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub failed: Vec<JobId>,
}