hammerwork 1.15.5

A high-performance, database-driven job queue for Rust with PostgreSQL and MySQL support, featuring job prioritization, cron scheduling, event streaming (Kafka/Kinesis/PubSub), webhooks, rate limiting, Prometheus metrics, and comprehensive monitoring
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
//! In-memory test implementation of the job queue for testing purposes.
//!
//! This module provides a `TestQueue` that implements the `DatabaseQueue` trait
//! entirely in memory, making it ideal for unit tests and development without
//! requiring a database connection.
//!
//! # Features
//!
//! - All operations are performed in-memory
//! - Thread-safe with async operations
//! - Supports time manipulation for testing delayed jobs
//! - Full compatibility with the `DatabaseQueue` trait
//! - Deterministic behavior for testing
//!
//! # Examples
//!
//! ```rust
//! use hammerwork::queue::test::TestQueue;
//! use hammerwork::{Job, JobStatus, queue::DatabaseQueue};
//! use serde_json::json;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let queue = TestQueue::new();
//!
//! // Enqueue a job
//! let job = Job::new("test_queue".to_string(), json!({"test": true}));
//! let job_id = queue.enqueue(job).await?;
//!
//! // Process the job
//! if let Some(mut job) = queue.dequeue("test_queue").await? {
//!     // Process the job...
//!     queue.complete_job(job.id).await?;
//! }
//! # Ok(())
//! # }
//! ```

use crate::{
    HammerworkError, Result,
    batch::{BatchId, BatchResult, BatchStatus, JobBatch},
    job::{Job, JobId, JobStatus},
    priority::{JobPriority, PriorityWeights},
    queue::{DatabaseQueue, QueuePauseInfo},
    rate_limit::ThrottleConfig,
    stats::{DeadJobSummary, QueueStats},
    workflow::{JobGroup, WorkflowId, WorkflowStatus},
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
};
use tokio::sync::RwLock;
use uuid::Uuid;

// We need to use a real sqlx Database type for the trait bound
// This is just a marker - TestQueue doesn't actually use SQL
#[cfg(feature = "postgres")]
type TestDatabaseType = sqlx::Postgres;

#[cfg(all(feature = "mysql", not(feature = "postgres")))]
type TestDatabaseType = sqlx::MySql;

#[cfg(all(not(feature = "postgres"), not(feature = "mysql")))]
type TestDatabaseType = sqlx::Any; // Fallback

/// Mock clock for controlling time in tests.
///
/// The `MockClock` allows you to control time flow in your tests, making it possible
/// to test time-dependent functionality like delayed jobs, cron schedules, and timeouts
/// in a deterministic and fast manner.
///
/// # Examples
///
/// ## Basic time manipulation
///
/// ```rust
/// use hammerwork::queue::test::MockClock;
/// use chrono::Duration;
///
/// let clock = MockClock::new();
/// let initial_time = clock.now();
///
/// // Advance time by 1 hour
/// clock.advance(Duration::hours(1));
///
/// let after_advance = clock.now();
/// assert_eq!((after_advance - initial_time).num_hours(), 1);
/// ```
///
/// ## Testing delayed jobs
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use hammerwork::queue::test::{MockClock, TestQueue};
/// use hammerwork::{Job, queue::DatabaseQueue};
/// use serde_json::json;
/// use chrono::Duration;
///
/// let clock = MockClock::new();
/// let queue = TestQueue::with_clock(clock.clone());
///
/// // Create a delayed job (runs in 2 hours)
/// let delayed_job = Job::with_delay(
///     "delayed_queue".to_string(),
///     json!({"message": "Hello future!"}),
///     Duration::hours(2)
/// );
///
/// let job_id = queue.enqueue(delayed_job).await?;
///
/// // Job should not be available immediately
/// assert!(queue.dequeue("delayed_queue").await?.is_none());
///
/// // Advance time by 2 hours
/// clock.advance(Duration::hours(2));
///
/// // Now the job should be available
/// let job = queue.dequeue("delayed_queue").await?.unwrap();
/// assert_eq!(job.id, job_id);
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct MockClock {
    current_time: Arc<Mutex<DateTime<Utc>>>,
}

impl MockClock {
    /// Create a new mock clock starting at the current time.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::MockClock;
    ///
    /// let clock = MockClock::new();
    /// let now = clock.now();
    /// println!("Mock clock started at: {}", now);
    /// ```
    pub fn new() -> Self {
        Self {
            current_time: Arc::new(Mutex::new(Utc::now())),
        }
    }

    /// Get the current mock time.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::MockClock;
    /// use chrono::Duration;
    ///
    /// let clock = MockClock::new();
    /// let time1 = clock.now();
    ///
    /// clock.advance(Duration::minutes(30));
    /// let time2 = clock.now();
    ///
    /// assert_eq!((time2 - time1).num_minutes(), 30);
    /// ```
    pub fn now(&self) -> DateTime<Utc> {
        *self.current_time.lock().unwrap()
    }

    /// Advance the mock time by the given duration.
    ///
    /// This is useful for testing time-dependent functionality without
    /// waiting for real time to pass.
    ///
    /// # Arguments
    ///
    /// * `duration` - The amount of time to advance the clock
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::MockClock;
    /// use chrono::Duration;
    ///
    /// let clock = MockClock::new();
    /// let start_time = clock.now();
    ///
    /// // Advance by 1 day, 2 hours, and 30 minutes
    /// clock.advance(Duration::days(1));
    /// clock.advance(Duration::hours(2));
    /// clock.advance(Duration::minutes(30));
    ///
    /// let end_time = clock.now();
    /// let total_duration = end_time - start_time;
    ///
    /// assert_eq!(total_duration.num_hours(), 26); // 24 + 2
    /// assert_eq!(total_duration.num_minutes(), 1590); // 26 * 60 + 30
    /// ```
    pub fn advance(&self, duration: chrono::Duration) {
        let mut time = self.current_time.lock().unwrap();
        *time += duration;
    }

    /// Set the mock time to a specific instant.
    ///
    /// This allows you to set the clock to any specific time, which can be
    /// useful for testing scenarios that depend on absolute times.
    ///
    /// # Arguments
    ///
    /// * `time` - The specific time to set the clock to
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::MockClock;
    /// use chrono::{Utc, TimeZone};
    ///
    /// let clock = MockClock::new();
    ///
    /// // Set to a specific date and time
    /// let specific_time = Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap();
    /// clock.set_time(specific_time);
    ///
    /// assert_eq!(clock.now(), specific_time);
    /// ```
    pub fn set_time(&self, time: DateTime<Utc>) {
        *self.current_time.lock().unwrap() = time;
    }
}

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

/// In-memory storage for the test queue
#[derive(Debug)]
struct TestStorage {
    /// All jobs stored by ID
    jobs: HashMap<JobId, Job>,
    /// Jobs organized by queue name and status
    queues: HashMap<String, HashMap<JobStatus, Vec<JobId>>>,
    /// Batch information
    batches: HashMap<BatchId, JobBatch>,
    /// Batch job mappings
    batch_jobs: HashMap<BatchId, Vec<JobId>>,
    /// Workflow information
    workflows: HashMap<WorkflowId, JobGroup>,
    /// Job dependencies (job_id -> depends on these job_ids)
    dependencies: HashMap<JobId, Vec<JobId>>,
    /// Reverse dependencies (job_id -> these jobs depend on it)
    dependents: HashMap<JobId, Vec<JobId>>,
    /// Throttle configurations
    throttle_configs: HashMap<String, ThrottleConfig>,
    /// Job results storage
    job_results: HashMap<JobId, (serde_json::Value, Option<DateTime<Utc>>)>,
    /// Paused queues information
    paused_queues: HashMap<String, QueuePauseInfo>,
    /// Mock clock for time control
    clock: MockClock,
}

impl TestStorage {
    fn new(clock: MockClock) -> Self {
        Self {
            jobs: HashMap::new(),
            queues: HashMap::new(),
            batches: HashMap::new(),
            batch_jobs: HashMap::new(),
            workflows: HashMap::new(),
            dependencies: HashMap::new(),
            dependents: HashMap::new(),
            throttle_configs: HashMap::new(),
            job_results: HashMap::new(),
            paused_queues: HashMap::new(),
            clock,
        }
    }

    /// Add a job to the appropriate queue and status list
    fn add_job_to_queue(&mut self, job: &Job) {
        let queue_jobs = self.queues.entry(job.queue_name.clone()).or_default();
        let status_jobs = queue_jobs.entry(job.status).or_default();
        if !status_jobs.contains(&job.id) {
            status_jobs.push(job.id);
        }
    }

    /// Update a job's status
    fn update_job_status(&mut self, job_id: JobId, new_status: JobStatus) -> Result<()> {
        // Get old status first
        let old_status = self
            .jobs
            .get(&job_id)
            .map(|job| job.status)
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        // Get the queue name
        let queue_name = self
            .jobs
            .get(&job_id)
            .map(|job| job.queue_name.clone())
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        // Remove from old status
        if let Some(queue_jobs) = self.queues.get_mut(&queue_name) {
            if let Some(status_jobs) = queue_jobs.get_mut(&old_status) {
                status_jobs.retain(|id| *id != job_id);
            }
        }

        // Update the job status
        if let Some(job) = self.jobs.get_mut(&job_id) {
            job.status = new_status;
        }

        // Add to new status
        let queue_jobs = self.queues.entry(queue_name).or_default();
        let status_jobs = queue_jobs.entry(new_status).or_default();
        if !status_jobs.contains(&job_id) {
            status_jobs.push(job_id);
        }

        Ok(())
    }

    /// Get the next job to dequeue based on priority and scheduled time
    fn get_next_job(&self, queue_name: &str, weights: Option<&PriorityWeights>) -> Option<JobId> {
        let queue_jobs = self.queues.get(queue_name)?;
        let pending_jobs = queue_jobs.get(&JobStatus::Pending)?;

        let now = self.clock.now();
        let eligible_jobs: Vec<&Job> = pending_jobs
            .iter()
            .filter_map(|id| self.jobs.get(id))
            .filter(|job| job.scheduled_at <= now)
            .collect();

        if eligible_jobs.is_empty() {
            return None;
        }

        // If no weights provided, use strict priority ordering
        if weights.is_none() {
            return eligible_jobs
                .into_iter()
                .max_by_key(|job| (job.priority.as_i32(), std::cmp::Reverse(job.scheduled_at)))
                .map(|job| job.id);
        }

        // Use weighted selection
        let weights = weights.unwrap();

        // If strict priority, just get highest priority job
        if weights.is_strict() {
            return eligible_jobs
                .into_iter()
                .max_by_key(|job| (job.priority.as_i32(), std::cmp::Reverse(job.scheduled_at)))
                .map(|job| job.id);
        }

        // Group jobs by priority
        let mut candidates_by_priority: HashMap<JobPriority, Vec<&Job>> = HashMap::new();
        for job in eligible_jobs {
            candidates_by_priority
                .entry(job.priority)
                .or_default()
                .push(job);
        }

        // Build weighted selection pool
        let mut weighted_choices = Vec::new();
        for priority in candidates_by_priority.keys() {
            let weight = weights.get_weight(*priority);
            for _ in 0..weight {
                weighted_choices.push(*priority);
            }
        }

        if weighted_choices.is_empty() {
            return None;
        }

        // Use hash-based selection for deterministic behavior
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        queue_name.hash(&mut hasher);
        self.clock.now().timestamp_millis().hash(&mut hasher);
        let hash_value = hasher.finish();

        let selected_priority = weighted_choices[hash_value as usize % weighted_choices.len()];

        // Get the oldest job from the selected priority
        candidates_by_priority
            .get(&selected_priority)
            .and_then(|jobs| {
                jobs.iter()
                    .min_by_key(|job| job.scheduled_at)
                    .map(|job| job.id)
            })
    }
}

/// In-memory test implementation of the job queue.
///
/// `TestQueue` provides a complete implementation of the `DatabaseQueue` trait
/// that runs entirely in memory, making it perfect for unit testing your job
/// processing logic without requiring a database connection.
///
/// # Features
///
/// - **Full DatabaseQueue compatibility**: Drop-in replacement for testing
/// - **Time control**: MockClock integration for testing time-dependent features
/// - **Deterministic behavior**: Predictable ordering and timing for reliable tests
/// - **Complete feature support**: Batches, workflows, cron jobs, priorities, and more
/// - **Thread-safe**: Safe to use in concurrent test scenarios
///
/// # Examples
///
/// ## Basic job processing test
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use hammerwork::queue::test::TestQueue;
/// use hammerwork::{Job, JobStatus, queue::DatabaseQueue};
/// use serde_json::json;
///
/// let queue = TestQueue::new();
///
/// // Enqueue a job
/// let job = Job::new("test_queue".to_string(), json!({"task": "process_data"}));
/// let job_id = queue.enqueue(job).await?;
///
/// // Dequeue and process the job
/// let job = queue.dequeue("test_queue").await?.unwrap();
/// assert_eq!(job.status, JobStatus::Running);
///
/// // Mark job as completed
/// queue.complete_job(job_id).await?;
///
/// // Verify completion
/// let completed_job = queue.get_job(job_id).await?.unwrap();
/// assert_eq!(completed_job.status, JobStatus::Completed);
/// # Ok(())
/// # }
/// ```
///
/// ## Testing job priorities
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use hammerwork::queue::test::TestQueue;
/// use hammerwork::{Job, JobPriority, queue::DatabaseQueue};
/// use serde_json::json;
///
/// let queue = TestQueue::new();
///
/// // Enqueue jobs with different priorities
/// let low_job = Job::new("priority_queue".to_string(), json!({"priority": "low"}))
///     .as_low_priority();
/// let high_job = Job::new("priority_queue".to_string(), json!({"priority": "high"}))
///     .as_high_priority();
///
/// let low_id = queue.enqueue(low_job).await?;
/// let high_id = queue.enqueue(high_job).await?;
///
/// // High priority job should be dequeued first
/// let first_job = queue.dequeue("priority_queue").await?.unwrap();
/// assert_eq!(first_job.id, high_id);
/// assert_eq!(first_job.priority, JobPriority::High);
///
/// let second_job = queue.dequeue("priority_queue").await?.unwrap();
/// assert_eq!(second_job.id, low_id);
/// assert_eq!(second_job.priority, JobPriority::Low);
/// # Ok(())
/// # }
/// ```
///
/// ## Testing job failures and retries
///
/// ```rust
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use hammerwork::queue::test::TestQueue;
/// use hammerwork::{Job, JobStatus, queue::DatabaseQueue};
/// use serde_json::json;
///
/// let queue = TestQueue::new();
///
/// // Create a job with custom retry settings
/// let job = Job::new("retry_queue".to_string(), json!({"data": "test"}))
///     .with_max_attempts(3);
/// let job_id = queue.enqueue(job).await?;
///
/// // Dequeue and fail the job
/// let job = queue.dequeue("retry_queue").await?.unwrap();
/// queue.fail_job(job_id, "Temporary network error").await?;
///
/// // Job should be in retrying state
/// let retrying_job = queue.get_job(job_id).await?.unwrap();
/// assert_eq!(retrying_job.status, JobStatus::Retrying);
/// assert_eq!(retrying_job.attempts, 1);
///
/// // After 3 failures, job becomes dead
/// queue.fail_job(job_id, "Still failing").await?;
/// queue.fail_job(job_id, "Final failure").await?;
///
/// let dead_job = queue.get_job(job_id).await?.unwrap();
/// assert_eq!(dead_job.status, JobStatus::Dead);
/// assert_eq!(dead_job.attempts, 3);
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct TestQueue {
    storage: Arc<RwLock<TestStorage>>,
    clock: MockClock,
}

impl TestQueue {
    /// Create a new test queue with a fresh MockClock.
    ///
    /// This is the most common way to create a TestQueue for testing.
    /// Each TestQueue gets its own isolated storage and time control.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::TestQueue;
    ///
    /// let queue = TestQueue::new();
    /// // Queue is ready to use for testing
    /// ```
    pub fn new() -> Self {
        let clock = MockClock::new();
        Self {
            storage: Arc::new(RwLock::new(TestStorage::new(clock.clone()))),
            clock,
        }
    }

    /// Create a new test queue with a custom MockClock.
    ///
    /// This allows you to share time control across multiple test components
    /// or start with a specific time state.
    ///
    /// # Arguments
    ///
    /// * `clock` - The MockClock instance to use for time control
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::{MockClock, TestQueue};
    /// use chrono::{Utc, TimeZone};
    ///
    /// // Create a clock set to a specific time
    /// let clock = MockClock::new();
    /// let specific_time = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
    /// clock.set_time(specific_time);
    ///
    /// // Create queue with this clock
    /// let queue = TestQueue::with_clock(clock);
    ///
    /// // Queue will use the specified time
    /// assert_eq!(queue.clock().now(), specific_time);
    /// ```
    pub fn with_clock(clock: MockClock) -> Self {
        Self {
            storage: Arc::new(RwLock::new(TestStorage::new(clock.clone()))),
            clock,
        }
    }

    /// Get access to the mock clock for time manipulation.
    ///
    /// This allows you to control time flow in your tests, which is essential
    /// for testing time-dependent features like delayed jobs, cron schedules,
    /// and timeouts.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use hammerwork::queue::test::TestQueue;
    /// use chrono::Duration;
    ///
    /// let queue = TestQueue::new();
    /// let initial_time = queue.clock().now();
    ///
    /// // Advance time by 1 hour
    /// queue.clock().advance(Duration::hours(1));
    ///
    /// let later_time = queue.clock().now();
    /// assert_eq!((later_time - initial_time).num_hours(), 1);
    /// ```
    pub fn clock(&self) -> &MockClock {
        &self.clock
    }

    /// Get the number of jobs in a specific status for a queue.
    ///
    /// This is a testing utility that helps you verify the state of your
    /// job queue during tests.
    ///
    /// # Arguments
    ///
    /// * `queue_name` - The name of the queue to check
    /// * `status` - The job status to count
    ///
    /// # Returns
    ///
    /// The number of jobs in the specified status
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use hammerwork::queue::test::TestQueue;
    /// use hammerwork::{Job, JobStatus, queue::DatabaseQueue};
    /// use serde_json::json;
    ///
    /// let queue = TestQueue::new();
    ///
    /// // Initially no jobs
    /// assert_eq!(queue.get_job_count("test_queue", &JobStatus::Pending).await, 0);
    ///
    /// // Add some jobs
    /// for i in 0..5 {
    ///     let job = Job::new("test_queue".to_string(), json!({"index": i}));
    ///     queue.enqueue(job).await?;
    /// }
    ///
    /// // Should have 5 pending jobs
    /// assert_eq!(queue.get_job_count("test_queue", &JobStatus::Pending).await, 5);
    ///
    /// // Process one job
    /// let job = queue.dequeue("test_queue").await?.unwrap();
    /// queue.complete_job(job.id).await?;
    ///
    /// // Should have 4 pending and 1 completed
    /// assert_eq!(queue.get_job_count("test_queue", &JobStatus::Pending).await, 4);
    /// assert_eq!(queue.get_job_count("test_queue", &JobStatus::Completed).await, 1);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_job_count(&self, queue_name: &str, status: &JobStatus) -> usize {
        let storage = self.storage.read().await;
        storage
            .queues
            .get(queue_name)
            .and_then(|q| q.get(status))
            .map(|jobs| jobs.len())
            .unwrap_or(0)
    }

    /// Get all jobs for testing purposes.
    ///
    /// This returns all jobs stored in the TestQueue, regardless of their
    /// queue name or status. This is useful for comprehensive testing and
    /// debugging.
    ///
    /// # Returns
    ///
    /// A vector containing all jobs in the TestQueue
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use hammerwork::queue::test::TestQueue;
    /// use hammerwork::{Job, queue::DatabaseQueue};
    /// use serde_json::json;
    ///
    /// let queue = TestQueue::new();
    ///
    /// // Add jobs to different queues
    /// let job1 = Job::new("queue_a".to_string(), json!({"type": "a"}));
    /// let job2 = Job::new("queue_b".to_string(), json!({"type": "b"}));
    ///
    /// queue.enqueue(job1).await?;
    /// queue.enqueue(job2).await?;
    ///
    /// // Get all jobs
    /// let all_jobs = queue.get_all_jobs().await;
    /// assert_eq!(all_jobs.len(), 2);
    ///
    /// // Jobs from different queues are included
    /// let queue_names: Vec<String> = all_jobs.iter()
    ///     .map(|job| job.queue_name.clone())
    ///     .collect();
    /// assert!(queue_names.contains(&"queue_a".to_string()));
    /// assert!(queue_names.contains(&"queue_b".to_string()));
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_all_jobs(&self) -> Vec<Job> {
        let storage = self.storage.read().await;
        storage.jobs.values().cloned().collect()
    }
}

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

#[async_trait]
impl DatabaseQueue for TestQueue {
    type Database = TestDatabaseType;

    async fn enqueue(&self, mut job: Job) -> Result<JobId> {
        let mut storage = self.storage.write().await;

        // Set timestamps using the mock clock
        let now = storage.clock.now();
        let original_scheduled_at = job.scheduled_at;
        let original_created_at = job.created_at;

        // Calculate the delay from the original creation
        let delay = original_scheduled_at - original_created_at;

        job.created_at = now;

        // For immediate jobs (where scheduled_at was set to created_at in Job::new()),
        // update scheduled_at to use the mock clock time
        if original_scheduled_at == original_created_at {
            job.scheduled_at = now;
        } else {
            // For delayed jobs, preserve the delay but use mock clock time as base
            job.scheduled_at = now + delay;
        }

        // Store the job
        storage.jobs.insert(job.id, job.clone());
        storage.add_job_to_queue(&job);

        Ok(job.id)
    }

    async fn dequeue(&self, queue_name: &str) -> Result<Option<Job>> {
        let mut storage = self.storage.write().await;

        if let Some(job_id) = storage.get_next_job(queue_name, None) {
            // Update job status to Running
            storage.update_job_status(job_id, JobStatus::Running)?;

            // Set started_at
            let now = storage.clock.now();
            if let Some(job) = storage.jobs.get_mut(&job_id) {
                job.started_at = Some(now);
                return Ok(Some(job.clone()));
            }
        }

        Ok(None)
    }

    async fn dequeue_with_priority_weights(
        &self,
        queue_name: &str,
        weights: &PriorityWeights,
    ) -> Result<Option<Job>> {
        let mut storage = self.storage.write().await;

        if let Some(job_id) = storage.get_next_job(queue_name, Some(weights)) {
            // Update job status to Running
            storage.update_job_status(job_id, JobStatus::Running)?;

            // Set started_at
            let now = storage.clock.now();
            if let Some(job) = storage.jobs.get_mut(&job_id) {
                job.started_at = Some(now);
                return Ok(Some(job.clone()));
            }
        }

        Ok(None)
    }

    async fn complete_job(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;

        storage.update_job_status(job_id, JobStatus::Completed)?;

        let now = storage.clock.now();
        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.completed_at = Some(now);

            // Handle cron jobs
            if job.recurring && job.cron_schedule.is_some() {
                // Calculate next run time
                if let Ok(schedule) = job
                    .cron_schedule
                    .as_ref()
                    .unwrap()
                    .parse::<cron::Schedule>()
                {
                    let timezone = job
                        .timezone
                        .as_ref()
                        .and_then(|tz| tz.parse::<chrono_tz::Tz>().ok())
                        .unwrap_or(chrono_tz::UTC);

                    let now_in_tz = now.with_timezone(&timezone);
                    if let Some(next) = schedule.after(&now_in_tz).next() {
                        job.next_run_at = Some(next.with_timezone(&Utc));
                    }
                }
            }
        }

        // Resolve dependencies
        if let Some(dependents) = storage.dependents.get(&job_id).cloned() {
            let now = storage.clock.now();
            for dependent_id in dependents {
                // Check if all dependencies are satisfied
                if let Some(deps) = storage.dependencies.get(&dependent_id) {
                    let all_complete = deps.iter().all(|dep_id| {
                        storage
                            .jobs
                            .get(dep_id)
                            .map(|j| j.status == JobStatus::Completed)
                            .unwrap_or(false)
                    });

                    if all_complete {
                        // Make the job eligible for execution
                        if let Some(dep_job) = storage.jobs.get_mut(&dependent_id) {
                            if dep_job.status == JobStatus::Pending {
                                dep_job.scheduled_at = now;
                            }
                        }
                    }
                }
            }
        }

        Ok(())
    }

    async fn fail_job(&self, job_id: JobId, error_message: &str) -> Result<()> {
        let mut storage = self.storage.write().await;

        // First increment attempts
        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.attempts += 1;
            job.error_message = Some(error_message.to_string());
        } else {
            return Err(HammerworkError::JobNotFound {
                id: job_id.to_string(),
            });
        }

        // Then check if we should retry
        let should_retry = if let Some(job) = storage.jobs.get(&job_id) {
            job.attempts < job.max_attempts
        } else {
            return Err(HammerworkError::JobNotFound {
                id: job_id.to_string(),
            });
        };

        let now = storage.clock.now();

        if should_retry {
            storage.update_job_status(job_id, JobStatus::Retrying)?;
        } else {
            storage.update_job_status(job_id, JobStatus::Dead)?;
            if let Some(job) = storage.jobs.get_mut(&job_id) {
                job.failed_at = Some(now);
            }

            // Check if this job belongs to a workflow with fail-fast policy
            let other_job_ids: Vec<JobId> = storage
                .workflows
                .values()
                .find(|workflow| {
                    workflow.failure_policy == crate::workflow::FailurePolicy::FailFast
                        && workflow.jobs.iter().any(|j| j.id == job_id)
                })
                .map(|workflow| {
                    workflow
                        .jobs
                        .iter()
                        .filter(|j| j.id != job_id)
                        .map(|j| j.id)
                        .collect()
                })
                .unwrap_or_default();

            // Fail all other pending jobs in the workflow
            for other_job_id in other_job_ids {
                if let Some(j) = storage.jobs.get(&other_job_id) {
                    if j.status == JobStatus::Pending || j.status == JobStatus::Retrying {
                        storage
                            .update_job_status(other_job_id, JobStatus::Failed)
                            .ok();
                        if let Some(j) = storage.jobs.get_mut(&other_job_id) {
                            j.error_message =
                                Some("Workflow failed (fail-fast policy)".to_string());
                            j.failed_at = Some(now);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    async fn retry_job(&self, job_id: JobId, retry_at: DateTime<Utc>) -> Result<()> {
        let mut storage = self.storage.write().await;

        storage.update_job_status(job_id, JobStatus::Pending)?;

        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.scheduled_at = retry_at;
            job.started_at = None;
        }

        Ok(())
    }

    async fn get_job(&self, job_id: JobId) -> Result<Option<Job>> {
        let storage = self.storage.read().await;
        Ok(storage.jobs.get(&job_id).cloned())
    }

    async fn delete_job(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;

        if let Some(job) = storage.jobs.remove(&job_id) {
            // Remove from queue
            if let Some(queue_jobs) = storage.queues.get_mut(&job.queue_name) {
                if let Some(status_jobs) = queue_jobs.get_mut(&job.status) {
                    status_jobs.retain(|id| *id != job_id);
                }
            }

            // Remove from batch if applicable
            if let Some(batch_id) = job.batch_id {
                if let Some(batch_jobs) = storage.batch_jobs.get_mut(&batch_id) {
                    batch_jobs.retain(|id| *id != job_id);
                }
            }

            // Clean up dependencies
            storage.dependencies.remove(&job_id);
            for deps in storage.dependents.values_mut() {
                deps.retain(|id| *id != job_id);
            }

            Ok(())
        } else {
            Err(HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })
        }
    }

    // Batch operations
    async fn enqueue_batch(&self, mut batch: JobBatch) -> Result<BatchId> {
        let mut storage = self.storage.write().await;

        batch.created_at = storage.clock.now();
        let batch_id = batch.id;

        // Enqueue all jobs in the batch
        let mut job_ids = Vec::new();
        for mut job in batch.jobs.iter().cloned() {
            job.batch_id = Some(batch_id);

            // Apply the same timestamp logic as regular enqueue
            let now = storage.clock.now();
            let original_scheduled_at = job.scheduled_at;
            let original_created_at = job.created_at;

            // Calculate the delay from the original creation
            let delay = original_scheduled_at - original_created_at;

            job.created_at = now;

            // For immediate jobs (where scheduled_at was set to created_at in Job::new()),
            // update scheduled_at to use the mock clock time
            if original_scheduled_at == original_created_at {
                job.scheduled_at = now;
            } else {
                // For delayed jobs, preserve the delay but use mock clock time as base
                job.scheduled_at = now + delay;
            }

            storage.jobs.insert(job.id, job.clone());
            storage.add_job_to_queue(&job);
            job_ids.push(job.id);
        }

        storage.batch_jobs.insert(batch_id, job_ids);
        storage.batches.insert(batch_id, batch);

        Ok(batch_id)
    }

    async fn get_batch_status(&self, batch_id: BatchId) -> Result<BatchResult> {
        let storage = self.storage.read().await;

        let batch = storage
            .batches
            .get(&batch_id)
            .ok_or_else(|| HammerworkError::Batch {
                message: format!("Batch {} not found", batch_id),
            })?;

        let job_ids = storage
            .batch_jobs
            .get(&batch_id)
            .ok_or_else(|| HammerworkError::Batch {
                message: format!("Batch jobs for {} not found", batch_id),
            })?;

        let mut completed_jobs = 0;
        let mut failed_jobs = 0;
        let mut pending_jobs = 0;
        let mut job_errors = HashMap::new();

        for job_id in job_ids {
            if let Some(job) = storage.jobs.get(job_id) {
                match &job.status {
                    JobStatus::Completed => completed_jobs += 1,
                    JobStatus::Failed | JobStatus::Dead | JobStatus::TimedOut => {
                        failed_jobs += 1;
                        if let Some(error) = &job.error_message {
                            job_errors.insert(job.id, error.clone());
                        }
                    }
                    JobStatus::Pending | JobStatus::Retrying => pending_jobs += 1,
                    JobStatus::Running => pending_jobs += 1, // Count running as pending
                    JobStatus::Archived => {} // Archived jobs don't count in workflow stats
                }
            }
        }

        let total_jobs = job_ids.len() as u32;
        let status = if failed_jobs > 0
            && batch.failure_mode == crate::batch::PartialFailureMode::FailFast
        {
            BatchStatus::Failed
        } else if completed_jobs == total_jobs {
            BatchStatus::Completed
        } else if completed_jobs + failed_jobs == total_jobs {
            BatchStatus::PartiallyFailed
        } else {
            BatchStatus::Processing
        };

        let completed_at = if status == BatchStatus::Completed || status == BatchStatus::Failed {
            Some(storage.clock.now())
        } else {
            None
        };

        Ok(BatchResult {
            batch_id,
            total_jobs,
            completed_jobs,
            failed_jobs,
            pending_jobs,
            status,
            created_at: batch.created_at,
            completed_at,
            error_summary: if !job_errors.is_empty() {
                Some(format!("{} jobs failed", job_errors.len()))
            } else {
                None
            },
            job_errors,
        })
    }

    async fn get_batch_jobs(&self, batch_id: BatchId) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;

        let job_ids = storage
            .batch_jobs
            .get(&batch_id)
            .ok_or_else(|| HammerworkError::Batch {
                message: format!("Batch {} not found", batch_id),
            })?;

        let jobs: Vec<Job> = job_ids
            .iter()
            .filter_map(|id| storage.jobs.get(id).cloned())
            .collect();

        Ok(jobs)
    }

    async fn delete_batch(&self, batch_id: BatchId) -> Result<()> {
        let mut storage = self.storage.write().await;

        // Delete all jobs in the batch
        if let Some(job_ids) = storage.batch_jobs.remove(&batch_id) {
            for job_id in job_ids {
                if let Some(job) = storage.jobs.remove(&job_id) {
                    if let Some(queue_jobs) = storage.queues.get_mut(&job.queue_name) {
                        if let Some(status_jobs) = queue_jobs.get_mut(&job.status) {
                            status_jobs.retain(|id| *id != job_id);
                        }
                    }
                }
            }
        }

        storage.batches.remove(&batch_id);
        Ok(())
    }

    // Dead job management
    async fn mark_job_dead(&self, job_id: JobId, error_message: &str) -> Result<()> {
        let mut storage = self.storage.write().await;

        storage.update_job_status(job_id, JobStatus::Dead)?;

        let now = storage.clock.now();
        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.failed_at = Some(now);
            job.error_message = Some(error_message.to_string());
        }

        Ok(())
    }

    async fn mark_job_timed_out(&self, job_id: JobId, error_message: &str) -> Result<()> {
        let mut storage = self.storage.write().await;

        storage.update_job_status(job_id, JobStatus::TimedOut)?;

        let now = storage.clock.now();
        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.timed_out_at = Some(now);
            job.error_message = Some(error_message.to_string());
        }

        Ok(())
    }

    async fn get_dead_jobs(&self, limit: Option<u32>, offset: Option<u32>) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;

        let mut dead_jobs: Vec<Job> = storage
            .jobs
            .values()
            .filter(|job| job.status == JobStatus::Dead || job.status == JobStatus::TimedOut)
            .cloned()
            .collect();

        // Sort by failed_at or timed_out_at (newest first)
        dead_jobs.sort_by(|a, b| {
            let a_time = a.failed_at.or(a.timed_out_at).unwrap_or(a.created_at);
            let b_time = b.failed_at.or(b.timed_out_at).unwrap_or(b.created_at);
            b_time.cmp(&a_time)
        });

        // Apply offset and limit
        let offset = offset.unwrap_or(0) as usize;
        let limit = limit.unwrap_or(100) as usize;

        Ok(dead_jobs.into_iter().skip(offset).take(limit).collect())
    }

    async fn get_dead_jobs_by_queue(
        &self,
        queue_name: &str,
        limit: Option<u32>,
        offset: Option<u32>,
    ) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;

        let mut dead_jobs: Vec<Job> = storage
            .jobs
            .values()
            .filter(|job| {
                job.queue_name == queue_name
                    && (job.status == JobStatus::Dead || job.status == JobStatus::TimedOut)
            })
            .cloned()
            .collect();

        // Sort by failed_at or timed_out_at (newest first)
        dead_jobs.sort_by(|a, b| {
            let a_time = a.failed_at.or(a.timed_out_at).unwrap_or(a.created_at);
            let b_time = b.failed_at.or(b.timed_out_at).unwrap_or(b.created_at);
            b_time.cmp(&a_time)
        });

        // Apply offset and limit
        let offset = offset.unwrap_or(0) as usize;
        let limit = limit.unwrap_or(100) as usize;

        Ok(dead_jobs.into_iter().skip(offset).take(limit).collect())
    }

    async fn retry_dead_job(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;

        let job = storage
            .jobs
            .get(&job_id)
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        if job.status != JobStatus::Dead && job.status != JobStatus::TimedOut {
            return Err(HammerworkError::Queue {
                message: format!("Job {} is not dead", job_id),
            });
        }

        storage.update_job_status(job_id, JobStatus::Pending)?;

        let now = storage.clock.now();
        if let Some(job) = storage.jobs.get_mut(&job_id) {
            job.attempts = 0;
            job.error_message = None;
            job.failed_at = None;
            job.timed_out_at = None;
            job.scheduled_at = now;
        }

        Ok(())
    }

    async fn purge_dead_jobs(&self, older_than: DateTime<Utc>) -> Result<u64> {
        let mut storage = self.storage.write().await;

        let dead_job_ids: Vec<JobId> = storage
            .jobs
            .iter()
            .filter(|(_, job)| {
                (job.status == JobStatus::Dead || job.status == JobStatus::TimedOut)
                    && job.failed_at.or(job.timed_out_at).unwrap_or(job.created_at) <= older_than
            })
            .map(|(id, _)| *id)
            .collect();

        let count = dead_job_ids.len() as u64;

        for job_id in dead_job_ids {
            if let Some(job) = storage.jobs.remove(&job_id) {
                if let Some(queue_jobs) = storage.queues.get_mut(&job.queue_name) {
                    if let Some(status_jobs) = queue_jobs.get_mut(&job.status) {
                        status_jobs.retain(|id| *id != job_id);
                    }
                }
            }
        }

        Ok(count)
    }

    async fn get_dead_job_summary(&self) -> Result<DeadJobSummary> {
        let storage = self.storage.read().await;

        let mut dead_jobs_by_queue: HashMap<String, u64> = HashMap::new();
        let mut error_patterns: HashMap<String, u64> = HashMap::new();
        let mut total_dead_jobs = 0;
        let mut oldest_dead_job: Option<DateTime<Utc>> = None;
        let mut newest_dead_job: Option<DateTime<Utc>> = None;

        for job in storage.jobs.values() {
            if job.status == JobStatus::Dead || job.status == JobStatus::TimedOut {
                total_dead_jobs += 1;

                *dead_jobs_by_queue
                    .entry(job.queue_name.clone())
                    .or_insert(0) += 1;

                if let Some(error) = &job.error_message {
                    let error_key = error.split('\n').next().unwrap_or(error).to_string();
                    *error_patterns.entry(error_key).or_insert(0) += 1;
                }

                let job_time = job.failed_at.or(job.timed_out_at).unwrap_or(job.created_at);

                if oldest_dead_job.is_none() || job_time < oldest_dead_job.unwrap() {
                    oldest_dead_job = Some(job_time);
                }

                if newest_dead_job.is_none() || job_time > newest_dead_job.unwrap() {
                    newest_dead_job = Some(job_time);
                }
            }
        }

        Ok(DeadJobSummary {
            total_dead_jobs,
            dead_jobs_by_queue,
            error_patterns,
            oldest_dead_job,
            newest_dead_job,
        })
    }

    // Statistics and monitoring
    async fn get_queue_stats(&self, queue_name: &str) -> Result<QueueStats> {
        let storage = self.storage.read().await;

        let queue_jobs = storage
            .queues
            .get(queue_name)
            .ok_or_else(|| HammerworkError::Queue {
                message: format!("Queue {} not found", queue_name),
            })?;

        let pending_count = queue_jobs
            .get(&JobStatus::Pending)
            .map(|j| j.len())
            .unwrap_or(0) as u64;
        let running_count = queue_jobs
            .get(&JobStatus::Running)
            .map(|j| j.len())
            .unwrap_or(0) as u64;
        let completed_count = queue_jobs
            .get(&JobStatus::Completed)
            .map(|j| j.len())
            .unwrap_or(0) as u64;
        let dead_count = queue_jobs
            .get(&JobStatus::Dead)
            .map(|j| j.len())
            .unwrap_or(0) as u64;
        let timed_out_count = queue_jobs
            .get(&JobStatus::TimedOut)
            .map(|j| j.len())
            .unwrap_or(0) as u64;

        // Calculate processing times for completed jobs
        let completed_jobs: Vec<&Job> = queue_jobs
            .get(&JobStatus::Completed)
            .map(|job_ids| {
                job_ids
                    .iter()
                    .filter_map(|id| storage.jobs.get(id))
                    .collect()
            })
            .unwrap_or_default();

        let processing_times: Vec<i64> = completed_jobs
            .iter()
            .filter_map(|job| match (job.started_at, job.completed_at) {
                (Some(start), Some(end)) => Some((end - start).num_milliseconds()),
                _ => None,
            })
            .collect();

        let avg_processing_time_ms = if !processing_times.is_empty() {
            processing_times.iter().sum::<i64>() as f64 / processing_times.len() as f64
        } else {
            0.0
        };

        let min_processing_time_ms = processing_times.iter().min().copied().unwrap_or(0) as u64;
        let max_processing_time_ms = processing_times.iter().max().copied().unwrap_or(0) as u64;

        // Get throughput (jobs completed in last minute)
        let one_minute_ago = storage.clock.now() - chrono::Duration::minutes(1);
        let jobs_per_minute = completed_jobs
            .iter()
            .filter(|job| job.completed_at.unwrap_or(job.created_at) > one_minute_ago)
            .count() as f64;

        let total_processed = completed_count + dead_count + timed_out_count;
        let failed_count = queue_jobs
            .get(&JobStatus::Failed)
            .map(|j| j.len())
            .unwrap_or(0) as u64
            + dead_count;
        let error_rate = if total_processed > 0 {
            (failed_count + timed_out_count) as f64 / total_processed as f64
        } else {
            0.0
        };

        let statistics = crate::stats::JobStatistics {
            total_processed,
            completed: completed_count,
            failed: failed_count,
            dead: dead_count,
            timed_out: timed_out_count,
            running: running_count,
            avg_processing_time_ms,
            min_processing_time_ms,
            max_processing_time_ms,
            throughput_per_minute: jobs_per_minute,
            error_rate,
            priority_stats: None,
            time_window: std::time::Duration::from_secs(3600),
            calculated_at: storage.clock.now(),
        };

        Ok(QueueStats {
            queue_name: queue_name.to_string(),
            pending_count,
            running_count,
            completed_count,
            dead_count,
            timed_out_count,
            statistics,
        })
    }

    async fn get_all_queue_stats(&self) -> Result<Vec<QueueStats>> {
        let storage = self.storage.read().await;
        let queue_names: Vec<String> = storage.queues.keys().cloned().collect();
        drop(storage);

        let mut stats = Vec::new();
        for queue_name in queue_names {
            if let Ok(queue_stats) = self.get_queue_stats(&queue_name).await {
                stats.push(queue_stats);
            }
        }

        Ok(stats)
    }

    async fn get_priority_stats(&self, queue_name: &str) -> Result<crate::priority::PriorityStats> {
        let storage = self.storage.read().await;

        let mut job_counts = std::collections::HashMap::new();
        let mut avg_processing_times = std::collections::HashMap::new();
        let mut recent_throughput = std::collections::HashMap::new();
        let mut priority_distribution = std::collections::HashMap::new();

        // Initialize counts for all priorities
        for priority in [
            crate::priority::JobPriority::Background,
            crate::priority::JobPriority::Low,
            crate::priority::JobPriority::Normal,
            crate::priority::JobPriority::High,
            crate::priority::JobPriority::Critical,
        ] {
            job_counts.insert(priority, 0);
            avg_processing_times.insert(priority, 0.0);
            recent_throughput.insert(priority, 0);
            priority_distribution.insert(priority, 0.0);
        }

        let mut total_jobs = 0u64;

        if let Some(queue_jobs) = storage.queues.get(queue_name) {
            for jobs in queue_jobs.values() {
                for job_id in jobs {
                    if let Some(job) = storage.jobs.get(job_id) {
                        *job_counts.entry(job.priority).or_insert(0) += 1;
                        total_jobs += 1;

                        // Calculate processing time if job is completed
                        if let (Some(started), Some(completed)) = (job.started_at, job.completed_at)
                        {
                            let processing_time = (completed - started).num_milliseconds() as f64;
                            let current_avg =
                                avg_processing_times.get(&job.priority).unwrap_or(&0.0);
                            let current_count = *job_counts.get(&job.priority).unwrap_or(&1);
                            let new_avg = (current_avg * (current_count - 1) as f64
                                + processing_time)
                                / current_count as f64;
                            avg_processing_times.insert(job.priority, new_avg);
                        }
                    }
                }
            }
        }

        // Calculate priority distribution percentages
        for (priority, count) in &job_counts {
            let percentage = if total_jobs > 0 {
                (*count as f32 / total_jobs as f32) * 100.0
            } else {
                0.0
            };
            priority_distribution.insert(*priority, percentage);
        }

        Ok(crate::priority::PriorityStats {
            job_counts,
            avg_processing_times,
            recent_throughput,
            priority_distribution,
        })
    }

    async fn get_job_counts_by_status(&self, queue_name: &str) -> Result<HashMap<String, u64>> {
        let storage = self.storage.read().await;

        let mut counts = HashMap::new();

        if let Some(queue_jobs) = storage.queues.get(queue_name) {
            for (status, jobs) in queue_jobs {
                let status_str = match status {
                    JobStatus::Pending => "pending",
                    JobStatus::Running => "running",
                    JobStatus::Completed => "completed",
                    JobStatus::Failed => "failed",
                    JobStatus::Dead => "dead",
                    JobStatus::TimedOut => "timed_out",
                    JobStatus::Retrying => "retrying",
                    JobStatus::Archived => "archived",
                };
                counts.insert(status_str.to_string(), jobs.len() as u64);
            }
        }

        Ok(counts)
    }

    async fn get_processing_times(
        &self,
        queue_name: &str,
        since: DateTime<Utc>,
    ) -> Result<Vec<i64>> {
        let storage = self.storage.read().await;

        let processing_times: Vec<i64> = storage
            .jobs
            .values()
            .filter(|job| {
                job.queue_name == queue_name
                    && job.status == JobStatus::Completed
                    && job.completed_at.unwrap_or(job.created_at) >= since
            })
            .filter_map(|job| match (job.started_at, job.completed_at) {
                (Some(start), Some(end)) => Some((end - start).num_milliseconds()),
                _ => None,
            })
            .collect();

        Ok(processing_times)
    }

    async fn get_error_frequencies(
        &self,
        queue_name: Option<&str>,
        since: DateTime<Utc>,
    ) -> Result<HashMap<String, u64>> {
        let storage = self.storage.read().await;

        let mut frequencies = HashMap::new();

        for job in storage.jobs.values() {
            if let Some(qn) = queue_name {
                if job.queue_name != qn {
                    continue;
                }
            }

            if (job.status == JobStatus::Failed
                || job.status == JobStatus::Dead
                || job.status == JobStatus::TimedOut)
                && job.failed_at.or(job.timed_out_at).unwrap_or(job.created_at) >= since
            {
                if let Some(error) = &job.error_message {
                    let error_key = error.split('\n').next().unwrap_or(error).to_string();
                    *frequencies.entry(error_key).or_insert(0) += 1;
                }
            }
        }

        Ok(frequencies)
    }

    async fn get_jobs_completed_in_range(
        &self,
        queue_name: Option<&str>,
        start_time: DateTime<Utc>,
        end_time: DateTime<Utc>,
        limit: Option<u32>,
    ) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;
        let mut matching_jobs = Vec::new();

        for job in storage.jobs.values() {
            // Filter by queue name if specified
            if let Some(qn) = queue_name {
                if job.queue_name != qn {
                    continue;
                }
            }

            // Check if job is completed and within time range
            if job.status == JobStatus::Completed {
                if let Some(completed_at) = job.completed_at {
                    if completed_at >= start_time && completed_at < end_time {
                        matching_jobs.push(job.clone());
                    }
                }
            }
        }

        // Sort by completion time (most recent first)
        matching_jobs.sort_by(|a, b| b.completed_at.cmp(&a.completed_at));

        // Apply limit if specified
        if let Some(limit) = limit {
            matching_jobs.truncate(limit as usize);
        }

        Ok(matching_jobs)
    }

    // Cron job management
    async fn enqueue_cron_job(&self, mut job: Job) -> Result<JobId> {
        if job.cron_schedule.is_none() {
            return Err(HammerworkError::Queue {
                message: "Job must have a cron schedule".to_string(),
            });
        }

        job.recurring = true;

        // Calculate initial next_run_at
        if let Ok(schedule) = job
            .cron_schedule
            .as_ref()
            .unwrap()
            .parse::<cron::Schedule>()
        {
            let timezone = job
                .timezone
                .as_ref()
                .and_then(|tz| tz.parse::<chrono_tz::Tz>().ok())
                .unwrap_or(chrono_tz::UTC);

            let now = self.clock.now();
            let now_in_tz = now.with_timezone(&timezone);

            if let Some(next) = schedule.after(&now_in_tz).next() {
                job.next_run_at = Some(next.with_timezone(&Utc));
                job.scheduled_at = job.next_run_at.unwrap();
            }
        } else {
            return Err(HammerworkError::Queue {
                message: format!("Invalid cron schedule: {:?}", job.cron_schedule),
            });
        }

        self.enqueue(job).await
    }

    async fn get_due_cron_jobs(&self, queue_name: Option<&str>) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;
        let now = storage.clock.now();

        let due_jobs: Vec<Job> = storage
            .jobs
            .values()
            .filter(|job| {
                job.recurring
                    && job.status == JobStatus::Completed
                    && job.next_run_at.map(|next| next <= now).unwrap_or(false)
                    && (queue_name.is_none() || job.queue_name == queue_name.unwrap())
            })
            .cloned()
            .collect();

        Ok(due_jobs)
    }

    async fn reschedule_cron_job(&self, job_id: JobId, next_run_at: DateTime<Utc>) -> Result<()> {
        let mut storage = self.storage.write().await;

        let job = storage
            .jobs
            .get_mut(&job_id)
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        if !job.recurring {
            return Err(HammerworkError::Queue {
                message: "Job is not a recurring job".to_string(),
            });
        }

        job.next_run_at = Some(next_run_at);

        // Create a new job instance for the next run
        let mut new_job = job.clone();
        new_job.id = Uuid::new_v4();
        new_job.status = JobStatus::Pending;
        new_job.attempts = 0;
        new_job.created_at = storage.clock.now();
        new_job.scheduled_at = next_run_at;
        new_job.started_at = None;
        new_job.completed_at = None;
        new_job.failed_at = None;
        new_job.timed_out_at = None;
        new_job.error_message = None;

        storage.jobs.insert(new_job.id, new_job.clone());
        storage.add_job_to_queue(&new_job);

        Ok(())
    }

    async fn get_recurring_jobs(&self, queue_name: &str) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;

        let recurring_jobs: Vec<Job> = storage
            .jobs
            .values()
            .filter(|job| job.queue_name == queue_name && job.recurring)
            .cloned()
            .collect();

        Ok(recurring_jobs)
    }

    async fn disable_recurring_job(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;

        let job = storage
            .jobs
            .get_mut(&job_id)
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        if !job.recurring {
            return Err(HammerworkError::Queue {
                message: "Job is not a recurring job".to_string(),
            });
        }

        job.recurring = false;
        job.next_run_at = None;

        Ok(())
    }

    async fn enable_recurring_job(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;

        // First, check if job exists and has cron schedule
        let cron_schedule = {
            let job = storage
                .jobs
                .get(&job_id)
                .ok_or_else(|| HammerworkError::JobNotFound {
                    id: job_id.to_string(),
                })?;

            if job.cron_schedule.is_none() {
                return Err(HammerworkError::Queue {
                    message: "Job does not have a cron schedule".to_string(),
                });
            }

            (job.cron_schedule.clone(), job.timezone.clone())
        };

        // Get current time
        let now = storage.clock.now();

        // Calculate next run time
        let next_run_at =
            if let Ok(schedule) = cron_schedule.0.as_ref().unwrap().parse::<cron::Schedule>() {
                let timezone = cron_schedule
                    .1
                    .as_ref()
                    .and_then(|tz| tz.parse::<chrono_tz::Tz>().ok())
                    .unwrap_or(chrono_tz::UTC);

                let now_in_tz = now.with_timezone(&timezone);
                schedule
                    .after(&now_in_tz)
                    .next()
                    .map(|next| next.with_timezone(&Utc))
            } else {
                None
            };

        // Now update the job
        let job = storage
            .jobs
            .get_mut(&job_id)
            .ok_or_else(|| HammerworkError::JobNotFound {
                id: job_id.to_string(),
            })?;

        job.recurring = true;
        job.next_run_at = next_run_at;

        Ok(())
    }

    // Throttling configuration
    async fn set_throttle_config(&self, queue_name: &str, config: ThrottleConfig) -> Result<()> {
        let mut storage = self.storage.write().await;
        storage
            .throttle_configs
            .insert(queue_name.to_string(), config);
        Ok(())
    }

    async fn get_throttle_config(&self, queue_name: &str) -> Result<Option<ThrottleConfig>> {
        let storage = self.storage.read().await;
        Ok(storage.throttle_configs.get(queue_name).cloned())
    }

    async fn remove_throttle_config(&self, queue_name: &str) -> Result<()> {
        let mut storage = self.storage.write().await;
        storage.throttle_configs.remove(queue_name);
        Ok(())
    }

    async fn get_all_throttle_configs(&self) -> Result<HashMap<String, ThrottleConfig>> {
        let storage = self.storage.read().await;
        Ok(storage.throttle_configs.clone())
    }

    async fn get_queue_depth(&self, queue_name: &str) -> Result<u64> {
        let storage = self.storage.read().await;

        let depth = storage
            .queues
            .get(queue_name)
            .and_then(|q| q.get(&JobStatus::Pending))
            .map(|jobs| jobs.len() as u64)
            .unwrap_or(0);

        Ok(depth)
    }

    // Job result storage
    async fn store_job_result(
        &self,
        job_id: JobId,
        result_data: serde_json::Value,
        expires_at: Option<DateTime<Utc>>,
    ) -> Result<()> {
        let mut storage = self.storage.write().await;

        // Verify job exists
        if !storage.jobs.contains_key(&job_id) {
            return Err(HammerworkError::JobNotFound {
                id: job_id.to_string(),
            });
        }

        storage
            .job_results
            .insert(job_id, (result_data, expires_at));
        Ok(())
    }

    async fn get_job_result(&self, job_id: JobId) -> Result<Option<serde_json::Value>> {
        let storage = self.storage.read().await;

        if let Some((result, expires_at)) = storage.job_results.get(&job_id) {
            // Check if expired
            if let Some(exp) = expires_at {
                if *exp <= storage.clock.now() {
                    return Ok(None);
                }
            }
            Ok(Some(result.clone()))
        } else {
            Ok(None)
        }
    }

    async fn delete_job_result(&self, job_id: JobId) -> Result<()> {
        let mut storage = self.storage.write().await;
        storage.job_results.remove(&job_id);
        Ok(())
    }

    async fn cleanup_expired_results(&self) -> Result<u64> {
        let mut storage = self.storage.write().await;
        let now = storage.clock.now();

        let expired_ids: Vec<JobId> = storage
            .job_results
            .iter()
            .filter(|(_, (_, expires_at))| expires_at.map(|exp| exp <= now).unwrap_or(false))
            .map(|(id, _)| *id)
            .collect();

        let count = expired_ids.len() as u64;

        for id in expired_ids {
            storage.job_results.remove(&id);
        }

        Ok(count)
    }

    // Workflow and dependency management
    async fn enqueue_workflow(&self, mut workflow: JobGroup) -> Result<WorkflowId> {
        let mut storage = self.storage.write().await;

        workflow.created_at = storage.clock.now();
        let workflow_id = workflow.id;

        // Store dependencies
        for (job_id, deps) in &workflow.dependencies {
            storage.dependencies.insert(*job_id, deps.clone());

            // Store reverse dependencies
            for dep_id in deps {
                storage.dependents.entry(*dep_id).or_default().push(*job_id);
            }
        }

        // Enqueue all jobs
        for mut job in workflow.jobs.iter().cloned() {
            // Apply the same timestamp logic as regular enqueue
            let now = storage.clock.now();
            let original_scheduled_at = job.scheduled_at;
            let original_created_at = job.created_at;

            // Calculate the delay from the original creation
            let delay = original_scheduled_at - original_created_at;

            job.created_at = now;

            // Check if job has dependencies
            let has_deps = workflow
                .dependencies
                .get(&job.id)
                .map(|deps| !deps.is_empty())
                .unwrap_or(false);

            // If job has dependencies, don't make it immediately available
            if has_deps {
                job.scheduled_at = DateTime::<Utc>::MAX_UTC; // Far future
            } else {
                // For jobs without dependencies, apply normal timestamp logic
                if original_scheduled_at == original_created_at {
                    job.scheduled_at = now;
                } else {
                    // For delayed jobs, preserve the delay but use mock clock time as base
                    job.scheduled_at = now + delay;
                }
            }

            storage.jobs.insert(job.id, job.clone());
            storage.add_job_to_queue(&job);
        }

        storage.workflows.insert(workflow_id, workflow);
        Ok(workflow_id)
    }

    async fn get_workflow_status(&self, workflow_id: WorkflowId) -> Result<Option<JobGroup>> {
        let storage = self.storage.read().await;

        if let Some(mut workflow) = storage.workflows.get(&workflow_id).cloned() {
            // Update workflow status based on job statuses
            let mut completed = 0;
            let mut failed = 0;

            for job_id in workflow.jobs.iter().map(|j| j.id) {
                if let Some(job) = storage.jobs.get(&job_id) {
                    match &job.status {
                        JobStatus::Completed => completed += 1,
                        JobStatus::Failed | JobStatus::Dead | JobStatus::TimedOut => failed += 1,
                        _ => {}
                    }
                }
            }

            workflow.completed_jobs = completed;
            workflow.failed_jobs = failed;

            // Only update status if it's not already set to a terminal state
            if workflow.status != WorkflowStatus::Cancelled {
                if failed > 0 {
                    workflow.status = WorkflowStatus::Failed;
                    workflow.failed_at = Some(storage.clock.now());
                } else if completed == workflow.total_jobs {
                    workflow.status = WorkflowStatus::Completed;
                    workflow.completed_at = Some(storage.clock.now());
                } else {
                    workflow.status = WorkflowStatus::Running;
                }
            }

            Ok(Some(workflow))
        } else {
            Ok(None)
        }
    }

    async fn resolve_job_dependencies(&self, completed_job_id: JobId) -> Result<Vec<JobId>> {
        let mut storage = self.storage.write().await;
        let mut resolved_jobs = Vec::new();

        if let Some(dependents) = storage.dependents.get(&completed_job_id).cloned() {
            for dependent_id in dependents {
                // Check if all dependencies are satisfied
                if let Some(deps) = storage.dependencies.get(&dependent_id) {
                    let all_complete = deps.iter().all(|dep_id| {
                        storage
                            .jobs
                            .get(dep_id)
                            .map(|j| j.status == JobStatus::Completed)
                            .unwrap_or(false)
                    });

                    if all_complete {
                        // Make the job eligible for execution
                        let now = storage.clock.now();
                        if let Some(job) = storage.jobs.get_mut(&dependent_id) {
                            if job.status == JobStatus::Pending
                                && job.scheduled_at == DateTime::<Utc>::MAX_UTC
                            {
                                job.scheduled_at = now;
                                resolved_jobs.push(dependent_id);
                            }
                        }
                    }
                }
            }
        }

        Ok(resolved_jobs)
    }

    async fn get_ready_jobs(&self, queue_name: &str, limit: u32) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;
        let now = storage.clock.now();

        let ready_jobs: Vec<Job> = storage
            .jobs
            .values()
            .filter(|job| {
                job.queue_name == queue_name &&
                job.status == JobStatus::Pending &&
                job.scheduled_at <= now &&
                // Check that all dependencies are satisfied
                storage.dependencies.get(&job.id)
                    .map(|deps| deps.is_empty() || deps.iter().all(|dep_id| {
                        storage.jobs.get(dep_id)
                            .map(|j| j.status == JobStatus::Completed)
                            .unwrap_or(false)
                    }))
                    .unwrap_or(true)
            })
            .take(limit as usize)
            .cloned()
            .collect();

        Ok(ready_jobs)
    }

    async fn fail_job_dependencies(&self, failed_job_id: JobId) -> Result<Vec<JobId>> {
        let mut storage = self.storage.write().await;
        let mut failed_jobs = Vec::new();

        // Get the workflow this job belongs to
        let workflow = storage
            .workflows
            .values()
            .find(|w| w.jobs.iter().any(|j| j.id == failed_job_id))
            .cloned();

        if let Some(workflow) = workflow {
            // Check failure policy
            match workflow.failure_policy {
                crate::workflow::FailurePolicy::ContinueOnFailure => {
                    // Don't fail dependencies, continue with independent jobs
                }
                crate::workflow::FailurePolicy::Manual => {
                    // Don't automatically fail dependencies, wait for manual intervention
                }
                crate::workflow::FailurePolicy::FailFast => {
                    // Fail all pending jobs in the workflow
                    let current_time = storage.clock.now();
                    for job in &workflow.jobs {
                        if let Some(j) = storage.jobs.get(&job.id) {
                            if j.status == JobStatus::Pending {
                                storage.update_job_status(job.id, JobStatus::Failed).ok();
                                if let Some(j) = storage.jobs.get_mut(&job.id) {
                                    j.error_message = Some("Dependency failed".to_string());
                                    j.failed_at = Some(current_time);
                                }
                                failed_jobs.push(job.id);
                            }
                        }
                    }
                }
            }
        }

        Ok(failed_jobs)
    }

    async fn get_workflow_jobs(&self, workflow_id: WorkflowId) -> Result<Vec<Job>> {
        let storage = self.storage.read().await;

        let workflow =
            storage
                .workflows
                .get(&workflow_id)
                .ok_or_else(|| HammerworkError::Workflow {
                    message: format!("Workflow {} not found", workflow_id),
                })?;

        let jobs: Vec<Job> = workflow
            .jobs
            .iter()
            .filter_map(|job| storage.jobs.get(&job.id).cloned())
            .collect();

        Ok(jobs)
    }

    async fn cancel_workflow(&self, workflow_id: WorkflowId) -> Result<()> {
        let mut storage = self.storage.write().await;

        let workflow = storage
            .workflows
            .get(&workflow_id)
            .ok_or_else(|| HammerworkError::Workflow {
                message: format!("Workflow {} not found", workflow_id),
            })?
            .clone();

        // Cancel all pending jobs
        let now = storage.clock.now();
        for job in &workflow.jobs {
            if let Some(j) = storage.jobs.get(&job.id) {
                if j.status == JobStatus::Pending || j.status == JobStatus::Running {
                    storage.update_job_status(job.id, JobStatus::Failed).ok();
                    if let Some(j) = storage.jobs.get_mut(&job.id) {
                        j.error_message = Some("Workflow cancelled".to_string());
                        j.failed_at = Some(now);
                    }
                }
            }
        }

        // Update workflow status
        if let Some(workflow) = storage.workflows.get_mut(&workflow_id) {
            workflow.status = WorkflowStatus::Cancelled;
            workflow.failed_at = Some(now);
        }

        Ok(())
    }

    // Archival methods
    async fn archive_jobs(
        &self,
        queue_name: Option<&str>,
        policy: &crate::archive::ArchivalPolicy,
        _config: &crate::archive::ArchivalConfig,
        _reason: crate::archive::ArchivalReason,
        _archived_by: Option<&str>,
    ) -> Result<crate::archive::ArchivalStats> {
        let mut storage = self.storage.write().await;
        let now = storage.clock.now();
        let mut jobs_archived = 0u64;
        let mut bytes_archived = 0u64;

        // Find jobs to archive based on policy
        let mut jobs_to_archive = Vec::new();

        for job in storage.jobs.values() {
            // Skip if queue_name filter doesn't match
            if let Some(queue) = queue_name {
                if job.queue_name != queue {
                    continue;
                }
            }

            // Check if job should be archived based on policy and status
            let age = match job.status {
                JobStatus::Completed => job.completed_at.map(|t| now - t),
                JobStatus::Failed => job.failed_at.map(|t| now - t),
                JobStatus::Dead => job.failed_at.map(|t| now - t), // Use failed_at for dead jobs
                JobStatus::TimedOut => job.timed_out_at.map(|t| now - t),
                _ => None,
            };

            if let Some(age) = age {
                if policy.should_archive(&job.status, age) {
                    jobs_to_archive.push(job.id);
                }
            }
        }

        // Archive jobs
        for job_id in jobs_to_archive.iter().take(policy.batch_size) {
            if let Some(job) = storage.jobs.get_mut(job_id) {
                // Calculate payload size for stats
                let payload_size = serde_json::to_string(&job.payload)
                    .map(|s| s.len() as u64)
                    .unwrap_or(0);

                // Update job status
                job.status = JobStatus::Archived;
                // In a real implementation, we'd move to archive table and compress

                jobs_archived += 1;
                bytes_archived += payload_size;
            }
        }

        // Update queue structure separately to avoid borrow checker issues
        let queue_names: Vec<_> = jobs_to_archive
            .iter()
            .take(policy.batch_size)
            .filter_map(|job_id| {
                storage
                    .jobs
                    .get(job_id)
                    .map(|job| (*job_id, job.queue_name.clone()))
            })
            .collect();

        for (job_id, queue_name) in queue_names {
            if let Some(queue_jobs) = storage.queues.get_mut(&queue_name) {
                // Remove from old status list
                for jobs_list in queue_jobs.values_mut() {
                    jobs_list.retain(|&id| id != job_id);
                }
                // Add to archived list
                queue_jobs
                    .entry(JobStatus::Archived)
                    .or_default()
                    .push(job_id);
            }
        }

        // Calculate compression ratio (mock)
        let compression_ratio = if policy.compress_payloads { 0.7 } else { 1.0 };

        Ok(crate::archive::ArchivalStats {
            jobs_archived,
            jobs_purged: 0,
            bytes_archived,
            bytes_purged: 0,
            compression_ratio,
            last_run_at: now,
            operation_duration: std::time::Duration::from_millis(100), // Mock duration
        })
    }

    async fn restore_archived_job(&self, job_id: JobId) -> Result<Job> {
        let mut storage = self.storage.write().await;

        if let Some(job) = storage.jobs.get_mut(&job_id) {
            if job.status == JobStatus::Archived {
                let queue_name = job.queue_name.clone(); // Clone to avoid borrow checker issues

                // Restore job to pending status
                job.status = JobStatus::Pending;
                let restored_job = job.clone();

                // Update queue structure
                if let Some(queue_jobs) = storage.queues.get_mut(&queue_name) {
                    // Remove from archived list
                    if let Some(archived_jobs) = queue_jobs.get_mut(&JobStatus::Archived) {
                        archived_jobs.retain(|&id| id != job_id);
                    }
                    // Add to pending list
                    queue_jobs
                        .entry(JobStatus::Pending)
                        .or_default()
                        .push(job_id);
                }

                Ok(restored_job)
            } else {
                Err(crate::HammerworkError::Queue {
                    message: format!("Job {} is not archived", job_id),
                })
            }
        } else {
            Err(crate::HammerworkError::Queue {
                message: format!("Job {} not found", job_id),
            })
        }
    }

    async fn list_archived_jobs(
        &self,
        queue_name: Option<&str>,
        limit: Option<u32>,
        offset: Option<u32>,
    ) -> Result<Vec<crate::archive::ArchivedJob>> {
        let storage = self.storage.read().await;
        let now = storage.clock.now();

        let mut archived_jobs = Vec::new();

        for job in storage.jobs.values() {
            if job.status != JobStatus::Archived {
                continue;
            }

            // Filter by queue if specified
            if let Some(queue) = queue_name {
                if job.queue_name != queue {
                    continue;
                }
            }

            let payload_size = serde_json::to_string(&job.payload)
                .map(|s| s.len())
                .unwrap_or(0);

            archived_jobs.push(crate::archive::ArchivedJob {
                id: job.id,
                queue_name: job.queue_name.clone(),
                status: job.status,
                created_at: job.created_at,
                archived_at: now, // Mock archived time
                archival_reason: crate::archive::ArchivalReason::Manual, // Mock reason
                original_payload_size: Some(payload_size),
                payload_compressed: false,             // Mock
                archived_by: Some("test".to_string()), // Mock user
            });
        }

        // Apply pagination
        let offset = offset.unwrap_or(0) as usize;
        let limit = limit.unwrap_or(100) as usize;

        archived_jobs.sort_by(|a, b| b.archived_at.cmp(&a.archived_at));

        if offset < archived_jobs.len() {
            let end = std::cmp::min(offset + limit, archived_jobs.len());
            Ok(archived_jobs[offset..end].to_vec())
        } else {
            Ok(Vec::new())
        }
    }

    async fn purge_archived_jobs(&self, older_than: DateTime<Utc>) -> Result<u64> {
        let mut storage = self.storage.write().await;
        let mut purged_count = 0u64;

        // Find archived jobs older than cutoff
        let jobs_to_purge: Vec<JobId> = storage
            .jobs
            .values()
            .filter(|job| {
                job.status == JobStatus::Archived && job.created_at < older_than // Mock: use created_at as archived_at
            })
            .map(|job| job.id)
            .collect();

        // Remove jobs
        for job_id in jobs_to_purge {
            storage.jobs.remove(&job_id);
            purged_count += 1;

            // Clean up from queue structure
            for queue_jobs in storage.queues.values_mut() {
                for jobs_list in queue_jobs.values_mut() {
                    jobs_list.retain(|&id| id != job_id);
                }
            }
        }

        Ok(purged_count)
    }

    async fn get_archival_stats(
        &self,
        queue_name: Option<&str>,
    ) -> Result<crate::archive::ArchivalStats> {
        let storage = self.storage.read().await;
        let now = storage.clock.now();

        let mut jobs_archived = 0u64;
        let mut bytes_archived = 0u64;

        for job in storage.jobs.values() {
            if job.status != JobStatus::Archived {
                continue;
            }

            // Filter by queue if specified
            if let Some(queue) = queue_name {
                if job.queue_name != queue {
                    continue;
                }
            }

            jobs_archived += 1;
            bytes_archived += serde_json::to_string(&job.payload)
                .map(|s| s.len() as u64)
                .unwrap_or(0);
        }

        Ok(crate::archive::ArchivalStats {
            jobs_archived,
            jobs_purged: 0,
            bytes_archived,
            bytes_purged: 0,
            compression_ratio: 0.8, // Mock compression ratio
            last_run_at: now,
            operation_duration: std::time::Duration::from_millis(50),
        })
    }

    async fn pause_queue(&self, queue_name: &str, paused_by: Option<&str>) -> Result<()> {
        let mut storage = self.storage.write().await;
        let now = storage.clock.now();

        let pause_info = QueuePauseInfo {
            queue_name: queue_name.to_string(),
            paused_at: now,
            paused_by: paused_by.map(|s| s.to_string()),
            reason: None,
        };

        storage
            .paused_queues
            .insert(queue_name.to_string(), pause_info);
        Ok(())
    }

    async fn resume_queue(&self, queue_name: &str, _resumed_by: Option<&str>) -> Result<()> {
        let mut storage = self.storage.write().await;
        storage.paused_queues.remove(queue_name);
        Ok(())
    }

    async fn is_queue_paused(&self, queue_name: &str) -> Result<bool> {
        let storage = self.storage.read().await;
        Ok(storage.paused_queues.contains_key(queue_name))
    }

    async fn get_queue_pause_info(&self, queue_name: &str) -> Result<Option<QueuePauseInfo>> {
        let storage = self.storage.read().await;
        Ok(storage.paused_queues.get(queue_name).cloned())
    }

    async fn get_paused_queues(&self) -> Result<Vec<QueuePauseInfo>> {
        let storage = self.storage.read().await;
        Ok(storage.paused_queues.values().cloned().collect())
    }
}

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

    #[tokio::test]
    async fn test_basic_enqueue_dequeue() {
        let queue = TestQueue::new();

        // Enqueue a job
        let job = Job::new("test_queue".to_string(), json!({"test": true}));
        let job_id = queue.enqueue(job).await.unwrap();

        // Verify job count
        assert_eq!(
            queue.get_job_count("test_queue", &JobStatus::Pending).await,
            1
        );

        // Dequeue the job
        let dequeued = queue.dequeue("test_queue").await.unwrap();
        assert!(dequeued.is_some());
        assert_eq!(dequeued.unwrap().id, job_id);

        // Verify job is now running
        assert_eq!(
            queue.get_job_count("test_queue", &JobStatus::Running).await,
            1
        );
        assert_eq!(
            queue.get_job_count("test_queue", &JobStatus::Pending).await,
            0
        );
    }

    #[tokio::test]
    async fn test_priority_dequeue() {
        let queue = TestQueue::new();

        // Enqueue jobs with different priorities
        let low_job =
            Job::new("test_queue".to_string(), json!({"priority": "low"})).as_low_priority();
        let high_job =
            Job::new("test_queue".to_string(), json!({"priority": "high"})).as_high_priority();
        let normal_job = Job::new("test_queue".to_string(), json!({"priority": "normal"}));

        let _low_id = queue.enqueue(low_job).await.unwrap();
        let high_id = queue.enqueue(high_job).await.unwrap();
        let _normal_id = queue.enqueue(normal_job).await.unwrap();

        // Dequeue should get the high priority job first
        let dequeued = queue.dequeue("test_queue").await.unwrap();
        assert_eq!(dequeued.unwrap().id, high_id);
    }

    #[tokio::test]
    async fn test_delayed_job() {
        let queue = TestQueue::new();
        let clock = queue.clock();

        // Enqueue a delayed job
        let delay = chrono::Duration::hours(1);
        let job = Job::with_delay("test_queue".to_string(), json!({"delayed": true}), delay);
        let job_id = queue.enqueue(job).await.unwrap();

        // Should not be able to dequeue immediately
        let dequeued = queue.dequeue("test_queue").await.unwrap();
        assert!(dequeued.is_none());

        // Advance time
        clock.advance(chrono::Duration::hours(2));

        // Now should be able to dequeue
        let dequeued = queue.dequeue("test_queue").await.unwrap();
        assert_eq!(dequeued.unwrap().id, job_id);
    }

    #[tokio::test]
    async fn test_job_completion() {
        let queue = TestQueue::new();

        // Enqueue and dequeue a job
        let job = Job::new("test_queue".to_string(), json!({"test": true}));
        let job_id = queue.enqueue(job).await.unwrap();
        let _dequeued = queue.dequeue("test_queue").await.unwrap();

        // Complete the job
        queue.complete_job(job_id).await.unwrap();

        // Verify job is completed
        let completed_job = queue.get_job(job_id).await.unwrap().unwrap();
        assert_eq!(completed_job.status, JobStatus::Completed);
        assert!(completed_job.completed_at.is_some());
    }

    #[tokio::test]
    async fn test_job_failure_and_retry() {
        let queue = TestQueue::new();

        // Enqueue a job with multiple retry attempts
        let job = Job::new("test_queue".to_string(), json!({"test": true})).with_max_attempts(3);
        let job_id = queue.enqueue(job).await.unwrap();

        // Dequeue and fail the job
        let _dequeued = queue.dequeue("test_queue").await.unwrap();
        queue.fail_job(job_id, "Test error").await.unwrap();

        // Job should be in retrying status
        let job = queue.get_job(job_id).await.unwrap().unwrap();
        assert_eq!(job.status, JobStatus::Retrying);
        assert_eq!(job.attempts, 1);

        // Retry the job
        let retry_at = queue.clock().now() + chrono::Duration::minutes(5);
        queue.retry_job(job_id, retry_at).await.unwrap();

        // Job should be pending again
        let job = queue.get_job(job_id).await.unwrap().unwrap();
        assert_eq!(job.status, JobStatus::Pending);
        assert_eq!(job.scheduled_at, retry_at);
    }

    #[tokio::test]
    async fn test_batch_operations() {
        let queue = TestQueue::new();

        // Create a batch of jobs
        let jobs = vec![
            Job::new("batch_queue".to_string(), json!({"batch": 1})),
            Job::new("batch_queue".to_string(), json!({"batch": 2})),
            Job::new("batch_queue".to_string(), json!({"batch": 3})),
        ];

        let batch = JobBatch::new("test_batch".to_string()).with_jobs(jobs);
        let batch_id = queue.enqueue_batch(batch).await.unwrap();

        // Get batch status
        let status = queue.get_batch_status(batch_id).await.unwrap();
        assert_eq!(status.total_jobs, 3);
        assert_eq!(status.pending_jobs, 3);
        assert_eq!(status.status, BatchStatus::Processing);

        // Process all jobs
        for _ in 0..3 {
            if let Some(job) = queue.dequeue("batch_queue").await.unwrap() {
                queue.complete_job(job.id).await.unwrap();
            }
        }

        // Check batch is completed
        let status = queue.get_batch_status(batch_id).await.unwrap();
        assert_eq!(status.completed_jobs, 3);
        assert_eq!(status.status, BatchStatus::Completed);
    }

    #[tokio::test]
    async fn test_workflow_with_dependencies() {
        let queue = TestQueue::new();

        // Create jobs
        let job1 = Job::new("workflow_queue".to_string(), json!({"step": 1}));
        let job2 = Job::new("workflow_queue".to_string(), json!({"step": 2}));
        let job3 = Job::new("workflow_queue".to_string(), json!({"step": 3}));

        let job1_id = job1.id;
        let job2_id = job2.id;
        let job3_id = job3.id;

        // Create workflow with dependencies: job2 depends on job1, job3 depends on job2
        let mut dependencies = HashMap::new();
        dependencies.insert(job2_id, vec![job1_id]);
        dependencies.insert(job3_id, vec![job2_id]);

        let mut workflow = JobGroup::new("test_workflow".to_string());
        workflow.jobs = vec![job1, job2, job3];
        workflow.total_jobs = 3;
        workflow.dependencies = dependencies;

        let workflow_id = queue.enqueue_workflow(workflow).await.unwrap();

        // Only job1 should be available initially
        let dequeued = queue.dequeue("workflow_queue").await.unwrap();
        assert_eq!(dequeued.unwrap().id, job1_id);

        // Job2 and job3 should not be available
        assert!(queue.dequeue("workflow_queue").await.unwrap().is_none());

        // Complete job1
        queue.complete_job(job1_id).await.unwrap();

        // Now job2 should be available
        let dequeued = queue.dequeue("workflow_queue").await.unwrap();
        assert_eq!(dequeued.unwrap().id, job2_id);

        // Complete job2
        queue.complete_job(job2_id).await.unwrap();

        // Now job3 should be available
        let dequeued = queue.dequeue("workflow_queue").await.unwrap();
        assert_eq!(dequeued.unwrap().id, job3_id);

        // Complete job3
        queue.complete_job(job3_id).await.unwrap();

        // Check workflow status
        let workflow_status = queue
            .get_workflow_status(workflow_id)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(workflow_status.status, WorkflowStatus::Completed);
        assert_eq!(workflow_status.completed_jobs, 3);
    }

    #[tokio::test]
    async fn test_cron_job() {
        let queue = TestQueue::new();
        let clock = queue.clock();

        // Create a cron job that runs every hour
        let mut job = Job::new("cron_queue".to_string(), json!({"cron": true}));
        job.cron_schedule = Some("0 0 * * * *".to_string());

        let job_id = queue.enqueue_cron_job(job).await.unwrap();

        // Get the job
        let job = queue.get_job(job_id).await.unwrap().unwrap();
        assert!(job.recurring);
        assert!(job.next_run_at.is_some());

        // Advance clock to make the cron job ready for execution
        // Since the cron is "0 0 * * * *" (every hour at minute 0),
        // advance to the next hour boundary
        clock.advance(chrono::Duration::hours(1));

        // Now the job should be available for dequeue
        let dequeued = queue.dequeue("cron_queue").await.unwrap();
        assert!(dequeued.is_some());
        queue.complete_job(job_id).await.unwrap();

        // Job should have next_run_at updated
        let job = queue.get_job(job_id).await.unwrap().unwrap();
        assert!(job.next_run_at.is_some());

        // Advance time to next run
        clock.advance(chrono::Duration::hours(2));

        // Get due cron jobs
        let due_jobs = queue.get_due_cron_jobs(Some("cron_queue")).await.unwrap();
        assert_eq!(due_jobs.len(), 1);
        assert_eq!(due_jobs[0].id, job_id);

        // Reschedule for next run
        let next_run = clock.now() + chrono::Duration::hours(1);
        queue.reschedule_cron_job(job_id, next_run).await.unwrap();

        // Should have created a new job instance
        assert_eq!(
            queue.get_job_count("cron_queue", &JobStatus::Pending).await,
            1
        );
    }

    #[tokio::test]
    async fn test_job_result_storage() {
        let queue = TestQueue::new();
        let clock = queue.clock();

        // Create and enqueue a job
        let job = Job::new("result_queue".to_string(), json!({"test": true}));
        let job_id = queue.enqueue(job).await.unwrap();

        // Store a result
        let result = json!({"success": true, "data": "test result"});
        let expires_at = clock.now() + chrono::Duration::hours(1);
        queue
            .store_job_result(job_id, result.clone(), Some(expires_at))
            .await
            .unwrap();

        // Retrieve the result
        let retrieved = queue.get_job_result(job_id).await.unwrap();
        assert_eq!(retrieved, Some(result));

        // Advance time past expiration
        clock.advance(chrono::Duration::hours(2));

        // Result should be expired
        let retrieved = queue.get_job_result(job_id).await.unwrap();
        assert_eq!(retrieved, None);

        // Cleanup expired results
        let cleaned = queue.cleanup_expired_results().await.unwrap();
        assert_eq!(cleaned, 1);
    }

    #[tokio::test]
    async fn test_queue_stats() {
        let queue = TestQueue::new();

        // Enqueue various jobs
        for i in 0..5 {
            let job = Job::new("stats_queue".to_string(), json!({"index": i}));
            queue.enqueue(job).await.unwrap();
        }

        // Process some jobs
        for _ in 0..2 {
            if let Some(job) = queue.dequeue("stats_queue").await.unwrap() {
                queue.complete_job(job.id).await.unwrap();
            }
        }

        // Fail one job
        if let Some(job) = queue.dequeue("stats_queue").await.unwrap() {
            queue.fail_job(job.id, "Test failure").await.unwrap();
            // Exhaust retries
            queue.fail_job(job.id, "Test failure").await.unwrap();
            queue.fail_job(job.id, "Test failure").await.unwrap();
        }

        // Get stats
        let stats = queue.get_queue_stats("stats_queue").await.unwrap();

        assert_eq!(stats.pending_count, 2);
        assert_eq!(stats.statistics.completed, 2);
        assert_eq!(stats.statistics.dead, 1); // One job marked as dead
        assert_eq!(stats.statistics.total_processed, 3); // 2 completed + 1 dead
    }
}