tiny-counter 0.1.0

Track event counts across time windows with fixed memory and fast queries
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
pub mod builder;
pub(crate) mod inner;
pub mod limiter;
pub mod query;

use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex};

use chrono::{DateTime, Duration, Utc};
use dashmap::DashMap;

use crate::config_converter::convert_if_needed;
use crate::{
    Clock, DeltaQuery, Error, EventCounterConfig, Formatter, MultiQuery, Query, RatioQuery, Result,
    SingleEventCounter, Storage, SystemClock, TimeUnit,
};

use self::inner::EventStoreInner;
use self::limiter::Limiter;

/// Top-level store for tracking multiple events with automatic counter creation.
///
/// EventStore provides a high-level API for recording and querying events.
/// Each event is tracked independently with its own SingleEventCounter.
/// Most methods use &self and internal locking for thread safety.
///
/// # Thread Safety and Sharing
///
/// EventStore is NOT Clone. To share across threads:
/// - Use `Arc<EventStore>` for read-heavy workloads (record, query)
/// - Use `Arc<Mutex<EventStore>>` when calling `compact()` (&mut self methods)
///
/// Auto-persistence is configured via the builder and managed internally.
///
/// # Drop and Synchronous I/O Behavior
///
/// **IMPORTANT**: When an EventStore is dropped, it performs **synchronous I/O** by calling
/// [`persist()`](Self::persist) if any events are dirty. This ensures data is not lost, but has implications:
///
/// ## When Drop I/O Matters
///
/// - **Async contexts**: Drop runs synchronously and will block the executor. This can cause
///   performance issues or even deadlocks in async code.
/// - **Performance-sensitive code**: Drop may take time proportional to the number of dirty events
///   and the storage backend's performance.
/// - **Panic unwinding**: If a panic occurs, Drop still runs, but persistence errors are silently
///   ignored (they cannot be returned during unwinding).
///
/// ## Best Practices for Cleanup
///
/// **Option 1 - Use `close()`** (recommended for clarity):
///
/// ```rust
/// # #[cfg(feature = "serde")]
/// # {
/// use tiny_counter::{EventStore, storage::MemoryStorage};
///
/// # async fn example() {
/// let store = EventStore::builder()
///     .with_storage(MemoryStorage::new())
///     .build()
///     .unwrap();
///
/// store.record("user_action");
///
/// // Explicitly close with error handling
/// store.close().expect("Failed to close store");
/// // Store is now consumed
/// # }
/// # }
/// ```
///
/// **Option 2 - Persist then drop**:
///
/// ```rust
/// # #[cfg(feature = "serde")]
/// # {
/// use tiny_counter::{EventStore, storage::MemoryStorage};
///
/// # async fn example() {
/// let store = EventStore::builder()
///     .with_storage(MemoryStorage::new())
///     .build()
///     .unwrap();
///
/// store.record("user_action");
///
/// // Explicitly persist before drop in async context
/// store.persist().expect("Failed to persist events");
/// // Now drop is safe - either no dirty data, or we already handled errors
/// drop(store);
/// # }
/// # }
/// ```
///
/// **With auto-persist**: If you configured auto-persist via the builder, Drop still performs
/// a final persist to catch any events recorded after the last auto-persist cycle:
///
/// ```rust
/// # #[cfg(all(feature = "tokio", feature = "serde"))]
/// # {
/// use tiny_counter::{EventStore, storage::MemoryStorage};
/// use chrono::Duration;
///
/// # async fn example() {
/// let store = EventStore::builder()
///     .with_storage(MemoryStorage::new())
///     .auto_persist(Duration::seconds(60))  // Background task every 60s
///     .build()
///     .unwrap();
///
/// store.record("event1");
/// // Auto-persist will eventually save this
///
/// tokio::time::sleep(std::time::Duration::from_millis(100)).await;
///
/// store.record("event2");
/// // This might not be saved yet by auto-persist!
///
/// // Option 1: Explicit persist before drop (recommended for critical data)
/// store.persist().expect("Failed to persist");
/// drop(store);
///
/// // Option 2: Let Drop handle it (blocks executor, errors ignored)
/// // drop(store);  // Will call persist() synchronously
/// # }
/// # }
/// ```
///
/// ## Tradeoffs
///
/// - **Drop with persist**: Convenient, prevents data loss, but blocks and may fail silently
/// - **Explicit persist**: More verbose, but allows error handling and avoids blocking Drop
/// - **Auto-persist**: Reduces manual persist calls, but Drop still needed for final flush
///
/// ## Alternative: No-Op Drop
///
/// If you don't configure storage (no `with_storage()` in builder), Drop is a no-op and has
/// no I/O implications.
pub struct EventStore {
    pub(crate) inner: Arc<EventStoreInner>,
    #[cfg(feature = "tokio")]
    pub(crate) auto_persist_handle: Option<tokio::task::JoinHandle<()>>,
}

impl EventStore {
    /// Creates a new EventStore with default configuration.
    ///
    /// Default configuration includes 6 time units totaling 256 buckets:
    /// - 60 Minutes
    /// - 72 Hours
    /// - 56 Days
    /// - 52 Weeks
    /// - 12 Months
    /// - 4 Years
    pub fn new() -> Self {
        Self::from_parts(
            SystemClock::new(),
            None,
            None,
            EventCounterConfig::default(),
        )
    }

    /// Gets current time from clock.
    pub fn clock_now(&self) -> DateTime<Utc> {
        self.inner.clock_now()
    }

    /// Creates an EventStore from individual components.
    ///
    /// This is an internal constructor used by the builder.
    pub(crate) fn from_parts(
        clock: Arc<dyn Clock>,
        storage: Option<Box<dyn Storage>>,
        formatter: Option<Arc<dyn crate::Formatter>>,
        config: EventCounterConfig,
    ) -> Self {
        Self {
            inner: Arc::new(EventStoreInner {
                events: DashMap::new(),
                clock,
                storage: storage.map(|s| Arc::new(Mutex::new(s))),
                formatter,
                config,
            }),
            #[cfg(feature = "tokio")]
            auto_persist_handle: None,
        }
    }

    /// Records a single event at the current time.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record("app_launch");
    /// store.record("button_click");
    /// ```
    pub fn record(&self, event_id: impl EventId) {
        self.record_count(event_id, 1);
    }

    /// Records multiple events at the current time.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record_count("api_call", 5);
    /// store.record_count("page_view", 10);
    /// ```
    pub fn record_count(&self, event_id: impl EventId, count: u32) {
        let counter = self.inner.get_counter_for_record(event_id.as_ref());
        let now = self.inner.clock.now();
        let mut counter = counter.lock().unwrap();
        counter.advance_if_needed(now);
        counter.record(count);
        counter.mark_dirty();
    }

    /// Records a single event at a specific time.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    /// use chrono::{Duration, Utc};
    ///
    /// let store = EventStore::new();
    /// let two_days_ago = Utc::now() - Duration::days(2);
    /// store.record_at("feature_used", two_days_ago).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `Error::FutureEvent` if the timestamp is in the future.
    ///
    /// **Note on old events**: Events beyond the tracking window are silently dropped with no error.
    /// This is intentional - the library uses fixed-size rotating buckets, and old data falls off
    /// as new data arrives. This represents a loss of granularity (e.g., "25 hours ago" becomes
    /// "1 day ago") rather than complete data loss. For production use, prefer `record()` for
    /// real-time events. Use `record_at()` and `record_ago()` primarily for testing and backfilling.
    pub fn record_at(&self, event_id: impl EventId, time: DateTime<Utc>) -> Result<()> {
        self.record_count_at(event_id, 1, time)
    }

    /// Records multiple events at a specific time.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    /// use chrono::{Duration, Utc};
    ///
    /// let store = EventStore::new();
    /// let yesterday = Utc::now() - Duration::days(1);
    /// store.record_count_at("sync_event", 3, yesterday).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `Error::FutureEvent` if the timestamp is in the future.
    ///
    /// **Note on old events**: Events beyond the tracking window are silently dropped with no error.
    /// This is intentional - the library uses fixed-size rotating buckets, and old data falls off
    /// as new data arrives. This represents a loss of granularity (e.g., "25 hours ago" becomes
    /// "1 day ago") rather than complete data loss. For production use, prefer `record()` for
    /// real-time events. Use `record_count_at()` and `record_count_ago()` primarily for testing and backfilling.
    pub fn record_count_at(
        &self,
        event_id: impl EventId,
        count: u32,
        time: DateTime<Utc>,
    ) -> Result<()> {
        let counter = self.inner.get_counter_for_record(event_id.as_ref());
        let now = self.inner.clock.now();
        let mut counter = counter.lock().unwrap();
        counter.advance_if_needed(now);
        counter.record_at(count, time)?;
        counter.mark_dirty();
        Ok(())
    }

    /// Records a single event that occurred a duration ago.
    ///
    /// **Important**: Events outside the tracking window are silently dropped with no error or warning.
    /// The tracking window depends on your configuration. With default settings (256 buckets across
    /// 6 time units), events older than approximately 4 years are dropped.
    ///
    /// This represents a loss of granularity (e.g., "25 hours ago" becomes "1 day ago"), not complete
    /// data loss. The library uses fixed-size rotating buckets - old data falls off as new data arrives.
    ///
    /// **Recommendation**: Use `record()` for production real-time events. Use `record_ago()` primarily
    /// for testing queries or backfilling recent historical data within the tracking window.
    ///
    /// This method never returns an error. Use [`record_at`](Self::record_at) if you need
    /// to detect when events fall outside the tracking window.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    /// use chrono::Duration;
    ///
    /// let store = EventStore::new();
    /// store.record_ago("sync", Duration::hours(3));
    ///
    /// // Events too old are dropped silently
    /// store.record_ago("ancient", Duration::days(365 * 10));
    /// let sum = store.query("ancient").ever().sum();
    /// assert_eq!(sum, Some(0)); // Event was dropped
    /// ```
    pub fn record_ago(&self, event_id: impl EventId, duration: Duration) {
        self.record_count_ago(event_id, 1, duration);
    }

    /// Records multiple events that occurred a duration ago.
    ///
    /// **Important**: Events outside the tracking window are silently dropped with no error or warning.
    /// The tracking window depends on your configuration. With default settings (256 buckets across
    /// 6 time units), events older than approximately 4 years are dropped.
    ///
    /// This represents a loss of granularity (e.g., "25 hours ago" becomes "1 day ago"), not complete
    /// data loss. The library uses fixed-size rotating buckets - old data falls off as new data arrives.
    ///
    /// **Recommendation**: Use `record_count()` for production real-time events. Use `record_count_ago()`
    /// primarily for testing queries or backfilling recent historical data within the tracking window.
    ///
    /// This method never returns an error. Use [`record_count_at`](Self::record_count_at) if you need
    /// to detect when events fall outside the tracking window.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    /// use chrono::Duration;
    ///
    /// let store = EventStore::new();
    /// store.record_count_ago("batch_process", 5, Duration::days(1));
    ///
    /// // Events too old are dropped silently
    /// store.record_count_ago("ancient_batch", 100, Duration::days(365 * 10));
    /// let sum = store.query("ancient_batch").ever().sum();
    /// assert_eq!(sum, Some(0)); // Events were dropped
    /// ```
    pub fn record_count_ago(&self, event_id: impl EventId, count: u32, duration: Duration) {
        let now = self.inner.clock.now();
        let time = now - duration;
        // Events outside tracking window are silently dropped.
        // This matches the API design where record_ago methods are infallible.
        let _ = self.record_count_at(event_id, count, time);
    }

    /// Creates a query builder for a single event.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record("app_launch");
    ///
    /// let count = store.query("app_launch").last_days(7).sum();
    /// assert_eq!(count, Some(1));
    /// ```
    pub fn query(&self, event_id: impl EventId) -> Query {
        Query::new(self.inner.clone(), event_id.as_ref().to_string())
    }

    /// Creates a query builder for multiple events.
    ///
    /// Combines counts from multiple events into a single sum.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record("app_launch");
    /// store.record("app_resume");
    ///
    /// let total_opens = store
    ///     .query_many(&["app_launch", "app_resume"])
    ///     .last_days(7)
    ///     .sum();
    ///
    /// assert_eq!(total_opens, Some(2));
    /// ```
    pub fn query_many(&self, event_ids: &[impl EventId]) -> MultiQuery {
        let event_ids_owned: Vec<String> =
            event_ids.iter().map(|s| s.as_ref().to_string()).collect();
        MultiQuery::new(self.inner.clone(), event_ids_owned)
    }

    /// Creates a ratio query builder for two events.
    ///
    /// Calculates the ratio of numerator to denominator events.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record_count("conversions", 25);
    /// store.record_count("visits", 100);
    ///
    /// let conversion_rate = store
    ///     .query_ratio("conversions", "visits")
    ///     .last_days(7);
    ///
    /// assert_eq!(conversion_rate, Some(0.25));
    /// ```
    pub fn query_ratio(&self, numerator: impl EventId, denominator: impl EventId) -> RatioQuery {
        RatioQuery::new(
            self.inner.clone(),
            numerator.as_ref().to_string(),
            denominator.as_ref().to_string(),
        )
    }

    /// Creates a delta query builder for two events.
    ///
    /// Calculates the net change (positive - negative) between two events.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record_count("items_added", 10);
    /// store.record_count("items_removed", 3);
    ///
    /// let inventory_change = store
    ///     .query_delta("items_added", "items_removed")
    ///     .last_days(7)
    ///     .sum();
    ///
    /// assert_eq!(inventory_change, 7);
    /// ```
    pub fn query_delta(&self, positive: impl EventId, negative: impl EventId) -> DeltaQuery {
        DeltaQuery::new(
            self.inner.clone(),
            positive.as_ref().to_string(),
            negative.as_ref().to_string(),
        )
    }

    /// Returns whether any events have been modified since the last persist.
    pub fn is_dirty(&self) -> bool {
        for entry in self.inner.events.iter() {
            let counter = entry.value().lock().unwrap();
            if counter.is_dirty() {
                return true;
            }
        }
        false
    }

    /// Creates a rate limiter builder for checking constraints.
    ///
    /// Use this to create complex rate limiting rules with multiple constraints.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::{EventStore, TimeUnit};
    ///
    /// let store = EventStore::new();
    ///
    /// let result = store
    ///     .limit()
    ///     .at_most("api_call", 10, TimeUnit::Minutes)
    ///     .at_most("api_call", 100, TimeUnit::Hours)
    ///     .check_and_record("api_call");
    ///
    /// assert!(result.is_ok());
    /// ```
    pub fn limit(&self) -> Limiter {
        Limiter::new(self.inner.clone())
    }

    /// Reconciles the delta between two events and records to balance them.
    ///
    /// This method calculates the all-time delta (positive - negative) and:
    /// - If delta > 0: records delta to the negative event
    /// - If delta < 0: records |delta| to the positive event
    /// - If delta == 0: does nothing
    ///
    /// This is useful for tracking net changes like credits/debits or joins/leaves.
    ///
    /// # Examples
    ///
    /// ```
    /// use tiny_counter::EventStore;
    ///
    /// let store = EventStore::new();
    /// store.record_count("credits", 100);
    /// store.record_count("debits", 30);
    ///
    /// // Balance adds 70 to debits to equalize
    /// store.balance_delta("credits", "debits").unwrap();
    ///
    /// let delta = store.query_delta("credits", "debits").ever().sum();
    /// assert_eq!(delta, 0);
    /// ```
    pub fn balance_delta(&self, positive: impl EventId, negative: impl EventId) -> Result<()> {
        let positive_str = positive.as_ref();
        let negative_str = negative.as_ref();

        // Query all-time delta
        let delta = self.query_delta(positive_str, negative_str).ever().sum();

        if delta > 0 {
            // Positive delta: record to negative event to balance
            self.record_count(negative_str, delta.min(u32::MAX as i64) as u32);
        } else if delta < 0 {
            // Negative delta: record |delta| to positive event to balance
            self.record_count(positive_str, (-delta).min(u32::MAX as i64) as u32);
        }
        // delta == 0: no-op

        Ok(())
    }

    /// Persists only dirty (modified) events to storage.
    ///
    /// This method performs synchronous I/O to save modified events to the configured storage
    /// backend. It's automatically called by the Drop implementation when the EventStore is
    /// dropped, but **explicit calls are recommended** in async contexts and for error handling.
    ///
    /// Returns an error if no storage is configured or if serialization/storage fails.
    /// Requires a formatter - either a built-in formatter (serde-bincode, serde-json) or a custom implementation.
    ///
    /// # Best Practices
    ///
    /// - **Async contexts**: Call `persist()` explicitly before the store goes out of scope
    ///   to avoid blocking the executor during Drop
    /// - **Error handling**: Explicit calls allow you to handle persistence errors, while
    ///   Drop silently ignores errors
    /// - **With auto-persist**: Still call `persist()` before drop to ensure the final batch
    ///   of events is saved and to catch any errors
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "serde")]
    /// # {
    /// use tiny_counter::{EventStore, storage::MemoryStorage};
    ///
    /// let store = EventStore::builder()
    ///     .with_storage(MemoryStorage::new())
    ///     .build()
    ///     .unwrap();
    ///
    /// store.record("event");
    /// assert!(store.is_dirty());
    ///
    /// // Explicit persist with error handling
    /// store.persist().expect("Failed to persist");
    /// assert!(!store.is_dirty());
    /// # }
    /// ```
    pub fn persist(&self) -> Result<()> {
        self.persist_if_dirty(false)
    }

    /// Explicitly persist and close the event store.
    ///
    /// This is a convenience method that calls [`persist()`](Self::persist) and then consumes
    /// the EventStore. It's semantically clearer than calling `persist()` + `drop()` and makes
    /// the intent of cleanup explicit.
    ///
    /// **Preferred over relying on Drop** in production code, especially in:
    /// - Async contexts where Drop would block the executor
    /// - Code where error handling is important
    /// - Shutdown sequences where explicit cleanup is desired
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "serde")]
    /// # {
    /// use tiny_counter::{EventStore, storage::MemoryStorage};
    ///
    /// let store = EventStore::builder()
    ///     .with_storage(MemoryStorage::new())
    ///     .build()
    ///     .unwrap();
    ///
    /// store.record("event");
    ///
    /// // Explicit cleanup with error handling
    /// store.close().expect("Failed to close store");
    /// // Store is now consumed and dropped
    /// # }
    /// ```
    pub fn close(self) -> Result<()> {
        self.persist()
        // self is dropped here after successful persist
    }

    fn persist_if_dirty(&self, force_dirty: bool) -> Result<()> {
        let storage = self
            .inner
            .storage
            .as_ref()
            .ok_or_else(|| Error::Storage("No storage configured".to_string()))?;

        let mut storage = storage.lock().unwrap();

        let formatter = self
            .inner
            .formatter
            .as_ref()
            .ok_or_else(|| Error::Serialization("No formatter configured".to_string()))?;

        // Begin transaction for atomic multi-event persistence
        storage.begin_transaction()?;

        let longest_time_unit = self.inner.config.specified_time_unit(TimeUnit::Ever);
        let persist_result = (|| {
            for entry in self.inner.events.iter() {
                let counter = entry.value();
                let event_id = entry.key();
                self.persist_counter(
                    &mut **storage,
                    &**formatter,
                    event_id,
                    counter,
                    longest_time_unit,
                    force_dirty,
                )?;
            }
            Ok(())
        })();

        // Commit or rollback based on result
        match persist_result {
            Ok(()) => {
                storage.commit_transaction()?;
                Ok(())
            }
            Err(e) => {
                // Attempt rollback, but return original error
                let _ = storage.rollback_transaction();
                Err(e)
            }
        }
    }

    /// Persists all events to storage, regardless of dirty status.
    ///
    /// Returns an error if no storage is configured or if serialization/storage fails.
    pub fn persist_all(&self) -> Result<()> {
        self.persist_if_dirty(true)
    }

    /// Clears the dirty flag on all events without persisting.
    ///
    /// Use this when you want to mark all events as clean without saving.
    pub fn reset_dirty(&self) {
        for entry in self.inner.events.iter() {
            let mut counter = entry.value().lock().unwrap();
            counter.reset_dirty();
        }
    }

    /// Compacts storage by loading all events, advancing to current time,
    /// persisting back to storage, and clearing memory.
    ///
    /// This method:
    /// - Loads all events from storage into memory (triggers convert_if_needed)
    /// - Advances all counters to current time
    /// - Persists all counters (saves non-empty, deletes empty)
    /// - Clears in-memory cache (events will be lazy-loaded as needed)
    ///
    /// # Thread Safety
    ///
    /// Requires `&mut self` for exclusive access. When sharing EventStore across
    /// threads, wrap in `Arc<Mutex<EventStore>>` to safely call this method.
    pub fn compact(&mut self) -> Result<()> {
        // Verify storage is configured
        if self.inner.storage.is_none() {
            return Err(Error::Storage("No storage configured".to_string()));
        }

        // List all keys from storage
        let keys = {
            let storage = self.inner.storage.as_ref().unwrap();
            let storage_guard = storage.lock().unwrap();
            storage_guard.list_keys()?
        };

        let now = self.inner.clock.now();

        // Load all events into memory and advance them
        for key in keys {
            // get_counter_for_query loads from storage if not already in memory
            // and automatically triggers convert_if_needed
            if let Some(counter) = self.inner.get_counter_for_query(&key) {
                let mut counter_guard = counter.lock().unwrap();
                counter_guard.advance_if_needed(now);
            }
        }

        // Persist all in-memory counters (handles save/delete logic)
        self.persist_all()?;

        // Clear in-memory cache to free memory
        self.inner.events.clear();

        Ok(())
    }

    /// Returns the approximate memory usage in bytes for all tracked events.
    ///
    /// This includes the bucket storage for all intervals across all events.
    pub fn memory_usage(&self) -> usize {
        let mut total: usize = 0;
        for entry in self.inner.events.iter() {
            let counter = entry.value().lock().unwrap();
            // Calculate memory for each interval in the counter
            total = total.saturating_add(counter.memory_usage())
        }
        total
    }

    /// Returns a list of tracked time units and their bucket counts.
    ///
    /// This reflects the default configuration used for new events.
    pub fn tracked_intervals(&self) -> Vec<(TimeUnit, usize)> {
        self.inner
            .config
            .as_vec()
            .iter()
            .map(|config| (config.time_unit(), config.bucket_count()))
            .collect()
    }

    /// Exports all event counters.
    ///
    /// Returns a HashMap mapping event IDs to their SingleEventCounter instances.
    /// Useful for serialization, backup, or multi-device sync.
    ///
    /// This method queries storage to find all event_ids on disk and loads any
    /// counters that aren't already in memory, ensuring a complete snapshot.
    pub fn export_all(&self) -> Result<HashMap<String, SingleEventCounter>> {
        let mut result = HashMap::new();

        // Collect all event_ids from both memory and storage
        let mut all_event_ids: HashSet<String> = HashSet::new();

        // Add all event_ids from memory
        for entry in self.inner.events.iter() {
            all_event_ids.insert(entry.key().clone());
        }

        // Add all event_ids from storage (if storage is configured)
        if let Some(storage) = &self.inner.storage {
            let storage_guard = storage.lock().unwrap();
            let storage_keys = storage_guard.list_keys()?;
            drop(storage_guard); // Release lock before loading counters
            for key in storage_keys {
                all_event_ids.insert(key);
            }
        }

        // Load each counter (from memory or storage) and add to result
        for event_id in all_event_ids {
            if let Some(counter_arc) = self.inner.get_counter_for_query(&event_id) {
                let counter = counter_arc.lock().unwrap();
                result.insert(event_id, counter.clone());
            }
        }

        Ok(result)
    }

    /// Exports only dirty (modified) event counters.
    ///
    /// Returns a HashMap of event IDs to SingleEventCounter for events that have
    /// been modified since the last persist or reset_dirty.
    pub fn export_dirty(&self) -> Result<HashMap<String, SingleEventCounter>> {
        let mut result = HashMap::new();
        for entry in self.inner.events.iter() {
            let counter = entry.value().lock().unwrap();
            if counter.is_dirty() {
                result.insert(entry.key().clone(), counter.clone());
            }
        }
        Ok(result)
    }

    /// Imports a single event counter, creating or replacing it.
    ///
    /// If the event already exists, it is replaced entirely (not merged).
    /// Marks the event as dirty after import.
    pub fn import_event(&self, event_id: impl EventId, counter: SingleEventCounter) -> Result<()> {
        let event_id_str = event_id.as_ref();
        let mut counter = convert_if_needed(counter, &self.inner.config);
        counter.mark_dirty();
        self.inner
            .events
            .insert(event_id_str.to_string(), Arc::new(Mutex::new(counter)));
        Ok(())
    }

    /// Imports multiple event counters, creating or replacing them.
    ///
    /// This is a batch version of import_event.
    pub fn import_all(&self, events: HashMap<String, SingleEventCounter>) -> Result<()> {
        for (event_id, counter) in events {
            self.import_event(event_id, counter)?;
        }
        Ok(())
    }

    /// Merges a single event counter into the store.
    ///
    /// If the event doesn't exist, creates it with the merged counter.
    /// If it exists, merges the counts using SingleEventCounter::merge.
    /// Marks the event as dirty after merge.
    pub fn merge_event(&self, event_id: impl EventId, counter: SingleEventCounter) -> Result<()> {
        let event_id_str = event_id.as_ref();
        if let Some(existing_entry) = self.inner.get_counter_for_query(event_id_str) {
            let mut existing = existing_entry.lock().unwrap();
            existing.merge(counter)?;
            existing.mark_dirty();
        } else {
            let mut new_counter = counter;
            new_counter.mark_dirty();
            self.inner
                .events
                .insert(event_id_str.to_string(), Arc::new(Mutex::new(new_counter)));
        }

        Ok(())
    }

    /// Merges multiple event counters into the store.
    ///
    /// This is a batch version of merge_event.
    pub fn merge_all(&self, events: HashMap<String, SingleEventCounter>) -> Result<()> {
        for (event_id, counter) in events {
            self.merge_event(event_id, counter)?;
        }
        Ok(())
    }

    pub fn merge(&self, other: Self) -> Result<()> {
        let events = other.export_all()?;
        self.merge_all(events)
    }

    /// Spawns a background task for automatic persistence.
    ///
    /// This method creates a tokio task that periodically checks if the store
    /// is dirty and persists if needed. The task runs until aborted.
    ///
    /// Returns a JoinHandle that can be used to abort the task.
    #[cfg(feature = "tokio")]
    #[allow(dead_code)] // Will be used by builder in ev-5e4
    pub(crate) fn spawn_auto_persist(
        inner: Arc<EventStoreInner>,
        interval: chrono::Duration,
    ) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            // Convert to std::time::Duration at tokio boundary
            let std_interval = interval
                .to_std()
                .expect("auto_persist interval must be positive");

            // Create a temporary BaseEventStore to call methods
            let store = EventStore {
                inner: inner.clone(),
                auto_persist_handle: None,
            };

            loop {
                tokio::time::sleep(std_interval).await;

                if store.is_dirty() {
                    if let Err(e) = store.persist() {
                        eprintln!("Auto-persist failed: {}", e);
                    }
                }
            }
        })
    }

    fn persist_counter(
        &self,
        storage: &mut dyn Storage,
        formatter: &dyn Formatter,
        event_id: &str,
        counter: &Mutex<SingleEventCounter>,
        longest_time_unit: TimeUnit,
        force_dirty: bool,
    ) -> Result<()> {
        if let Some(data) = {
            // We get the lock, so we can get the data and serialize it.
            let mut counter = counter.lock().unwrap();
            if force_dirty || counter.is_dirty() {
                // We optimistically mark the event as not dirty
                if !counter.is_empty(longest_time_unit) {
                    let data = formatter.serialize(&counter)?;
                    counter.reset_dirty();
                    Some(data)
                } else {
                    counter.reset_dirty();
                    None
                }
            } else {
                return Ok(());
            }
            // We release the lock here, with the data serialized, and marked as not dirty.
        } {
            // We're now out of the lock, and can let the storage take as long as it wants to
            // store the event.
            // If another thread now records another event, then dirty will be set to true,
            // but it will have missed this save, and will be done next time. That's ok.
            storage.save(event_id, data)
        } else {
            storage.delete(event_id)
        }
        .map_err(|_e| {
            // If storage errors, then we should re-mark the event as dirty.
            // Dirty may already be set to true (if another thread has recorded an event),
            // but we won't lose that just because we serialized.
            // We'll just try again later.
            let mut counter = counter.lock().unwrap();
            counter.mark_dirty();
            _e
        })
    }
}

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

/// Drop implementation for EventStore.
///
/// **IMPORTANT**: This implementation performs **synchronous I/O** which can block the
/// current thread. See the struct-level documentation for [`EventStore`] for best practices.
///
/// When an EventStore is dropped:
/// 1. The background auto-persist task is aborted (if configured)
/// 2. A final `persist()` is attempted if the store has dirty (unsaved) events
///
/// # Behavior Details
///
/// - **Blocking I/O**: The persist operation blocks until complete. In async contexts,
///   this blocks the executor thread.
/// - **Silent errors**: Any errors during persist are ignored (logged to stderr in debug
///   builds). Errors cannot be returned from Drop.
/// - **Panic safety**: Drop runs during panic unwinding, so errors are swallowed to avoid
///   double-panics.
///
/// # Recommendations
///
/// Call [`persist()`](Self::persist) explicitly before dropping to:
/// - Avoid blocking in async code
/// - Handle errors properly
/// - Make cleanup behavior explicit
impl Drop for EventStore {
    fn drop(&mut self) {
        // Abort the background auto-persist task if present.
        // This ensures the task doesn't outlive the EventStore and prevents
        // potential use-after-free of the inner Arc<EventStoreInner>.
        #[cfg(feature = "tokio")]
        if let Some(handle) = &self.auto_persist_handle {
            handle.abort();
        }

        // Perform a final synchronous persist if there are unsaved changes.
        // This is a best-effort attempt - errors are silently ignored because:
        // 1. Drop cannot return an error
        // 2. Drop may be called during panic unwinding (can't panic in Drop)
        // 3. For critical data, users should call persist() explicitly before drop
        if self.is_dirty() {
            let _ = self.persist();
            // Errors are intentionally ignored. In production code, call persist()
            // explicitly before dropping the store to handle errors properly.
        }
    }
}

/// A generic event id trait for type-safe event identification.
///
/// This trait enables using custom types (especially enums) as event identifiers,
/// providing compile-time guarantees about which events exist in your system.
///
/// # Benefits of Type-Safe Event IDs
///
/// Using enums instead of raw strings provides several advantages:
///
/// 1. **Bounded event set**: Compiler enforces a fixed set of events, preventing typos
/// 2. **Memory safety**: Enum variants guarantee a bounded number of event counters
/// 3. **Refactoring safety**: Renaming events shows all usage sites at compile time
/// 4. **Documentation**: Enum definition serves as single source of truth for all events
///
/// # Memory Implications
///
/// Each unique event ID creates a new counter in memory (~2KB per event with default config).
/// Using patterns like `format!("user:{}:event", user_id)` creates unbounded event IDs
/// that grow with your user base, defeating the fixed-memory guarantee.
///
/// **Safe pattern** (bounded events):
/// ```rust
/// use tiny_counter::{EventStore, EventId};
///
/// #[derive(Debug)]
/// enum AppEvent {
///     UserLogin,
///     UserLogout,
///     ApiCall,
/// }
///
/// impl AsRef<str> for AppEvent {
///     fn as_ref(&self) -> &str {
///         match self {
///             AppEvent::UserLogin => "user_login",
///             AppEvent::UserLogout => "user_logout",
///             AppEvent::ApiCall => "api_call",
///         }
///     }
/// }
///
/// impl EventId for AppEvent {}
///
/// let store = EventStore::new();
/// store.record(AppEvent::UserLogin);  // Type-safe, bounded memory
/// ```
///
/// **Unsafe pattern** (unbounded events, avoid this):
/// ```rust,no_run
/// # use tiny_counter::EventStore;
/// let store = EventStore::new();
/// for user_id in 0..1_000_000 {
///     // WARNING: Creates 1M separate counters, ~2GB memory!
///     store.record(format!("user:{}:login", user_id));
/// }
/// ```
///
/// For per-user tracking, use a single event ID and separate EventStore instances per user,
/// or aggregate at the application level rather than in the event store.
pub trait EventId: AsRef<str> {}

impl EventId for str {}
impl EventId for String {}
impl EventId for &str {}
impl EventId for &String {}

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

    use chrono::TimeZone;

    #[test]
    fn test_new_creates_empty_store_with_default_intervals() {
        let store = EventStore::new();
        assert!(!store.is_dirty());

        // Verify default intervals work by querying each time unit
        let intervals = store.tracked_intervals();
        assert_eq!(intervals.len(), 6);
    }

    #[test]
    fn test_default_configs_have_256_buckets_total() {
        let config = EventCounterConfig::default();
        let total: usize = config.as_vec().iter().map(|c| c.bucket_count()).sum();
        assert_eq!(total, 256);
    }

    #[test]
    fn test_record_creates_counter_on_demand() {
        let store = EventStore::new();
        store.record("test_event");

        // Verify counter was created by querying the event
        let sum = store.query("test_event").last_days(1).sum();
        assert_eq!(sum, Some(1));
    }

    #[test]
    fn test_record_count_with_count_5() {
        let store = EventStore::new();
        store.record_count("test_event", 5);

        let sum = store.query("test_event").last_days(1).sum();
        assert_eq!(sum, Some(5));
    }

    #[test]
    fn test_record_at_with_past_time() {
        let store = EventStore::new();
        let now = Utc::now();
        let past = now - Duration::days(2);

        store.record_at("test_event", past).unwrap();

        // Verify event was recorded
        assert!(store.is_dirty());
        let sum = store.query("test_event").last_days(7).sum();
        assert_eq!(sum, Some(1));
    }

    #[test]
    fn test_record_ago_with_duration() {
        let store = EventStore::new();
        store.record_ago("test_event", Duration::hours(3));

        // Verify event was recorded
        assert!(store.is_dirty());
        let sum = store.query("test_event").last_hours(24).sum();
        assert_eq!(sum, Some(1));
    }

    #[test]
    fn test_query_returns_query_builder() {
        let store = EventStore::new();
        store.record("test_event");

        let query = store.query("test_event");
        let sum = query.last_days(1).sum();
        assert_eq!(sum, Some(1));
    }

    #[test]
    fn test_query_nonexistent_event_returns_none() {
        let store = EventStore::new();
        let query = store.query("nonexistent");
        let sum = query.last_days(1).sum();
        assert_eq!(sum, None);
    }

    #[test]
    fn test_query_many() {
        let store = EventStore::new();
        store.record_count("event1", 5);
        store.record_count("event2", 3);

        let event_ids = &["event1", "event2"];
        let query = store.query_many(event_ids);
        let sum = query.last_days(1).sum();
        assert_eq!(sum, Some(8));
    }

    #[test]
    fn test_query_ratio() {
        let store = EventStore::new();
        store.record_count("numerator", 10);
        store.record_count("denominator", 5);

        let ratio = store.query_ratio("numerator", "denominator").last_days(1);
        assert_eq!(ratio, Some(2.0));
    }

    #[test]
    fn test_query_delta() {
        let store = EventStore::new();
        store.record_count("positive", 10);
        store.record_count("negative", 3);

        let delta = store.query_delta("positive", "negative").last_days(1).sum();
        assert_eq!(delta, 7);
    }

    #[test]
    fn test_dirty_tracking_starts_clean() {
        let store = EventStore::new();
        assert!(!store.is_dirty());
    }

    #[test]
    fn test_dirty_tracking_becomes_dirty_after_record() {
        let store = EventStore::new();
        store.record("test_event");
        assert!(store.is_dirty());
    }

    #[test]
    fn test_query_returns_none_for_nonexistent_event() {
        let store = EventStore::new();
        let result = store.query("nonexistent").last_days(1).sum();
        assert_eq!(result, None);
    }

    #[test]
    fn test_integration_record_multiple_events_query_each() {
        let store = EventStore::new();

        // Record different events
        store.record_count("login", 5);
        store.record_count("logout", 3);
        store.record_count("error", 1);

        // Query each
        let login_sum = store.query("login").last_days(1).sum();
        let logout_sum = store.query("logout").last_days(1).sum();
        let error_sum = store.query("error").last_days(1).sum();

        assert_eq!(login_sum, Some(5));
        assert_eq!(logout_sum, Some(3));
        assert_eq!(error_sum, Some(1));
    }

    #[test]
    fn test_record_creates_counter_with_default_config() {
        let store = EventStore::new();
        store.record("test_event");

        // Verify counter has all 4 default intervals by querying each
        assert!(store.query("test_event").last_minutes(1).sum().is_some());
        assert!(store.query("test_event").last_hours(1).sum().is_some());
        assert!(store.query("test_event").last_days(1).sum().is_some());
        assert!(store.query("test_event").last_months(1).sum().is_some());
    }

    #[test]
    fn test_default_trait() {
        let store = EventStore::default();
        assert!(!store.is_dirty());

        // Verify default intervals are configured
        let intervals = store.tracked_intervals();
        assert_eq!(intervals.len(), 6);
    }

    #[test]
    fn test_default_config_includes_all_six_time_units() {
        let store = EventStore::new();
        store.record("test_event");

        // Verify that all 6 time units are in the default configuration
        let intervals = store.tracked_intervals();
        let time_units: Vec<TimeUnit> = intervals.iter().map(|(unit, _)| *unit).collect();

        assert!(time_units.contains(&TimeUnit::Minutes));
        assert!(time_units.contains(&TimeUnit::Hours));
        assert!(time_units.contains(&TimeUnit::Days));
        assert!(time_units.contains(&TimeUnit::Weeks));
        assert!(time_units.contains(&TimeUnit::Months));
        assert!(time_units.contains(&TimeUnit::Years));

        // Verify that all 6 default time units work
        assert!(store.query("test_event").last_minutes(1).sum().is_some());
        assert!(store.query("test_event").last_hours(1).sum().is_some());
        assert!(store.query("test_event").last_days(1).sum().is_some());
        assert!(store.query("test_event").last_weeks(1).sum().is_some());
        assert!(store.query("test_event").last_months(1).sum().is_some());
        assert!(store.query("test_event").last_years(1).sum().is_some());
    }

    #[test]
    fn test_record_at_with_time_before_creation() {
        let store = EventStore::new();
        let now = Utc.with_ymd_and_hms(2025, 1, 10, 12, 0, 0).unwrap();

        // Mock time by using TestClock would be better, but for this test
        // we'll just verify it doesn't panic
        let past = now - Duration::days(2);
        let result = store.record_at("test_event", past);

        // Should succeed (not a future event)
        assert!(result.is_ok());
    }

    #[test]
    fn test_multiple_records_to_same_event() {
        let store = EventStore::new();
        store.record("test_event");
        store.record("test_event");
        store.record("test_event");

        let sum = store.query("test_event").last_days(1).sum();
        assert_eq!(sum, Some(3));
    }

    #[test]
    fn test_record_count_ago() {
        let store = EventStore::new();
        store.record_count_ago("test_event", 5, Duration::hours(2));

        // Should have recorded in the past
        let sum = store.query("test_event").last_hours(24).sum();
        assert_eq!(sum, Some(5));
    }

    #[test]
    fn test_record_ago_outside_tracking_window_silently_drops() {
        let store = EventStore::new();

        // Default store tracks up to 4 years in the past
        // Record an event 10 years ago - should be silently dropped
        store.record_ago("ancient_event", Duration::days(365 * 10));

        // Query returns Some(0) because event counter exists but event was dropped
        let sum = store.query("ancient_event").ever().sum();
        assert_eq!(sum, Some(0));
    }

    #[test]
    fn test_record_count_ago_outside_tracking_window_silently_drops() {
        let store = EventStore::new();

        // Default store tracks up to 4 years in the past
        // Record events 10 years ago - should be silently dropped
        store.record_count_ago("ancient_event", 100, Duration::days(365 * 10));

        // Query returns Some(0) because event counter exists but events were dropped
        let sum = store.query("ancient_event").ever().sum();
        assert_eq!(sum, Some(0));
    }

    #[test]
    fn test_balance_delta_positive() {
        let store = EventStore::new();
        store.record_count("credits", 10);
        store.record_count("debits", 3);

        // Delta = 10 - 3 = 7 (positive), so record 7 to debits
        store.balance_delta("credits", "debits").unwrap();

        let credits_sum = store.query("credits").ever().sum();
        let debits_sum = store.query("debits").ever().sum();

        assert_eq!(credits_sum, Some(10));
        assert_eq!(debits_sum, Some(10)); // 3 + 7 = 10
    }

    #[test]
    fn test_balance_delta_negative() {
        let store = EventStore::new();
        store.record_count("credits", 3);
        store.record_count("debits", 10);

        // Delta = 3 - 10 = -7 (negative), so record 7 to credits
        store.balance_delta("credits", "debits").unwrap();

        let credits_sum = store.query("credits").ever().sum();
        let debits_sum = store.query("debits").ever().sum();

        assert_eq!(credits_sum, Some(10)); // 3 + 7 = 10
        assert_eq!(debits_sum, Some(10));
    }

    #[test]
    fn test_balance_delta_zero() {
        let store = EventStore::new();
        store.record_count("credits", 10);
        store.record_count("debits", 10);

        // Delta = 10 - 10 = 0, so no-op
        store.balance_delta("credits", "debits").unwrap();

        let credits_sum = store.query("credits").ever().sum();
        let debits_sum = store.query("debits").ever().sum();

        assert_eq!(credits_sum, Some(10));
        assert_eq!(debits_sum, Some(10));
    }

    #[test]
    fn test_reset_dirty() {
        let store = EventStore::new();
        store.record("test_event");
        assert!(store.is_dirty());

        store.reset_dirty();
        assert!(!store.is_dirty());
    }

    #[test]
    fn test_memory_usage_empty_store() {
        let store = EventStore::new();
        assert_eq!(store.memory_usage(), 0);
    }

    #[test]
    fn test_memory_usage_with_events() {
        let store = EventStore::new();
        store.record("test_event");

        // Memory usage should be greater than 0
        let usage = store.memory_usage();
        assert!(usage > 0);
    }

    #[test]
    fn test_tracked_intervals_returns_default_config() {
        let store = EventStore::new();
        let intervals = store.tracked_intervals();

        assert_eq!(intervals.len(), 6);
        assert!(intervals.contains(&(TimeUnit::Minutes, 60)));
        assert!(intervals.contains(&(TimeUnit::Hours, 72)));
        assert!(intervals.contains(&(TimeUnit::Days, 56)));
        assert!(intervals.contains(&(TimeUnit::Weeks, 52)));
        assert!(intervals.contains(&(TimeUnit::Months, 12)));
        assert!(intervals.contains(&(TimeUnit::Years, 4)));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_without_storage_returns_error() {
        let store = EventStore::new();
        store.record("test_event");

        let result = store.persist();
        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Storage(_) => (),
            _ => panic!("Expected Storage error"),
        }
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_all_without_storage_returns_error() {
        let store = EventStore::new();
        store.record("test_event");

        let result = store.persist_all();
        assert!(result.is_err());
        match result.unwrap_err() {
            Error::Storage(_) => (),
            _ => panic!("Expected Storage error"),
        }
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_with_storage() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let store = EventStoreBuilder::new()
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        store.record("event1");
        store.record("event2");
        assert!(store.is_dirty());

        let result = store.persist();
        assert!(result.is_ok());
        assert!(!store.is_dirty());
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_close_persists_and_consumes() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let storage = MemoryStorage::new();
        let store = EventStoreBuilder::new()
            .with_storage(storage)
            .build()
            .unwrap();

        store.record("event1");
        store.record("event2");
        assert!(store.is_dirty());

        // close() should persist and consume the store
        let result = store.close();
        assert!(result.is_ok());
        // store is now consumed and cannot be used
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_close_returns_error_on_persist_failure() {
        use crate::EventStoreBuilder;

        // Create store without storage - persist will fail
        let store = EventStoreBuilder::new().build().unwrap();

        store.record("event1");
        assert!(store.is_dirty());

        // close() should return error when persist fails
        let result = store.close();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No storage configured"));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_only_dirty_events() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let store = EventStoreBuilder::new()
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        // Record and persist event1
        store.record("event1");
        store.persist().unwrap();
        assert!(!store.is_dirty());

        // Record event2
        store.record("event2");
        assert!(store.is_dirty());

        // Persist should only write event2
        store.persist().unwrap();
        assert!(!store.is_dirty());
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_all() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let store = EventStoreBuilder::new()
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        store.record("event1");
        store.record("event2");
        store.record("event3");

        let result = store.persist_all();
        assert!(result.is_ok());
        assert!(!store.is_dirty());
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_serialization_roundtrip() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let store = EventStoreBuilder::new()
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        store.record_count("test_event", 42);
        store.persist().unwrap();

        // Verify the counter has the right value
        let sum = store.query("test_event").last_days(1).sum();
        assert_eq!(sum, Some(42));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_clears_dirty_events() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let store = EventStoreBuilder::new()
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        store.record("event1");
        store.record("event2");
        assert!(store.is_dirty());

        store.persist().unwrap();
        assert!(!store.is_dirty());
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_compact_advances_and_saves() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let mut store = EventStoreBuilder::new()
            .track_days(7)
            .with_storage(MemoryStorage::new())
            .build()
            .unwrap();

        // Record events
        store.record("event1");
        store.record("event2");

        // Persist both
        store.persist_all().unwrap();
        assert!(!store.is_dirty());

        // Compact should re-save all events after advancing
        store.compact().unwrap();

        // Events should still be queryable
        assert_eq!(store.query("event1").last_days(7).sum(), Some(1));
        assert_eq!(store.query("event2").last_days(7).sum(), Some(1));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_compact_while_recording() {
        use crate::storage::MemoryStorage;
        use crate::{EventCounterConfig, SystemClock};
        use std::thread;

        // Create store with from_parts to avoid Clone issue
        let base_store = EventStore::from_parts(
            SystemClock::new(),
            Some(Box::new(MemoryStorage::new())),
            Some(Arc::new(crate::formatter::BincodeFormat)),
            EventCounterConfig::default(),
        );

        // Wrap in Arc+Mutex since compact needs &mut self
        let store = Arc::new(Mutex::new(base_store));
        let store_writer = Arc::clone(&store);
        let store_compactor = Arc::clone(&store);

        // Writer thread - continuously record
        let writer = thread::spawn(move || {
            for _ in 0..100 {
                let store = store_writer.lock().unwrap();
                store.record("compact_event");
                drop(store);
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Compactor thread - compact multiple times
        let compactor = thread::spawn(move || {
            for _ in 0..10 {
                thread::sleep(std::time::Duration::from_micros(50));
                let mut store = store_compactor.lock().unwrap();
                let _ = store.compact();
            }
        });

        writer.join().unwrap();
        compactor.join().unwrap();

        // Verify all 100 events were recorded (no data loss)
        let store = store.lock().unwrap();
        let sum = store.query("compact_event").last_days(7).sum();
        assert_eq!(sum, Some(100));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_compact_without_storage_returns_error() {
        let mut store = EventStore::new();
        store.record("event1");

        let result = store.compact();
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("No storage configured"));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_memory_usage_calculation() {
        let store = EventStore::new();
        store.record("event1");
        store.record("event2");

        let usage = store.memory_usage();
        // Each event has 6 intervals (default config)
        // Each interval has at least 1 bucket (8 bytes)
        // So minimum is 2 events * 6 intervals * 1 bucket * 8 bytes = 96 bytes
        assert!(usage >= 96);
    }

    #[test]
    fn test_export_all_returns_all_counters() {
        let store = EventStore::new();
        store.record("event1");
        store.record("event2");
        store.record("event3");

        let exported = store.export_all().unwrap();
        assert_eq!(exported.len(), 3);
        assert!(exported.contains_key("event1"));
        assert!(exported.contains_key("event2"));
        assert!(exported.contains_key("event3"));
    }

    #[test]
    fn test_export_dirty_returns_only_dirty_counters() {
        let store = EventStore::new();
        store.record("event1");
        store.record("event2");
        store.reset_dirty();

        // Now record event3 (should be dirty)
        store.record("event3");

        let exported = store.export_dirty().unwrap();
        assert_eq!(exported.len(), 1);
        assert!(exported.contains_key("event3"));
        assert!(!exported.contains_key("event1"));
        assert!(!exported.contains_key("event2"));
    }

    #[test]
    fn test_export_all_returns_empty_map_for_empty_store() {
        let store = EventStore::new();
        let exported = store.export_all().unwrap();
        assert_eq!(exported.len(), 0);
    }

    #[test]
    fn test_export_dirty_returns_empty_map_when_nothing_dirty() {
        let store = EventStore::new();
        store.record("event1");
        store.reset_dirty();

        let exported = store.export_dirty().unwrap();
        assert_eq!(exported.len(), 0);
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_export_all_includes_disk_only_counters() {
        use crate::storage::MemoryStorage;
        use crate::EventStoreBuilder;

        let storage = MemoryStorage::new();
        let store = EventStoreBuilder::new()
            .with_storage(storage)
            .build()
            .unwrap();

        // Record and persist event1
        store.record_count("event1", 10);
        store.persist().unwrap();

        // Record event2 (stays in memory)
        store.record_count("event2", 20);

        // Clear memory by dropping store and creating new one with same storage
        let storage2 = {
            let storage_arc = store.inner.storage.as_ref().unwrap().clone();
            let storage_guard = storage_arc.lock().unwrap();
            // Create a new MemoryStorage and copy data from old one
            let mut new_storage = MemoryStorage::new();
            for key in storage_guard.list_keys().unwrap() {
                let data = storage_guard.load(&key).unwrap().unwrap();
                new_storage.save(&key, data).unwrap();
            }
            new_storage
        };

        let store2 = EventStoreBuilder::new()
            .with_storage(storage2)
            .build()
            .unwrap();

        // Record event3 (only in memory)
        store2.record_count("event3", 30);

        // export_all should include:
        // - event1 (from storage, not in memory)
        // - event3 (from memory, not in storage)
        let exported = store2.export_all().unwrap();
        assert_eq!(exported.len(), 2);
        assert!(exported.contains_key("event1"));
        assert!(exported.contains_key("event3"));

        // Verify the counts are correct
        assert_eq!(store2.query("event1").last_days(1).sum(), Some(10));
        assert_eq!(store2.query("event3").last_days(1).sum(), Some(30));
    }

    #[test]
    fn test_import_event_creates_new_counter() {
        let store1 = EventStore::new();
        store1.record_count("event1", 42);

        let exported = store1.export_all().unwrap();
        let counter = exported.get("event1").unwrap().clone();

        let store2 = EventStore::new();
        store2.import_event("event1", counter).unwrap();

        let sum = store2.query("event1").last_days(1).sum();
        assert_eq!(sum, Some(42));
    }

    #[test]
    fn test_import_event_overwrites_existing() {
        let store = EventStore::new();
        store.record_count("event1", 10);

        let store2 = EventStore::new();
        store2.record_count("event1", 42);

        let exported = store2.export_all().unwrap();
        let counter = exported.get("event1").unwrap().clone();

        store.import_event("event1", counter).unwrap();

        let sum = store.query("event1").last_days(1).sum();
        // Should have overwritten 10 with 42
        assert_eq!(sum, Some(42));
    }

    #[test]
    fn test_import_all_batch_imports() {
        let store1 = EventStore::new();
        store1.record_count("event1", 10);
        store1.record_count("event2", 20);
        store1.record_count("event3", 30);

        let exported = store1.export_all().unwrap();

        let store2 = EventStore::new();
        store2.import_all(exported).unwrap();

        assert_eq!(store2.query("event1").last_days(1).sum(), Some(10));
        assert_eq!(store2.query("event2").last_days(1).sum(), Some(20));
        assert_eq!(store2.query("event3").last_days(1).sum(), Some(30));
    }

    #[test]
    fn test_merge_event_combines_counts() {
        let store1 = EventStore::new();
        store1.record_count("event1", 10);

        let store2 = EventStore::new();
        store2.record_count("event1", 20);

        let exported = store2.export_all().unwrap();
        let counter = exported.get("event1").unwrap().clone();

        store1.merge_event("event1", counter).unwrap();

        let sum = store1.query("event1").last_days(1).sum();
        // Should have 10 + 20 = 30
        assert_eq!(sum, Some(30));
    }

    #[test]
    fn test_merge_event_creates_counter_if_not_exists() {
        let store1 = EventStore::new();

        let store2 = EventStore::new();
        store2.record_count("event1", 42);

        let exported = store2.export_all().unwrap();
        let counter = exported.get("event1").unwrap().clone();

        store1.merge_event("event1", counter).unwrap();

        let sum = store1.query("event1").last_days(1).sum();
        assert_eq!(sum, Some(42));
    }

    #[test]
    fn test_merge_all_combines_multiple_events() {
        let store1 = EventStore::new();
        store1.record_count("event1", 10);
        store1.record_count("event2", 20);

        let store2 = EventStore::new();
        store2.record_count("event1", 5);
        store2.record_count("event3", 30);

        let exported = store2.export_all().unwrap();
        store1.merge_all(exported).unwrap();

        assert_eq!(store1.query("event1").last_days(1).sum(), Some(15)); // 10 + 5
        assert_eq!(store1.query("event2").last_days(1).sum(), Some(20)); // unchanged
        assert_eq!(store1.query("event3").last_days(1).sum(), Some(30));
        // new
    }

    #[test]
    fn test_merge_is_commutative_at_store_level() {
        let store_a1 = EventStore::new();
        store_a1.record_count("event1", 10);

        let store_b1 = EventStore::new();
        store_b1.record_count("event1", 20);

        let store_a2 = EventStore::new();
        store_a2.record_count("event1", 10);

        let store_b2 = EventStore::new();
        store_b2.record_count("event1", 20);

        // a + b
        let b1_export = store_b1.export_all().unwrap();
        store_a1.merge_all(b1_export).unwrap();

        // b + a
        let a2_export = store_a2.export_all().unwrap();
        store_b2.merge_all(a2_export).unwrap();

        // Both should equal 30
        assert_eq!(store_a1.query("event1").last_days(1).sum(), Some(30));
        assert_eq!(store_b2.query("event1").last_days(1).sum(), Some(30));
    }

    #[test]
    fn test_merge_is_associative_at_store_level() {
        let store_a1 = EventStore::new();
        store_a1.record_count("event1", 10);

        let store_b1 = EventStore::new();
        store_b1.record_count("event1", 20);

        let store_c1 = EventStore::new();
        store_c1.record_count("event1", 30);

        let store_a2 = EventStore::new();
        store_a2.record_count("event1", 10);

        let store_b2 = EventStore::new();
        store_b2.record_count("event1", 20);

        let store_c2 = EventStore::new();
        store_c2.record_count("event1", 30);

        // (a + b) + c
        let b1_export = store_b1.export_all().unwrap();
        store_a1.merge_all(b1_export).unwrap();
        let c1_export = store_c1.export_all().unwrap();
        store_a1.merge_all(c1_export).unwrap();

        // a + (b + c)
        let c2_export = store_c2.export_all().unwrap();
        store_b2.merge_all(c2_export).unwrap();
        let b2_export = store_b2.export_all().unwrap();
        store_a2.merge_all(b2_export).unwrap();

        // Both should equal 60
        assert_eq!(store_a1.query("event1").last_days(1).sum(), Some(60));
        assert_eq!(store_a2.query("event1").last_days(1).sum(), Some(60));
    }

    // Test for ev-dcq: BaseEventStore with auto_persist_handle field
    #[cfg(all(feature = "tokio", feature = "serde"))]
    #[tokio::test]
    async fn test_base_event_store_has_auto_persist_handle_field() {
        use crate::storage::MemoryStorage;
        use std::time::Duration;

        // Create store with storage
        let mut store = EventStore::from_parts(
            SystemClock::new(),
            Some(Box::new(MemoryStorage::new())),
            #[cfg(feature = "serde-bincode")]
            Some(Arc::new(crate::formatter::BincodeFormat)),
            #[cfg(all(feature = "serde-json", not(feature = "serde-bincode")))]
            Some(Arc::new(crate::formatter::JsonFormat)),
            EventCounterConfig::default(),
        );

        // Verify field exists and is None initially
        assert!(store.auto_persist_handle.is_none());

        // Simulate setting the handle (this will fail until we add the field)
        let handle = tokio::spawn(async {
            tokio::time::sleep(Duration::from_millis(10)).await;
        });
        store.auto_persist_handle = Some(handle);
        assert!(store.auto_persist_handle.is_some());
    }

    // Test for ev-dcq: spawn_auto_persist creates background task
    #[cfg(all(feature = "tokio", feature = "serde"))]
    #[tokio::test]
    async fn test_spawn_auto_persist_creates_background_task() {
        use crate::storage::MemoryStorage;
        use chrono::Duration;

        // Create store with storage
        let store = EventStore::from_parts(
            SystemClock::new(),
            Some(Box::new(MemoryStorage::new())),
            #[cfg(feature = "serde-bincode")]
            Some(Arc::new(crate::formatter::BincodeFormat)),
            #[cfg(all(feature = "serde-json", not(feature = "serde-bincode")))]
            Some(Arc::new(crate::formatter::JsonFormat)),
            EventCounterConfig::default(),
        );

        // Record an event to make it dirty
        store.record("test");
        assert!(store.is_dirty());

        // Spawn auto-persist task
        let handle =
            EventStore::spawn_auto_persist(store.inner.clone(), Duration::milliseconds(50));

        // Wait for auto-persist to run
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Store should no longer be dirty
        assert!(!store.is_dirty());

        // Clean up
        handle.abort();
    }

    // Test for ev-dcq: Drop implementation aborts handle and does final persist
    #[cfg(all(feature = "tokio", feature = "serde"))]
    #[tokio::test]
    async fn test_drop_aborts_handle_and_persists() {
        use crate::storage::MemoryStorage;
        use std::time::Duration;

        // Create store with storage
        let mut store = EventStore::from_parts(
            SystemClock::new(),
            Some(Box::new(MemoryStorage::new())),
            #[cfg(feature = "serde-bincode")]
            Some(Arc::new(crate::formatter::BincodeFormat)),
            #[cfg(all(feature = "serde-json", not(feature = "serde-bincode")))]
            Some(Arc::new(crate::formatter::JsonFormat)),
            EventCounterConfig::default(),
        );

        // Spawn a long-running task
        let handle = tokio::spawn(async {
            loop {
                tokio::time::sleep(Duration::from_secs(1)).await;
            }
        });
        store.auto_persist_handle = Some(handle);

        // Record an event
        store.record("test");
        assert!(store.is_dirty());

        // Drop the store
        drop(store);

        // Task should be aborted (we can't directly test this, but Drop runs)
        // The test passes if Drop doesn't panic
    }

    // Test for ev-rh6: Drop persists dirty data even when auto-persist is enabled
    #[cfg(all(
        feature = "tokio",
        feature = "serde",
        feature = "serde",
        feature = "storage-fs"
    ))]
    #[tokio::test]
    async fn test_drop_persists_dirty_data_with_auto_persist() {
        use crate::storage::FilePerEvent;
        use chrono::Duration;
        use tempfile::tempdir;

        let temp_dir = tempdir().unwrap();
        let storage_path = temp_dir.path().join("events");

        // Create store with auto-persist enabled (short interval)
        let store = EventStore::builder()
            .with_storage(FilePerEvent::new(&storage_path, ".dat").unwrap())
            .auto_persist(Duration::milliseconds(100))
            .build()
            .unwrap();

        // Record first event and wait for auto-persist to run
        store.record("event1");
        tokio::time::sleep(std::time::Duration::from_millis(150)).await;

        // Verify first event was persisted
        {
            let storage = FilePerEvent::new(&storage_path, ".dat").unwrap();
            let data = storage.load("event1").unwrap();
            assert!(
                data.is_some(),
                "First event should be persisted by auto-persist"
            );
        }

        // Record second event (makes store dirty again)
        store.record("event2");

        // Drop the store BEFORE next auto-persist cycle
        drop(store);

        // Load from storage and verify BOTH events are present
        let storage = FilePerEvent::new(&storage_path, ".dat").unwrap();
        let event1_data = storage.load("event1").unwrap();
        let event2_data = storage.load("event2").unwrap();

        assert!(
            event1_data.is_some(),
            "First event should still be in storage"
        );
        assert!(
            event2_data.is_some(),
            "Second event MUST be persisted on drop, not lost"
        );
    }
}

#[cfg(test)]
mod concurrency_tests {
    use std::thread;

    use super::*;

    #[test]
    fn test_concurrent_record_from_multiple_threads() {
        let store = Arc::new(EventStore::new());
        let mut handles = vec![];

        // Spawn 10 threads, each recording 100 events
        for _ in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = thread::spawn(move || {
                for _ in 0..100 {
                    store_clone.record("concurrent_event");
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }

        // Should have 1000 total events
        let sum = store.query("concurrent_event").last_days(1).sum();
        assert_eq!(sum, Some(1000));
    }

    #[test]
    fn test_query_while_recording() {
        let store = Arc::new(EventStore::new());
        let store_writer = Arc::clone(&store);
        let store_reader = Arc::clone(&store);

        // Writer thread
        let writer = thread::spawn(move || {
            for i in 0..100 {
                store_writer.record_count("test_event", i);
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Reader thread
        let reader = thread::spawn(move || {
            for _ in 0..50 {
                let _sum = store_reader.query("test_event").last_days(1).sum();
                thread::sleep(std::time::Duration::from_micros(20));
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();

        // Should have sum of 0..100 = 4950
        let final_sum = store.query("test_event").last_days(1).sum();
        assert_eq!(final_sum, Some(4950));
    }

    #[cfg(feature = "serde-bincode")]
    #[test]
    fn test_persist_while_recording() {
        use crate::storage::MemoryStorage;

        // Create base store directly to avoid enum issues with Arc+Clone
        let base_store = EventStore::from_parts(
            SystemClock::new(),
            Some(Box::new(MemoryStorage::new())),
            #[cfg(feature = "serde")]
            {
                #[cfg(feature = "serde-bincode")]
                {
                    Some(Arc::new(crate::formatter::BincodeFormat))
                }
                #[cfg(not(feature = "serde-bincode"))]
                {
                    None
                }
            },
            EventCounterConfig::default(),
        );
        let store = Arc::new(base_store);

        let store_writer = Arc::clone(&store);
        let store_persister = Arc::clone(&store);

        // Writer thread
        let writer = thread::spawn(move || {
            for _ in 0..100 {
                store_writer.record("persist_event");
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Persister thread
        let persister = thread::spawn(move || {
            for _ in 0..20 {
                let _ = store_persister.persist();
                thread::sleep(std::time::Duration::from_micros(50));
            }
        });

        writer.join().unwrap();
        persister.join().unwrap();

        // Final persist
        store.persist().unwrap();

        // Should have 100 events
        let sum = store.query("persist_event").last_days(1).sum();
        assert_eq!(sum, Some(100));
        assert!(!store.is_dirty());
    }

    #[test]
    fn test_concurrent_query_many_from_multiple_threads() {
        let store = Arc::new(EventStore::new());
        let mut handles = vec![];

        // Pre-populate events
        store.record_count("event1", 100);
        store.record_count("event2", 200);
        store.record_count("event3", 300);

        // Spawn 10 threads, each querying multiple events 50 times
        for _ in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = thread::spawn(move || {
                for _ in 0..50 {
                    let event_ids = &["event1", "event2", "event3"];
                    let sum = store_clone.query_many(event_ids).last_days(1).sum();
                    // Should always get the same sum
                    assert_eq!(sum, Some(600));
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_query_ratio_from_multiple_threads() {
        let store = Arc::new(EventStore::new());
        let mut handles = vec![];

        // Pre-populate events
        store.record_count("numerator", 100);
        store.record_count("denominator", 50);

        // Spawn 10 threads, each querying ratio 50 times
        for _ in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = thread::spawn(move || {
                for _ in 0..50 {
                    let ratio = store_clone
                        .query_ratio("numerator", "denominator")
                        .last_days(1);
                    // Should always get the same ratio
                    assert_eq!(ratio, Some(2.0));
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_query_delta_from_multiple_threads() {
        let store = Arc::new(EventStore::new());
        let mut handles = vec![];

        // Pre-populate events
        store.record_count("positive", 150);
        store.record_count("negative", 50);

        // Spawn 10 threads, each querying delta 50 times
        for _ in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = thread::spawn(move || {
                for _ in 0..50 {
                    let delta = store_clone
                        .query_delta("positive", "negative")
                        .last_days(1)
                        .sum();
                    // Should always get the same delta
                    assert_eq!(delta, 100);
                }
            });
            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_concurrent_query_many_while_recording() {
        let store = Arc::new(EventStore::new());
        let store_writer = Arc::clone(&store);
        let store_reader = Arc::clone(&store);

        // Writer thread
        let writer = thread::spawn(move || {
            for i in 0..100 {
                store_writer.record_count("event1", i);
                store_writer.record_count("event2", i * 2);
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Reader thread querying multiple events
        let reader = thread::spawn(move || {
            for _ in 0..50 {
                let event_ids = &["event1", "event2"];
                let _sum = store_reader.query_many(event_ids).last_days(1).sum();
                thread::sleep(std::time::Duration::from_micros(20));
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();

        // Verify final sums
        let event1_sum = store.query("event1").last_days(1).sum().unwrap();
        let event2_sum = store.query("event2").last_days(1).sum().unwrap();
        let multi_sum = store
            .query_many(&["event1", "event2"])
            .last_days(1)
            .sum()
            .unwrap();

        // event1: sum of 0..100 = 4950
        // event2: sum of (0..100)*2 = 9900
        assert_eq!(event1_sum, 4950);
        assert_eq!(event2_sum, 9900);
        assert_eq!(multi_sum, 14850);
    }

    #[test]
    fn test_concurrent_query_ratio_while_recording() {
        let store = Arc::new(EventStore::new());
        let store_writer = Arc::clone(&store);
        let store_reader = Arc::clone(&store);

        // Writer thread
        let writer = thread::spawn(move || {
            for i in 1..=100 {
                store_writer.record_count("numerator", i * 2);
                store_writer.record_count("denominator", i);
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Reader thread querying ratio
        let reader = thread::spawn(move || {
            for _ in 0..50 {
                let _ratio = store_reader
                    .query_ratio("numerator", "denominator")
                    .last_days(1);
                thread::sleep(std::time::Duration::from_micros(20));
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();

        // Verify final ratio
        let ratio = store.query_ratio("numerator", "denominator").last_days(1);
        // numerator: sum of 2,4,6,...,200 = 10100
        // denominator: sum of 1,2,3,...,100 = 5050
        // ratio = 10100/5050 = 2.0
        assert_eq!(ratio, Some(2.0));
    }

    #[test]
    fn test_concurrent_query_delta_while_recording() {
        let store = Arc::new(EventStore::new());
        let store_writer = Arc::clone(&store);
        let store_reader = Arc::clone(&store);

        // Writer thread
        let writer = thread::spawn(move || {
            for i in 0..100 {
                store_writer.record_count("positive", i * 2);
                store_writer.record_count("negative", i);
                thread::sleep(std::time::Duration::from_micros(10));
            }
        });

        // Reader thread querying delta
        let reader = thread::spawn(move || {
            for _ in 0..50 {
                let _delta = store_reader
                    .query_delta("positive", "negative")
                    .last_days(1)
                    .sum();
                thread::sleep(std::time::Duration::from_micros(20));
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();

        // Verify final delta
        let delta = store.query_delta("positive", "negative").last_days(1).sum();
        // positive: sum of 0,2,4,...,198 = 9900
        // negative: sum of 0,1,2,...,99 = 4950
        // delta = 9900 - 4950 = 4950
        assert_eq!(delta, 4950);
    }
}