amaters-server 0.2.1

AmateRS server binary
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
//! Health check endpoint
//!
//! Provides health status information for monitoring and orchestration systems.
//! Supports deep health probes, readiness/liveness separation, health history
//! tracking, and dependency health aggregation.

use async_trait::async_trait;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tracing::{debug, warn};

// ---------------------------------------------------------------------------
// Health status types
// ---------------------------------------------------------------------------

/// Health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
    /// Server is healthy and ready to serve requests
    Healthy,
    /// Server is starting up
    Starting,
    /// Server is shutting down
    ShuttingDown,
    /// Server has encountered an error
    Unhealthy,
    /// Server is operational but degraded
    Degraded,
}

/// Component health status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component name
    pub name: String,
    /// Component status
    pub status: HealthStatus,
    /// Optional message
    pub message: Option<String>,
    /// Last check timestamp
    pub last_check: u64,
}

// ---------------------------------------------------------------------------
// Deep health probe types
// ---------------------------------------------------------------------------

/// Result of a deep health probe
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthProbeResult {
    /// Probe status
    pub status: ProbeStatus,
    /// Latency of the probe in milliseconds
    pub latency_ms: f64,
    /// Human-readable message
    pub message: String,
}

/// Probe status (more granular than HealthStatus)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ProbeStatus {
    /// Everything is fine
    Healthy,
    /// Operational but degraded
    Degraded,
    /// Not operational
    Unhealthy,
}

impl ProbeStatus {
    /// Convert to a HealthStatus
    pub fn to_health_status(self) -> HealthStatus {
        match self {
            ProbeStatus::Healthy => HealthStatus::Healthy,
            ProbeStatus::Degraded => HealthStatus::Degraded,
            ProbeStatus::Unhealthy => HealthStatus::Unhealthy,
        }
    }

    /// Return the worse of two statuses
    pub fn worse(self, other: ProbeStatus) -> ProbeStatus {
        match (self, other) {
            (ProbeStatus::Unhealthy, _) | (_, ProbeStatus::Unhealthy) => ProbeStatus::Unhealthy,
            (ProbeStatus::Degraded, _) | (_, ProbeStatus::Degraded) => ProbeStatus::Degraded,
            _ => ProbeStatus::Healthy,
        }
    }
}

/// Trait for deep health check probes
#[async_trait]
pub trait DeepHealthCheck: Send + Sync {
    /// Execute the health check and return the result
    async fn check(&self) -> HealthProbeResult;
}

// ---------------------------------------------------------------------------
// Built-in probes
// ---------------------------------------------------------------------------

/// Storage probe — verifies storage is readable/writable by writing a test
/// key, reading it back, and deleting it.
pub struct StorageProbe {
    /// Path to storage directory for the probe
    storage_path: std::path::PathBuf,
}

impl StorageProbe {
    /// Create a new storage probe targeting the given directory
    pub fn new(storage_path: std::path::PathBuf) -> Self {
        Self { storage_path }
    }
}

#[async_trait]
impl DeepHealthCheck for StorageProbe {
    async fn check(&self) -> HealthProbeResult {
        let start = Instant::now();
        let test_file = self.storage_path.join(".health_probe_test");

        // Write
        let write_result = tokio::fs::write(&test_file, b"health_probe").await;
        if let Err(e) = write_result {
            return HealthProbeResult {
                status: ProbeStatus::Unhealthy,
                latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                message: format!("storage write failed: {e}"),
            };
        }

        // Read back
        let read_result = tokio::fs::read(&test_file).await;
        match read_result {
            Ok(data) if data == b"health_probe" => {}
            Ok(_) => {
                let _ = tokio::fs::remove_file(&test_file).await;
                return HealthProbeResult {
                    status: ProbeStatus::Unhealthy,
                    latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                    message: "storage read returned unexpected data".to_string(),
                };
            }
            Err(e) => {
                let _ = tokio::fs::remove_file(&test_file).await;
                return HealthProbeResult {
                    status: ProbeStatus::Unhealthy,
                    latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                    message: format!("storage read failed: {e}"),
                };
            }
        }

        // Delete
        if let Err(e) = tokio::fs::remove_file(&test_file).await {
            return HealthProbeResult {
                status: ProbeStatus::Degraded,
                latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                message: format!("storage cleanup failed (non-critical): {e}"),
            };
        }

        HealthProbeResult {
            status: ProbeStatus::Healthy,
            latency_ms: start.elapsed().as_secs_f64() * 1000.0,
            message: "storage read/write/delete OK".to_string(),
        }
    }
}

/// WAL probe — verifies the write-ahead log directory is appendable.
pub struct WalProbe {
    /// Path to the WAL directory
    wal_path: std::path::PathBuf,
}

impl WalProbe {
    /// Create a new WAL probe targeting the given directory
    pub fn new(wal_path: std::path::PathBuf) -> Self {
        Self { wal_path }
    }
}

#[async_trait]
impl DeepHealthCheck for WalProbe {
    async fn check(&self) -> HealthProbeResult {
        let start = Instant::now();
        let test_file = self.wal_path.join(".wal_health_probe");

        // Try to append
        let result = tokio::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .truncate(false)
            .open(&test_file)
            .await;

        match result {
            Ok(_file) => {
                let _ = tokio::fs::remove_file(&test_file).await;
                HealthProbeResult {
                    status: ProbeStatus::Healthy,
                    latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                    message: "WAL directory is appendable".to_string(),
                }
            }
            Err(e) => HealthProbeResult {
                status: ProbeStatus::Unhealthy,
                latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                message: format!("WAL append test failed: {e}"),
            },
        }
    }
}

// Raw statvfs binding — avoids depending on the `libc` crate.
#[cfg(any(target_os = "macos", target_os = "linux"))]
unsafe extern "C" {
    #[link_name = "statvfs"]
    fn statvfs_raw(path: *const std::ffi::c_char, buf: *mut u8) -> std::ffi::c_int;
}

/// Disk space probe — checks available disk space against a threshold.
pub struct DiskSpaceProbe {
    /// Path to check disk space for
    path: std::path::PathBuf,
    /// Minimum required free bytes
    min_free_bytes: u64,
}

impl DiskSpaceProbe {
    /// Create a new disk space probe.
    ///
    /// `min_free_bytes` is the threshold below which the probe reports degraded
    /// (at half the threshold) or unhealthy (at zero or below threshold / 4).
    pub fn new(path: std::path::PathBuf, min_free_bytes: u64) -> Self {
        Self {
            path,
            min_free_bytes,
        }
    }

    /// Get available space on the filesystem containing `path`.
    ///
    /// Uses platform-specific raw syscalls without depending on the `libc`
    /// crate, keeping the build pure Rust.
    fn available_space(&self) -> Result<u64, String> {
        self.available_space_impl()
    }

    #[cfg(target_os = "macos")]
    fn available_space_impl(&self) -> Result<u64, String> {
        use std::ffi::CString;
        use std::os::unix::ffi::OsStrExt;

        let c_path = CString::new(self.path.as_os_str().as_bytes())
            .map_err(|e| format!("invalid path: {e}"))?;

        // macOS statvfs layout (LP64). We only need f_frsize and f_bavail.
        // struct statvfs is 64 bytes on macOS (all u64 fields after the
        // initial f_bsize). We allocate a generous buffer.
        #[repr(C)]
        struct Statvfs {
            f_bsize: u64,
            f_frsize: u64,
            f_blocks: u64,
            f_bfree: u64,
            f_bavail: u64,
            // remaining fields not needed
            _pad: [u64; 11],
        }

        let mut buf: Statvfs = unsafe { std::mem::zeroed() };
        // macOS syscall: statvfs(2)
        let ret = unsafe { statvfs_raw(c_path.as_ptr(), &mut buf as *mut Statvfs as *mut u8) };
        if ret != 0 {
            return Err(format!(
                "statvfs failed: {}",
                std::io::Error::last_os_error()
            ));
        }

        let available = buf.f_bavail.saturating_mul(buf.f_frsize);
        Ok(available)
    }

    #[cfg(target_os = "linux")]
    fn available_space_impl(&self) -> Result<u64, String> {
        use std::ffi::CString;
        use std::os::unix::ffi::OsStrExt;

        let c_path = CString::new(self.path.as_os_str().as_bytes())
            .map_err(|e| format!("invalid path: {e}"))?;

        #[repr(C)]
        struct Statvfs {
            f_bsize: u64,
            f_frsize: u64,
            f_blocks: u64,
            f_bfree: u64,
            f_bavail: u64,
            _pad: [u64; 11],
        }

        let mut buf: Statvfs = unsafe { std::mem::zeroed() };
        let ret = unsafe { statvfs_raw(c_path.as_ptr(), &mut buf as *mut Statvfs as *mut u8) };
        if ret != 0 {
            return Err(format!(
                "statvfs failed: {}",
                std::io::Error::last_os_error()
            ));
        }

        let available = buf.f_bavail.saturating_mul(buf.f_frsize);
        Ok(available)
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    fn available_space_impl(&self) -> Result<u64, String> {
        // On unsupported platforms, just verify the directory exists.
        if self.path.exists() {
            Ok(u64::MAX)
        } else {
            Err("path does not exist".to_string())
        }
    }
}

#[async_trait]
impl DeepHealthCheck for DiskSpaceProbe {
    async fn check(&self) -> HealthProbeResult {
        let start = Instant::now();

        match self.available_space() {
            Ok(available) => {
                let latency_ms = start.elapsed().as_secs_f64() * 1000.0;
                if available >= self.min_free_bytes {
                    HealthProbeResult {
                        status: ProbeStatus::Healthy,
                        latency_ms,
                        message: format!(
                            "disk space OK: {} bytes available (threshold: {})",
                            available, self.min_free_bytes
                        ),
                    }
                } else if available >= self.min_free_bytes / 4 {
                    HealthProbeResult {
                        status: ProbeStatus::Degraded,
                        latency_ms,
                        message: format!(
                            "disk space low: {} bytes available (threshold: {})",
                            available, self.min_free_bytes
                        ),
                    }
                } else {
                    HealthProbeResult {
                        status: ProbeStatus::Unhealthy,
                        latency_ms,
                        message: format!(
                            "disk space critically low: {} bytes available (threshold: {})",
                            available, self.min_free_bytes
                        ),
                    }
                }
            }
            Err(e) => HealthProbeResult {
                status: ProbeStatus::Unhealthy,
                latency_ms: start.elapsed().as_secs_f64() * 1000.0,
                message: format!("disk space check failed: {e}"),
            },
        }
    }
}

// ---------------------------------------------------------------------------
// Health history
// ---------------------------------------------------------------------------

/// A point-in-time health snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthSnapshot {
    /// Timestamp (seconds since UNIX epoch)
    pub timestamp: u64,
    /// Overall status at this point
    pub status: HealthStatus,
    /// Whether the server was alive
    pub alive: bool,
    /// Whether the server was ready
    pub ready: bool,
}

/// Ring buffer for health check history
#[derive(Debug)]
pub struct HealthHistory {
    /// Fixed-size buffer of snapshots (ring buffer)
    buffer: Vec<Option<HealthSnapshot>>,
    /// Current write position
    write_pos: usize,
    /// Number of entries written (may exceed capacity — used for stats)
    total_written: usize,
    /// Capacity
    capacity: usize,
}

impl HealthHistory {
    /// Create a new history buffer with the given capacity
    pub fn new(capacity: usize) -> Self {
        let capacity = capacity.max(1); // at least 1
        Self {
            buffer: (0..capacity).map(|_| None).collect(),
            write_pos: 0,
            total_written: 0,
            capacity,
        }
    }

    /// Record a new snapshot
    pub fn record(&mut self, snapshot: HealthSnapshot) {
        self.buffer[self.write_pos] = Some(snapshot);
        self.write_pos = (self.write_pos + 1) % self.capacity;
        self.total_written += 1;
    }

    /// Return all recorded snapshots in chronological order
    pub fn snapshots(&self) -> Vec<HealthSnapshot> {
        let count = self.total_written.min(self.capacity);
        let mut result = Vec::with_capacity(count);

        if self.total_written < self.capacity {
            // Haven't wrapped yet — entries are 0..write_pos
            for s in self.buffer.iter().take(self.write_pos).flatten() {
                result.push(s.clone());
            }
        } else {
            // Wrapped — oldest is at write_pos, read around
            for i in 0..self.capacity {
                let idx = (self.write_pos + i) % self.capacity;
                if let Some(s) = &self.buffer[idx] {
                    result.push(s.clone());
                }
            }
        }

        result
    }

    /// Calculate uptime percentage from the history buffer.
    /// "Up" means the snapshot's `alive` field is true.
    pub fn uptime_percent(&self) -> f64 {
        let snaps = self.snapshots();
        if snaps.is_empty() {
            return 100.0;
        }
        let alive_count = snaps.iter().filter(|s| s.alive).count();
        (alive_count as f64 / snaps.len() as f64) * 100.0
    }
}

// ---------------------------------------------------------------------------
// Dependency health
// ---------------------------------------------------------------------------

/// Health information for a single dependency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DependencyHealth {
    /// Dependency name
    pub name: String,
    /// Current status
    pub status: ProbeStatus,
    /// Latency in milliseconds
    pub latency_ms: f64,
    /// Last checked timestamp (seconds since UNIX epoch)
    pub last_checked: u64,
    /// Human-readable message
    pub message: String,
}

// ---------------------------------------------------------------------------
// Liveness / readiness responses
// ---------------------------------------------------------------------------

/// Liveness check response (lightweight)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LivenessResponse {
    /// Whether the process is alive
    pub alive: bool,
    /// Current status
    pub status: HealthStatus,
    /// Uptime in seconds
    pub uptime_seconds: u64,
    /// Timestamp
    pub timestamp: u64,
}

/// Readiness check response (includes dependency detail)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReadinessResponse {
    /// Whether the server is ready to serve traffic
    pub ready: bool,
    /// Current status
    pub status: HealthStatus,
    /// Component statuses
    pub components: Vec<ComponentHealth>,
    /// Dependency statuses
    pub dependencies: Vec<DependencyHealth>,
    /// Timestamp
    pub timestamp: u64,
}

// ---------------------------------------------------------------------------
// Overall health check response (enhanced)
// ---------------------------------------------------------------------------

/// Overall health check response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckResponse {
    /// Overall status
    pub status: HealthStatus,
    /// Server version
    pub version: String,
    /// Uptime in seconds
    pub uptime_seconds: u64,
    /// Component health statuses
    pub components: Vec<ComponentHealth>,
    /// Dependency health statuses
    pub dependencies: Vec<DependencyHealth>,
    /// Deep probe results (keyed by probe name)
    pub probes: HashMap<String, HealthProbeResult>,
    /// Uptime percentage from history
    pub uptime_percent: f64,
    /// Current timestamp
    pub timestamp: u64,
}

// ---------------------------------------------------------------------------
// HealthChecker
// ---------------------------------------------------------------------------

/// Health checker
///
/// Tracks the health of various server components, runs deep probes,
/// maintains health history, and aggregates dependency health.
#[derive(Clone)]
pub struct HealthChecker {
    inner: Arc<HealthCheckerInner>,
}

struct HealthCheckerInner {
    /// Server start time
    start_time: AtomicU64,
    /// Overall status (encoded as u64)
    status: AtomicU64,
    /// Storage health
    storage_healthy: AtomicBool,
    /// Network health
    network_healthy: AtomicBool,
    /// Whether cluster mode is enabled
    cluster_enabled: AtomicBool,
    /// Cluster health (only meaningful when cluster_enabled is true)
    cluster_healthy: AtomicBool,
    /// Registered deep health probes
    probes: RwLock<HashMap<String, Arc<dyn DeepHealthCheck>>>,
    /// Registered dependency checkers
    dependency_checkers: RwLock<HashMap<String, Arc<dyn DeepHealthCheck>>>,
    /// Cached dependency health results
    dependency_health: RwLock<HashMap<String, DependencyHealth>>,
    /// Health history
    history: RwLock<HealthHistory>,
}

fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

impl HealthChecker {
    /// Create a new health checker with default history capacity (10)
    pub fn new() -> Self {
        Self::with_history_capacity(10)
    }

    /// Create a new health checker with the given history capacity
    pub fn with_history_capacity(capacity: usize) -> Self {
        Self {
            inner: Arc::new(HealthCheckerInner {
                start_time: AtomicU64::new(now_secs()),
                status: AtomicU64::new(HealthStatus::Starting as u64),
                storage_healthy: AtomicBool::new(false),
                network_healthy: AtomicBool::new(false),
                cluster_enabled: AtomicBool::new(false),
                cluster_healthy: AtomicBool::new(false),
                probes: RwLock::new(HashMap::new()),
                dependency_checkers: RwLock::new(HashMap::new()),
                dependency_health: RwLock::new(HashMap::new()),
                history: RwLock::new(HealthHistory::new(capacity)),
            }),
        }
    }

    // ---- status getters/setters (same API as before) ----

    /// Set overall status
    pub fn set_status(&self, status: HealthStatus) {
        self.inner.status.store(status as u64, Ordering::SeqCst);
    }

    /// Get current overall status
    pub fn status(&self) -> HealthStatus {
        match self.inner.status.load(Ordering::SeqCst) {
            0 => HealthStatus::Healthy,
            1 => HealthStatus::Starting,
            2 => HealthStatus::ShuttingDown,
            3 => HealthStatus::Unhealthy,
            4 => HealthStatus::Degraded,
            _ => HealthStatus::Unhealthy,
        }
    }

    /// Mark storage as healthy
    pub fn set_storage_healthy(&self, healthy: bool) {
        self.inner.storage_healthy.store(healthy, Ordering::SeqCst);
    }

    /// Mark network as healthy
    pub fn set_network_healthy(&self, healthy: bool) {
        self.inner.network_healthy.store(healthy, Ordering::SeqCst);
    }

    /// Mark cluster mode as enabled
    pub fn set_cluster_enabled(&self, enabled: bool) {
        self.inner.cluster_enabled.store(enabled, Ordering::SeqCst);
    }

    /// Mark cluster as healthy
    pub fn set_cluster_healthy(&self, healthy: bool) {
        self.inner.cluster_healthy.store(healthy, Ordering::SeqCst);
    }

    /// Get uptime in seconds
    pub fn uptime_seconds(&self) -> u64 {
        let now = now_secs();
        let start = self.inner.start_time.load(Ordering::SeqCst);
        now.saturating_sub(start)
    }

    // ---- liveness / readiness ----

    /// Check if server is alive (not shutting down or unhealthy).
    /// This is a lightweight check suitable for liveness probes.
    pub fn is_alive(&self) -> bool {
        matches!(
            self.status(),
            HealthStatus::Healthy | HealthStatus::Starting | HealthStatus::Degraded
        )
    }

    /// Check if server is ready to serve traffic.
    /// Returns false during startup, shutdown, and recovery.
    pub fn is_ready(&self) -> bool {
        let status = self.status();
        let base_ok = matches!(status, HealthStatus::Healthy | HealthStatus::Degraded);
        base_ok
            && self.inner.storage_healthy.load(Ordering::SeqCst)
            && self.inner.network_healthy.load(Ordering::SeqCst)
    }

    /// Build a liveness response (lightweight, fast)
    pub fn liveness_response(&self) -> LivenessResponse {
        LivenessResponse {
            alive: self.is_alive(),
            status: self.status(),
            uptime_seconds: self.uptime_seconds(),
            timestamp: now_secs(),
        }
    }

    /// Build a readiness response (includes component and dependency info)
    pub fn readiness_response(&self) -> ReadinessResponse {
        let components = self.build_component_list();
        let dependencies: Vec<DependencyHealth> = self
            .inner
            .dependency_health
            .read()
            .values()
            .cloned()
            .collect();

        ReadinessResponse {
            ready: self.is_ready(),
            status: self.status(),
            components,
            dependencies,
            timestamp: now_secs(),
        }
    }

    // ---- deep probes ----

    /// Register a deep health probe under the given name
    pub fn register_probe(&self, name: impl Into<String>, probe: Arc<dyn DeepHealthCheck>) {
        self.inner.probes.write().insert(name.into(), probe);
    }

    /// Run all registered deep probes and return their results
    pub async fn run_probes(&self) -> HashMap<String, HealthProbeResult> {
        let probes: Vec<(String, Arc<dyn DeepHealthCheck>)> = {
            let guard = self.inner.probes.read();
            guard
                .iter()
                .map(|(k, v)| (k.clone(), Arc::clone(v)))
                .collect()
        };

        let mut results = HashMap::with_capacity(probes.len());
        for (name, probe) in probes {
            let result = probe.check().await;
            results.insert(name, result);
        }
        results
    }

    // ---- dependency health ----

    /// Register a dependency health checker
    pub fn register_dependency(&self, name: impl Into<String>, checker: Arc<dyn DeepHealthCheck>) {
        let name = name.into();
        self.inner
            .dependency_checkers
            .write()
            .insert(name.clone(), checker);
        // Initialize with unknown state
        self.inner.dependency_health.write().insert(
            name.clone(),
            DependencyHealth {
                name,
                status: ProbeStatus::Unhealthy,
                latency_ms: 0.0,
                last_checked: 0,
                message: "not yet checked".to_string(),
            },
        );
    }

    /// Run all dependency checks and update cached results.
    /// Returns the aggregated worst status.
    pub async fn check_dependencies(&self) -> ProbeStatus {
        let checkers: Vec<(String, Arc<dyn DeepHealthCheck>)> = {
            let guard = self.inner.dependency_checkers.read();
            guard
                .iter()
                .map(|(k, v)| (k.clone(), Arc::clone(v)))
                .collect()
        };

        let mut worst = ProbeStatus::Healthy;
        let now = now_secs();

        for (name, checker) in checkers {
            let result = checker.check().await;
            worst = worst.worse(result.status);
            let dep = DependencyHealth {
                name: name.clone(),
                status: result.status,
                latency_ms: result.latency_ms,
                last_checked: now,
                message: result.message,
            };
            self.inner.dependency_health.write().insert(name, dep);
        }

        worst
    }

    /// Get the current aggregated dependency status (from cached results)
    pub fn aggregated_dependency_status(&self) -> ProbeStatus {
        let guard = self.inner.dependency_health.read();
        guard
            .values()
            .fold(ProbeStatus::Healthy, |acc, d| acc.worse(d.status))
    }

    // ---- health history ----

    /// Record a snapshot of the current health state into the history buffer
    pub fn record_snapshot(&self) {
        let snapshot = HealthSnapshot {
            timestamp: now_secs(),
            status: self.status(),
            alive: self.is_alive(),
            ready: self.is_ready(),
        };
        self.inner.history.write().record(snapshot);
    }

    /// Get the health check history as a chronologically ordered list
    pub fn health_history(&self) -> Vec<HealthSnapshot> {
        self.inner.history.read().snapshots()
    }

    /// Get the uptime percentage from the history buffer
    pub fn uptime_percent(&self) -> f64 {
        self.inner.history.read().uptime_percent()
    }

    // ---- full health response ----

    /// Get full health check response (enhanced with probes, deps, history)
    pub fn get_health(&self) -> HealthCheckResponse {
        let components = self.build_component_list();
        let dependencies: Vec<DependencyHealth> = self
            .inner
            .dependency_health
            .read()
            .values()
            .cloned()
            .collect();
        let probes = HashMap::new(); // synchronous path — probes not run
        let uptime_pct = self.uptime_percent();

        HealthCheckResponse {
            status: self.status(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            uptime_seconds: self.uptime_seconds(),
            components,
            dependencies,
            probes,
            uptime_percent: uptime_pct,
            timestamp: now_secs(),
        }
    }

    /// Get full health check response including deep probe results (async)
    pub async fn get_health_deep(&self) -> HealthCheckResponse {
        let components = self.build_component_list();
        let dependencies: Vec<DependencyHealth> = self
            .inner
            .dependency_health
            .read()
            .values()
            .cloned()
            .collect();
        let probes = self.run_probes().await;
        let uptime_pct = self.uptime_percent();

        HealthCheckResponse {
            status: self.status(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            uptime_seconds: self.uptime_seconds(),
            components,
            dependencies,
            probes,
            uptime_percent: uptime_pct,
            timestamp: now_secs(),
        }
    }

    /// Format health as JSON
    pub fn get_health_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.get_health())
    }

    // ---- helpers ----

    fn build_component_list(&self) -> Vec<ComponentHealth> {
        let now = now_secs();

        let storage_status = if self.inner.storage_healthy.load(Ordering::SeqCst) {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unhealthy
        };

        let network_status = if self.inner.network_healthy.load(Ordering::SeqCst) {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unhealthy
        };

        let cluster_healthy = self.inner.cluster_healthy.load(Ordering::SeqCst);
        let cluster_enabled = self.inner.cluster_enabled.load(Ordering::SeqCst);
        let cluster_status = if cluster_enabled {
            if cluster_healthy {
                HealthStatus::Healthy
            } else {
                HealthStatus::Unhealthy
            }
        } else {
            HealthStatus::Starting // Cluster is optional, not enabled
        };

        let cluster_message = if cluster_enabled {
            if cluster_healthy {
                "cluster active".to_string()
            } else {
                "cluster unhealthy".to_string()
            }
        } else {
            "cluster disabled (standalone mode)".to_string()
        };

        vec![
            ComponentHealth {
                name: "storage".to_string(),
                status: storage_status,
                message: None,
                last_check: now,
            },
            ComponentHealth {
                name: "network".to_string(),
                status: network_status,
                message: None,
                last_check: now,
            },
            ComponentHealth {
                name: "cluster".to_string(),
                status: cluster_status,
                message: Some(cluster_message),
                last_check: now,
            },
        ]
    }
}

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

// ---------------------------------------------------------------------------
// HTTP health check server
// ---------------------------------------------------------------------------

/// Lightweight HTTP server that exposes health check endpoints.
///
/// Routes:
/// - `GET /health`  — full health status (JSON)
/// - `GET /healthz` — simple alive check (200 or 503)
/// - `GET /readyz`  — readiness check (200 or 503)
/// - `GET /livez`   — liveness check (200 or 503)
/// - `GET /metrics` — health metrics (history, uptime percentage)
pub struct HealthHttpServer {
    checker: Arc<HealthChecker>,
    bind_addr: SocketAddr,
    shutdown: Arc<AtomicBool>,
}

/// Handle returned by [`HealthHttpServer::start`] to control the running server.
pub struct HealthHttpHandle {
    shutdown: Arc<AtomicBool>,
    port: u16,
    join_handle: tokio::task::JoinHandle<Result<(), std::io::Error>>,
}

impl HealthHttpHandle {
    /// Signal the HTTP health server to stop accepting new connections.
    pub fn stop(&self) {
        self.shutdown.store(true, Ordering::SeqCst);
    }

    /// Return the port the server is listening on.
    pub fn port(&self) -> u16 {
        self.port
    }

    /// Wait for the server task to finish after calling [`stop`](Self::stop).
    pub async fn join(self) -> Result<(), std::io::Error> {
        match self.join_handle.await {
            Ok(inner) => inner,
            Err(e) => Err(std::io::Error::other(e)),
        }
    }
}

impl HealthHttpServer {
    /// Create a new health HTTP server.
    ///
    /// `bind_addr` is the address to listen on (e.g. `0.0.0.0:8081`).
    pub fn new(checker: Arc<HealthChecker>, bind_addr: SocketAddr) -> Self {
        Self {
            checker,
            bind_addr,
            shutdown: Arc::new(AtomicBool::new(false)),
        }
    }

    /// Start the server in a background tokio task.
    ///
    /// Returns a [`HealthHttpHandle`] that can be used to query the port and
    /// signal shutdown.
    pub async fn start(self) -> Result<HealthHttpHandle, std::io::Error> {
        let listener = TcpListener::bind(self.bind_addr).await?;
        let local_addr = listener.local_addr()?;
        let port = local_addr.port();
        let shutdown = Arc::clone(&self.shutdown);
        let checker = Arc::clone(&self.checker);

        let shutdown_flag = Arc::clone(&shutdown);
        let join_handle =
            tokio::spawn(async move { Self::accept_loop(listener, checker, shutdown_flag).await });

        Ok(HealthHttpHandle {
            shutdown,
            port,
            join_handle,
        })
    }

    /// Main accept loop.
    async fn accept_loop(
        listener: TcpListener,
        checker: Arc<HealthChecker>,
        shutdown: Arc<AtomicBool>,
    ) -> Result<(), std::io::Error> {
        loop {
            if shutdown.load(Ordering::SeqCst) {
                debug!("health HTTP server shutting down");
                break;
            }

            // Use a short timeout so we can check the shutdown flag periodically.
            let accept_result =
                tokio::time::timeout(Duration::from_millis(200), listener.accept()).await;

            match accept_result {
                Ok(Ok((stream, _addr))) => {
                    let checker = Arc::clone(&checker);
                    tokio::spawn(async move {
                        if let Err(e) = Self::handle_connection(stream, &checker).await {
                            warn!("health HTTP connection error: {e}");
                        }
                    });
                }
                Ok(Err(e)) => {
                    warn!("health HTTP accept error: {e}");
                }
                Err(_) => {
                    // Timeout — loop back to check shutdown flag
                }
            }
        }
        Ok(())
    }

    /// Handle a single TCP connection: read the HTTP request, route, respond.
    async fn handle_connection(
        mut stream: tokio::net::TcpStream,
        checker: &HealthChecker,
    ) -> Result<(), std::io::Error> {
        let mut buf = [0u8; 4096];
        let n = stream.read(&mut buf).await?;
        if n == 0 {
            return Ok(());
        }

        let request = String::from_utf8_lossy(&buf[..n]);
        let (method, path) = Self::parse_request_line(&request);

        let (status_code, status_text, body) = match method {
            "GET" => Self::route(path, checker),
            _ => (
                405,
                "Method Not Allowed",
                r#"{"error":"method not allowed"}"#.to_string(),
            ),
        };

        let response = format!(
            "HTTP/1.1 {} {}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
            status_code,
            status_text,
            body.len(),
            body
        );

        stream.write_all(response.as_bytes()).await?;
        stream.flush().await?;
        Ok(())
    }

    /// Parse the request line (first line of the HTTP request).
    /// Returns (method, path). Defaults to ("", "") on malformed input.
    fn parse_request_line(request: &str) -> (&str, &str) {
        let first_line = request.lines().next().unwrap_or("");
        let mut parts = first_line.split_whitespace();
        let method = parts.next().unwrap_or("");
        let path = parts.next().unwrap_or("");
        (method, path)
    }

    /// Route a GET request to the appropriate handler.
    fn route(path: &str, checker: &HealthChecker) -> (u16, &'static str, String) {
        match path {
            "/health" => Self::handle_health(checker),
            "/healthz" => Self::handle_healthz(checker),
            "/readyz" => Self::handle_readyz(checker),
            "/livez" => Self::handle_livez(checker),
            "/metrics" => Self::handle_metrics(checker),
            _ => (404, "Not Found", r#"{"error":"not found"}"#.to_string()),
        }
    }

    /// `GET /health` — full health status JSON.
    fn handle_health(checker: &HealthChecker) -> (u16, &'static str, String) {
        let health = checker.get_health();
        let status_code = match health.status {
            HealthStatus::Healthy | HealthStatus::Degraded => 200,
            _ => 503,
        };
        let status_text = if status_code == 200 {
            "OK"
        } else {
            "Service Unavailable"
        };
        let body = serde_json::to_string(&health)
            .unwrap_or_else(|e| format!(r#"{{"error":"serialization failed: {e}"}}"#));
        (status_code, status_text, body)
    }

    /// `GET /healthz` — simple alive check.
    fn handle_healthz(checker: &HealthChecker) -> (u16, &'static str, String) {
        let alive = checker.is_alive();
        let status_code = if alive { 200 } else { 503 };
        let status_text = if alive { "OK" } else { "Service Unavailable" };
        let body = format!(r#"{{"alive":{alive}}}"#);
        (status_code, status_text, body)
    }

    /// `GET /readyz` — readiness check.
    fn handle_readyz(checker: &HealthChecker) -> (u16, &'static str, String) {
        let ready = checker.is_ready();
        let status_code = if ready { 200 } else { 503 };
        let status_text = if ready { "OK" } else { "Service Unavailable" };
        let body = format!(r#"{{"ready":{ready}}}"#);
        (status_code, status_text, body)
    }

    /// `GET /livez` — liveness check.
    fn handle_livez(checker: &HealthChecker) -> (u16, &'static str, String) {
        let resp = checker.liveness_response();
        let status_code = if resp.alive { 200 } else { 503 };
        let status_text = if resp.alive {
            "OK"
        } else {
            "Service Unavailable"
        };
        let body = serde_json::to_string(&resp)
            .unwrap_or_else(|e| format!(r#"{{"error":"serialization failed: {e}"}}"#));
        (status_code, status_text, body)
    }

    /// `GET /metrics` — health history and uptime metrics.
    fn handle_metrics(checker: &HealthChecker) -> (u16, &'static str, String) {
        let history = checker.health_history();
        let uptime_percent = checker.uptime_percent();
        let uptime_seconds = checker.uptime_seconds();

        #[derive(Serialize)]
        struct MetricsResponse {
            uptime_seconds: u64,
            uptime_percent: f64,
            history_count: usize,
            history: Vec<HealthSnapshot>,
        }

        let resp = MetricsResponse {
            uptime_seconds,
            uptime_percent,
            history_count: history.len(),
            history,
        };

        let body = serde_json::to_string(&resp)
            .unwrap_or_else(|e| format!(r#"{{"error":"serialization failed: {e}"}}"#));
        (200, "OK", body)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // ---- Original tests (preserved) ----

    #[test]
    fn test_health_checker_creation() {
        let checker = HealthChecker::new();
        assert_eq!(checker.status(), HealthStatus::Starting);
        assert!(!checker.is_ready());
        assert!(checker.is_alive());
    }

    #[test]
    fn test_set_status() {
        let checker = HealthChecker::new();

        checker.set_status(HealthStatus::Healthy);
        assert_eq!(checker.status(), HealthStatus::Healthy);

        checker.set_status(HealthStatus::ShuttingDown);
        assert_eq!(checker.status(), HealthStatus::ShuttingDown);

        checker.set_status(HealthStatus::Unhealthy);
        assert_eq!(checker.status(), HealthStatus::Unhealthy);
    }

    #[test]
    fn test_component_health() {
        let checker = HealthChecker::new();

        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);
        checker.set_cluster_healthy(true);
        checker.set_status(HealthStatus::Healthy);

        assert!(checker.is_ready());
        assert!(checker.is_alive());
    }

    #[test]
    fn test_not_ready_when_components_unhealthy() {
        let checker = HealthChecker::new();

        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(false); // Storage not healthy

        assert!(!checker.is_ready());
    }

    #[test]
    fn test_uptime() {
        let checker = HealthChecker::new();
        sleep(Duration::from_millis(100));

        let uptime = checker.uptime_seconds();
        // Uptime should be a reasonable value (u64 is always >= 0)
        assert!(uptime < 1000); // Should be less than 1000 seconds
    }

    #[test]
    fn test_health_response() {
        let checker = HealthChecker::new();
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);
        checker.set_status(HealthStatus::Healthy);

        let health = checker.get_health();
        assert_eq!(health.status, HealthStatus::Healthy);
        assert_eq!(health.components.len(), 3);
        assert_eq!(health.version, env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn test_health_json() {
        let checker = HealthChecker::new();
        let json = checker.get_health_json();
        assert!(json.is_ok());

        let json_str = json.expect("JSON serialization failed");
        assert!(json_str.contains("status"));
        assert!(json_str.contains("version"));
        assert!(json_str.contains("components"));
    }

    #[test]
    fn test_is_alive() {
        let checker = HealthChecker::new();

        checker.set_status(HealthStatus::Starting);
        assert!(checker.is_alive());

        checker.set_status(HealthStatus::Healthy);
        assert!(checker.is_alive());

        checker.set_status(HealthStatus::ShuttingDown);
        assert!(!checker.is_alive());

        checker.set_status(HealthStatus::Unhealthy);
        assert!(!checker.is_alive());
    }

    // ---- Deep probe tests ----

    /// A simple test probe that always returns healthy
    struct AlwaysHealthyProbe;

    #[async_trait]
    impl DeepHealthCheck for AlwaysHealthyProbe {
        async fn check(&self) -> HealthProbeResult {
            HealthProbeResult {
                status: ProbeStatus::Healthy,
                latency_ms: 0.1,
                message: "always healthy".to_string(),
            }
        }
    }

    /// A probe that returns unhealthy
    struct AlwaysUnhealthyProbe;

    #[async_trait]
    impl DeepHealthCheck for AlwaysUnhealthyProbe {
        async fn check(&self) -> HealthProbeResult {
            HealthProbeResult {
                status: ProbeStatus::Unhealthy,
                latency_ms: 5.0,
                message: "always unhealthy".to_string(),
            }
        }
    }

    /// A probe that returns degraded
    struct AlwaysDegradedProbe;

    #[async_trait]
    impl DeepHealthCheck for AlwaysDegradedProbe {
        async fn check(&self) -> HealthProbeResult {
            HealthProbeResult {
                status: ProbeStatus::Degraded,
                latency_ms: 2.0,
                message: "always degraded".to_string(),
            }
        }
    }

    #[tokio::test]
    async fn test_deep_probe_execution_and_result_reporting() {
        let checker = HealthChecker::new();
        checker.register_probe("test_healthy", Arc::new(AlwaysHealthyProbe));
        checker.register_probe("test_unhealthy", Arc::new(AlwaysUnhealthyProbe));

        let results = checker.run_probes().await;
        assert_eq!(results.len(), 2);

        let healthy = results.get("test_healthy").expect("missing healthy probe");
        assert_eq!(healthy.status, ProbeStatus::Healthy);
        assert_eq!(healthy.message, "always healthy");

        let unhealthy = results
            .get("test_unhealthy")
            .expect("missing unhealthy probe");
        assert_eq!(unhealthy.status, ProbeStatus::Unhealthy);
        assert_eq!(unhealthy.message, "always unhealthy");
    }

    #[tokio::test]
    async fn test_storage_probe_passes_with_valid_storage() {
        let dir = std::env::temp_dir().join("amaters_health_test_storage");
        let _ = std::fs::create_dir_all(&dir);

        let probe = StorageProbe::new(dir.clone());
        let result = probe.check().await;

        assert_eq!(result.status, ProbeStatus::Healthy);
        assert!(result.latency_ms >= 0.0);
        assert!(result.message.contains("OK"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_storage_probe_fails_with_invalid_path() {
        let probe = StorageProbe::new(std::path::PathBuf::from(
            "/nonexistent_path_for_health_check_test_12345",
        ));
        let result = probe.check().await;
        assert_eq!(result.status, ProbeStatus::Unhealthy);
    }

    #[tokio::test]
    async fn test_wal_probe_passes() {
        let dir = std::env::temp_dir().join("amaters_health_test_wal");
        let _ = std::fs::create_dir_all(&dir);

        let probe = WalProbe::new(dir.clone());
        let result = probe.check().await;

        assert_eq!(result.status, ProbeStatus::Healthy);
        assert!(result.message.contains("appendable"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_disk_space_probe_healthy() {
        // Threshold of 1 byte — should always pass on a running system
        let probe = DiskSpaceProbe::new(std::env::temp_dir(), 1);
        let result = probe.check().await;
        assert_eq!(result.status, ProbeStatus::Healthy);
    }

    // ---- Liveness vs readiness ----

    #[test]
    fn test_liveness_vs_readiness_during_startup() {
        let checker = HealthChecker::new();
        // Starting state: alive but not ready
        assert!(checker.is_alive());
        assert!(!checker.is_ready());

        let live_resp = checker.liveness_response();
        assert!(live_resp.alive);

        let ready_resp = checker.readiness_response();
        assert!(!ready_resp.ready);
    }

    #[test]
    fn test_liveness_vs_readiness_during_shutdown() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::ShuttingDown);

        assert!(!checker.is_alive());
        assert!(!checker.is_ready());

        let live_resp = checker.liveness_response();
        assert!(!live_resp.alive);

        let ready_resp = checker.readiness_response();
        assert!(!ready_resp.ready);
    }

    #[test]
    fn test_readiness_requires_components() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        // Storage and network still false
        assert!(!checker.is_ready());

        checker.set_storage_healthy(true);
        assert!(!checker.is_ready()); // network still down

        checker.set_network_healthy(true);
        assert!(checker.is_ready()); // now ready
    }

    // ---- Health history ring buffer ----

    #[test]
    fn test_health_history_ring_buffer_correctness() {
        let mut history = HealthHistory::new(3);

        // Record 5 entries — buffer should keep last 3
        for i in 0..5u64 {
            history.record(HealthSnapshot {
                timestamp: i,
                status: HealthStatus::Healthy,
                alive: true,
                ready: true,
            });
        }

        let snaps = history.snapshots();
        assert_eq!(snaps.len(), 3);
        // Oldest should be timestamp 2
        assert_eq!(snaps[0].timestamp, 2);
        assert_eq!(snaps[1].timestamp, 3);
        assert_eq!(snaps[2].timestamp, 4);
    }

    #[test]
    fn test_health_history_partial_fill() {
        let mut history = HealthHistory::new(10);

        history.record(HealthSnapshot {
            timestamp: 100,
            status: HealthStatus::Healthy,
            alive: true,
            ready: true,
        });
        history.record(HealthSnapshot {
            timestamp: 200,
            status: HealthStatus::Unhealthy,
            alive: false,
            ready: false,
        });

        let snaps = history.snapshots();
        assert_eq!(snaps.len(), 2);
        assert_eq!(snaps[0].timestamp, 100);
        assert_eq!(snaps[1].timestamp, 200);
    }

    // ---- Uptime percentage ----

    #[test]
    fn test_uptime_percentage_all_alive() {
        let mut history = HealthHistory::new(5);
        for i in 0..5 {
            history.record(HealthSnapshot {
                timestamp: i,
                status: HealthStatus::Healthy,
                alive: true,
                ready: true,
            });
        }
        let pct = history.uptime_percent();
        assert!((pct - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_uptime_percentage_partial() {
        let mut history = HealthHistory::new(4);
        // 3 alive, 1 dead => 75%
        for i in 0..3 {
            history.record(HealthSnapshot {
                timestamp: i,
                status: HealthStatus::Healthy,
                alive: true,
                ready: true,
            });
        }
        history.record(HealthSnapshot {
            timestamp: 3,
            status: HealthStatus::Unhealthy,
            alive: false,
            ready: false,
        });

        let pct = history.uptime_percent();
        assert!((pct - 75.0).abs() < 0.01);
    }

    #[test]
    fn test_uptime_percentage_empty_is_100() {
        let history = HealthHistory::new(10);
        assert!((history.uptime_percent() - 100.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_health_checker_uptime_percent_and_history() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);

        checker.record_snapshot();
        checker.record_snapshot();

        checker.set_status(HealthStatus::Unhealthy);
        checker.record_snapshot();

        let history = checker.health_history();
        assert_eq!(history.len(), 3);

        // 2 alive, 1 not alive
        let pct = checker.uptime_percent();
        assert!((pct - 100.0 * 2.0 / 3.0).abs() < 0.01);
    }

    // ---- Dependency aggregation ----

    #[tokio::test]
    async fn test_dependency_aggregation_one_unhealthy() {
        let checker = HealthChecker::new();
        checker.register_dependency("dep_ok", Arc::new(AlwaysHealthyProbe));
        checker.register_dependency("dep_bad", Arc::new(AlwaysUnhealthyProbe));

        let worst = checker.check_dependencies().await;
        assert_eq!(worst, ProbeStatus::Unhealthy);

        // Aggregated should also be unhealthy
        assert_eq!(
            checker.aggregated_dependency_status(),
            ProbeStatus::Unhealthy
        );
    }

    #[tokio::test]
    async fn test_dependency_aggregation_all_healthy() {
        let checker = HealthChecker::new();
        checker.register_dependency("dep_a", Arc::new(AlwaysHealthyProbe));
        checker.register_dependency("dep_b", Arc::new(AlwaysHealthyProbe));

        let worst = checker.check_dependencies().await;
        assert_eq!(worst, ProbeStatus::Healthy);
    }

    #[tokio::test]
    async fn test_dependency_health_in_readiness_response() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);
        checker.register_dependency("cache", Arc::new(AlwaysHealthyProbe));

        let _ = checker.check_dependencies().await;

        let resp = checker.readiness_response();
        assert!(resp.ready);
        assert_eq!(resp.dependencies.len(), 1);
        assert_eq!(resp.dependencies[0].name, "cache");
        assert_eq!(resp.dependencies[0].status, ProbeStatus::Healthy);
    }

    // ---- Degraded state ----

    #[test]
    fn test_degraded_state_alive_and_ready() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Degraded);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);

        // Degraded is alive and can be ready
        assert!(checker.is_alive());
        assert!(checker.is_ready());
    }

    #[tokio::test]
    async fn test_degraded_dependency_aggregation() {
        let checker = HealthChecker::new();
        checker.register_dependency("dep_ok", Arc::new(AlwaysHealthyProbe));
        checker.register_dependency("dep_degraded", Arc::new(AlwaysDegradedProbe));

        let worst = checker.check_dependencies().await;
        assert_eq!(worst, ProbeStatus::Degraded);
    }

    // ---- Concurrent health checks ----

    #[tokio::test]
    async fn test_concurrent_health_checks() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);
        checker.register_probe("probe_a", Arc::new(AlwaysHealthyProbe));
        checker.register_dependency("dep_a", Arc::new(AlwaysHealthyProbe));

        // Run multiple operations concurrently
        let checker_clone1 = checker.clone();
        let checker_clone2 = checker.clone();
        let checker_clone3 = checker.clone();

        let (r1, r2, r3) = tokio::join!(
            async move { checker_clone1.run_probes().await },
            async move { checker_clone2.check_dependencies().await },
            async move {
                checker_clone3.record_snapshot();
                checker_clone3.health_history()
            },
        );

        assert_eq!(r1.len(), 1);
        assert_eq!(r2, ProbeStatus::Healthy);
        assert!(!r3.is_empty());
    }

    // ---- ProbeStatus::worse ----

    #[test]
    fn test_probe_status_worse() {
        assert_eq!(
            ProbeStatus::Healthy.worse(ProbeStatus::Healthy),
            ProbeStatus::Healthy
        );
        assert_eq!(
            ProbeStatus::Healthy.worse(ProbeStatus::Degraded),
            ProbeStatus::Degraded
        );
        assert_eq!(
            ProbeStatus::Degraded.worse(ProbeStatus::Healthy),
            ProbeStatus::Degraded
        );
        assert_eq!(
            ProbeStatus::Healthy.worse(ProbeStatus::Unhealthy),
            ProbeStatus::Unhealthy
        );
        assert_eq!(
            ProbeStatus::Degraded.worse(ProbeStatus::Unhealthy),
            ProbeStatus::Unhealthy
        );
    }

    // ---- Deep health response ----

    #[tokio::test]
    async fn test_get_health_deep_includes_probes() {
        let checker = HealthChecker::new();
        checker.register_probe("deep_test", Arc::new(AlwaysHealthyProbe));

        let resp = checker.get_health_deep().await;
        assert_eq!(resp.probes.len(), 1);
        let probe_result = resp.probes.get("deep_test").expect("missing probe result");
        assert_eq!(probe_result.status, ProbeStatus::Healthy);
    }

    // ---- HTTP health server tests ----

    async fn start_test_server(checker: HealthChecker) -> HealthHttpHandle {
        let addr: SocketAddr = "127.0.0.1:0".parse().expect("valid addr");
        HealthHttpServer::new(Arc::new(checker), addr)
            .start()
            .await
            .expect("failed to start health HTTP server")
    }

    async fn http_request(port: u16, method: &str, path: &str) -> (u16, String) {
        let mut stream = tokio::net::TcpStream::connect(format!("127.0.0.1:{port}"))
            .await
            .expect("failed to connect");
        let req =
            format!("{method} {path} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
        stream.write_all(req.as_bytes()).await.expect("write");
        let mut resp = String::new();
        stream.read_to_string(&mut resp).await.expect("read");
        let line = resp.lines().next().unwrap_or("");
        let code: u16 = line
            .split_whitespace()
            .nth(1)
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);
        let body = resp.split("\r\n\r\n").nth(1).unwrap_or("").to_string();
        (code, body)
    }

    async fn http_get(port: u16, path: &str) -> (u16, String) {
        http_request(port, "GET", path).await
    }

    #[tokio::test]
    async fn test_health_http_server_starts() {
        let checker = HealthChecker::new();
        let handle = start_test_server(checker).await;
        let port = handle.port();
        assert!(port > 0);

        // Verify we can connect
        let result = tokio::net::TcpStream::connect(format!("127.0.0.1:{port}")).await;
        assert!(result.is_ok());

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_health_endpoint() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/health").await;
        assert_eq!(status, 200);
        assert!(body.contains("\"status\":\"healthy\""));
        assert!(body.contains("\"version\""));
        assert!(body.contains("\"components\""));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_healthz_endpoint() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/healthz").await;
        assert_eq!(status, 200);
        assert!(body.contains("\"alive\":true"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_healthz_unhealthy() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Unhealthy);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/healthz").await;
        assert_eq!(status, 503);
        assert!(body.contains("\"alive\":false"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_readyz_endpoint() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/readyz").await;
        assert_eq!(status, 200);
        assert!(body.contains("\"ready\":true"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_readyz_not_ready() {
        let checker = HealthChecker::new();
        // Starting status — not ready (storage/network not set)

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/readyz").await;
        assert_eq!(status, 503);
        assert!(body.contains("\"ready\":false"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_livez_endpoint() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/livez").await;
        assert_eq!(status, 200);
        assert!(body.contains("\"alive\":true"));
        assert!(body.contains("\"uptime_seconds\""));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_metrics_endpoint() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);
        checker.record_snapshot();
        checker.record_snapshot();

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/metrics").await;
        assert_eq!(status, 200);
        assert!(body.contains("\"uptime_seconds\""));
        assert!(body.contains("\"uptime_percent\""));
        assert!(body.contains("\"history_count\":2"));
        assert!(body.contains("\"history\""));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_unknown_path_404() {
        let checker = HealthChecker::new();

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_get(port, "/unknown").await;
        assert_eq!(status, 404);
        assert!(body.contains("not found"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_non_get_method_405() {
        let checker = HealthChecker::new();

        let handle = start_test_server(checker).await;
        let port = handle.port();

        let (status, body) = http_request(port, "POST", "/health").await;
        assert_eq!(status, 405);
        assert!(body.contains("method not allowed"));

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_concurrent_http_requests() {
        let checker = HealthChecker::new();
        checker.set_status(HealthStatus::Healthy);
        checker.set_storage_healthy(true);
        checker.set_network_healthy(true);

        let handle = start_test_server(checker).await;
        let port = handle.port();

        // Fire 10 concurrent requests across different endpoints
        let mut tasks = Vec::new();
        for i in 0..10 {
            let path = match i % 4 {
                0 => "/health",
                1 => "/healthz",
                2 => "/readyz",
                _ => "/livez",
            };
            tasks.push(tokio::spawn(async move { http_get(port, path).await }));
        }

        for task in tasks {
            let (status, _body) = task.await.expect("task panicked");
            assert_eq!(status, 200);
        }

        handle.stop();
        let _ = handle.join().await;
    }

    #[tokio::test]
    async fn test_server_shutdown() {
        let checker = HealthChecker::new();

        let handle = start_test_server(checker).await;
        let port = handle.port();

        // Verify server is listening
        let (status, _) = http_get(port, "/healthz").await;
        assert_eq!(status, 200);

        // Signal shutdown
        handle.stop();
        let result = handle.join().await;
        assert!(result.is_ok());

        // After shutdown, connection should fail (with a small delay for cleanup)
        tokio::time::sleep(Duration::from_millis(300)).await;
        let connect_result = tokio::time::timeout(
            Duration::from_millis(500),
            tokio::net::TcpStream::connect(format!("127.0.0.1:{port}")),
        )
        .await;

        // Either timeout or connection refused — both are acceptable
        match connect_result {
            Err(_) => {}     // timeout — server stopped
            Ok(Err(_)) => {} // connection refused — server stopped
            Ok(Ok(_)) => {
                // Connection succeeded — this can happen if the OS hasn't fully
                // released the port yet; we just verify the server task exited.
            }
        }
    }
}