biovault 0.1.89

A bioinformatics data vault CLI tool
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
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use notify::{Config as NotifyConfig, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::signal;
use tracing::{debug, error, info, warn};

use crate::config::Config;
use crate::messages::sync::MessageSync;
use crate::syftbox::app::SyftBoxApp;
use crate::syftbox::{
    detect_mode, is_syftbox_running, start_syftbox as start_syftbox_process, SyftBoxMode,
};

#[derive(Debug, Serialize, Deserialize)]
pub struct DaemonStatus {
    pid: u32,
    started_at: DateTime<Utc>,
    last_sync: Option<DateTime<Utc>>,
    message_count: usize,
    status: String,
}

impl DaemonStatus {
    fn new(pid: u32) -> Self {
        Self {
            pid,
            started_at: Utc::now(),
            last_sync: None,
            message_count: 0,
            status: "running".to_string(),
        }
    }
}

// NOTE: Daemon struct and its methods are NOT suitable for unit testing because they:
// - Require actual file system operations (log files, status files)
// - Depend on MessageSync which requires a database connection
// - Depend on SyftBoxApp which requires SyftBox configuration
// - Perform real process management and signal handling
// These should be tested via integration/e2e tests with proper setup.
pub struct Daemon {
    config: Config,
    sync: MessageSync,
    log_writer: Arc<Mutex<std::fs::File>>,
    status: Arc<Mutex<DaemonStatus>>,
    status_path: PathBuf,
}

impl Daemon {
    // NOT unit testable - requires MessageSync, SyftBoxApp, and file system setup
    pub fn new(config: &Config) -> Result<Self> {
        let biovault_dir = get_biovault_dir(config)?;
        let logs_dir = biovault_dir.join("logs");
        std::fs::create_dir_all(&logs_dir)
            .with_context(|| format!("Failed to create logs directory: {:?}", logs_dir))?;

        let log_path = logs_dir.join("daemon.log");
        let log_writer = Arc::new(Mutex::new(
            std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(&log_path)
                .with_context(|| format!("Failed to create log file: {:?}", log_path))?,
        ));

        let db_path = super::messages::get_message_db_path(config)?;
        let data_dir = config.get_syftbox_data_dir()?;
        let app = SyftBoxApp::new(&data_dir, &config.email, "biovault")?;
        let sync = MessageSync::new(&db_path, app)?;

        let status_path = biovault_dir.join("daemon.status");
        let status = Arc::new(Mutex::new(DaemonStatus::new(std::process::id())));

        Ok(Self {
            config: config.clone(),
            sync,
            log_writer,
            status,
            status_path,
        })
    }

    fn log(&self, level: &str, message: &str) {
        let timestamp = Utc::now().format("%Y-%m-%d %H:%M:%S%.3f UTC");
        let log_line = format!("[{}] [{}] {}\n", timestamp, level, message);

        if let Ok(mut writer) = self.log_writer.lock() {
            let _ = writer.write_all(log_line.as_bytes());
            let _ = writer.flush();
        }

        match level {
            "ERROR" => error!("{}", message),
            "WARN" => warn!("{}", message),
            "INFO" => info!("{}", message),
            "DEBUG" => debug!("{}", message),
            _ => info!("{}", message),
        }
    }

    fn update_status(&self, last_sync: Option<DateTime<Utc>>, message_count: usize) {
        if let Ok(mut status) = self.status.lock() {
            if let Some(sync_time) = last_sync {
                status.last_sync = Some(sync_time);
            }
            status.message_count += message_count;

            let status_json = serde_json::to_string_pretty(&*status).unwrap_or_default();
            let _ = std::fs::write(&self.status_path, status_json);
        }
    }

    // NOT unit testable - requires MessageSync and actual message processing
    async fn sync_messages(&self) -> Result<()> {
        self.log("INFO", "Starting message sync");

        match self.sync.sync_quiet() {
            Ok((new_messages, count)) => {
                if count > 0 {
                    self.log("INFO", &format!("Processed {} new messages", count));
                    for msg_id in new_messages {
                        self.log("DEBUG", &format!("New message: {}", msg_id));
                    }
                } else {
                    self.log("DEBUG", "No new messages");
                }
                self.update_status(Some(Utc::now()), count);
                Ok(())
            }
            Err(e) => {
                self.log("ERROR", &format!("Message sync failed: {}", e));
                Err(e)
            }
        }
    }

    // NOT unit testable - spawns actual SyftBox processes (sbenv or syftbox command)
    async fn start_syftbox(&self) -> Result<()> {
        let mode = detect_mode(&self.config)?;
        match mode {
            SyftBoxMode::Sbenv => self.log("INFO", "Starting SyftBox via sbenv..."),
            SyftBoxMode::Direct => self.log("INFO", "Starting SyftBox directly..."),
        }

        match start_syftbox_process(&self.config) {
            Ok(true) => self.log("INFO", "Successfully started SyftBox"),
            Ok(false) => self.log("INFO", "SyftBox already running"),
            Err(e) => {
                self.log("ERROR", &format!("Failed to start SyftBox: {}", e));
                return Err(e);
            }
        }

        Ok(())
    }

    async fn ensure_syftbox_running(&self) -> Result<()> {
        if is_syftbox_running(&self.config)? {
            self.log("INFO", "SyftBox is already running");
            Ok(())
        } else {
            self.log("WARN", "SyftBox is not running, attempting to start it...");
            self.start_syftbox().await
        }
    }

    // NOT unit testable - main daemon loop with file watching, signal handling, and async runtime
    pub async fn run(&self) -> Result<()> {
        self.log("INFO", "BioVault daemon starting");

        // Log system resource info at startup
        let thread_count = std::thread::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
        self.log("INFO", &format!("System CPU cores: {}", thread_count));
        self.log("INFO", "Tokio worker threads: 2 (limited)");

        // Log resource limits
        let pid = std::process::id();
        if let Ok(limits) = std::fs::read_to_string(format!("/proc/{}/limits", pid)) {
            if let Some(proc_line) = limits.lines().find(|l| l.starts_with("Max processes")) {
                self.log("INFO", &format!("Resource limit: {}", proc_line.trim()));
            }
            if let Some(thread_line) = limits.lines().find(|l| l.contains("threads")) {
                self.log("INFO", &format!("Resource limit: {}", thread_line.trim()));
            }
        }

        self.log("INFO", &format!("Config email: {}", self.config.email));
        self.log(
            "INFO",
            &format!("Config syftbox_config: {:?}", self.config.syftbox_config),
        );

        // Debug: check environment variables
        if let Ok(syftbox_data_dir) = std::env::var("SYFTBOX_DATA_DIR") {
            self.log(
                "INFO",
                &format!("ENV SYFTBOX_DATA_DIR: {}", syftbox_data_dir),
            );
        } else {
            self.log("WARN", "ENV SYFTBOX_DATA_DIR not set");
        }

        let data_dir = self.config.get_syftbox_data_dir()?;
        self.log("INFO", &format!("SyftBox data_dir: {:?}", data_dir));

        let mode = detect_mode(&self.config)?;
        self.log("INFO", &format!("SyftBox mode: {:?}", mode));

        // Ensure SyftBox is running first
        if let Err(e) = self.ensure_syftbox_running().await {
            self.log(
                "WARN",
                &format!(
                    "Could not ensure SyftBox is running: {}. Continuing anyway...",
                    e
                ),
            );
        }

        let app = SyftBoxApp::new(&data_dir, &self.config.email, "biovault")?;
        let watch_path = app
            .data_dir
            .join("datasites")
            .join(&app.email)
            .join("app_data")
            .join("biovault")
            .join("rpc")
            .join("message");

        if !watch_path.exists() {
            std::fs::create_dir_all(&watch_path)
                .with_context(|| format!("Failed to create watch directory: {:?}", watch_path))?;
        }

        self.log("INFO", &format!("Watching directory: {:?}", watch_path));

        let (tx, rx) = mpsc::channel();
        let rx = Arc::new(Mutex::new(rx));

        let mut watcher = RecommendedWatcher::new(
            move |res| {
                if let Err(e) = tx.send(res) {
                    eprintln!("Watch error: {}", e);
                }
            },
            NotifyConfig::default(),
        )?;

        watcher.watch(&watch_path, RecursiveMode::Recursive)?;

        let mut last_sync = Utc::now();
        let mut last_syftbox_check = Utc::now();
        let mut last_stats_log = Utc::now();
        let mut syftbox_restart_attempts = 0;
        const MAX_RESTART_ATTEMPTS: u32 = 3;
        const SYFTBOX_CHECK_INTERVAL_SECS: i64 = 10; // Health check every 10 seconds for testing
        const MESSAGE_SYNC_INTERVAL_SECS: i64 = 30;
        const STATS_LOG_INTERVAL_SECS: i64 = 300; // Log stats every 5 minutes

        // Use tokio interval for periodic checks (runs every 1 second)
        let mut check_interval = tokio::time::interval(Duration::from_secs(1));
        check_interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);

        self.log(
            "INFO",
            &format!(
                "Health check interval: {} seconds",
                SYFTBOX_CHECK_INTERVAL_SECS
            ),
        );
        self.log(
            "INFO",
            &format!(
                "Message sync interval: {} seconds",
                MESSAGE_SYNC_INTERVAL_SECS
            ),
        );

        loop {
            tokio::select! {
                _ = signal::ctrl_c() => {
                    self.log("INFO", "Received shutdown signal");
                    break;
                }
                _ = check_interval.tick() => {
                    let now = Utc::now();

                    // Check for file system events (checked every second for fast response)
                    let has_event = if let Ok(rx) = rx.lock() {
                        // Drain all pending events
                        let mut found_event = false;
                        while rx.try_recv().is_ok() {
                            found_event = true;
                        }
                        found_event
                    } else {
                        false
                    };

                    if has_event {
                        self.log("DEBUG", "File system event detected");
                        match self.sync_messages().await {
                            Ok(_) => {}
                            Err(e) => {
                                self.log("ERROR", &format!("Event-triggered sync failed: {}", e));
                                // Check if it's a resource error
                                if e.to_string().contains("Resource") || e.to_string().contains("thread") {
                                    self.log("FATAL", "Resource exhaustion detected - check thread/process limits");
                                }
                            }
                        }
                        last_sync = now;
                    }

                    // Periodic SyftBox health check
                    let seconds_since_check = (now - last_syftbox_check).num_seconds();
                    if seconds_since_check >= SYFTBOX_CHECK_INTERVAL_SECS {
                        self.log("INFO", &format!("Running SyftBox health check (last check {} seconds ago)...", seconds_since_check));

                        match self.ensure_syftbox_running().await {
                            Ok(_) => {
                                // Reset restart attempts on successful check
                                syftbox_restart_attempts = 0;
                                self.log("INFO", "SyftBox health check passed");
                            }
                            Err(e) => {
                                syftbox_restart_attempts += 1;
                                self.log("ERROR", &format!(
                                    "SyftBox health check failed (attempt {}/{}): {}",
                                    syftbox_restart_attempts, MAX_RESTART_ATTEMPTS, e
                                ));

                                if syftbox_restart_attempts >= MAX_RESTART_ATTEMPTS {
                                    self.log("FATAL", &format!(
                                        "Failed to restart SyftBox after {} attempts. Daemon will exit.",
                                        MAX_RESTART_ATTEMPTS
                                    ));
                                    return Err(anyhow::anyhow!(
                                        "Unable to keep SyftBox running after {} attempts",
                                        MAX_RESTART_ATTEMPTS
                                    ));
                                }
                            }
                        }
                        last_syftbox_check = now;
                    }

                    // Regular message sync
                    let seconds_since_sync = (now - last_sync).num_seconds();
                    if seconds_since_sync >= MESSAGE_SYNC_INTERVAL_SECS {
                        self.log("DEBUG", &format!("Running message sync (last sync {} seconds ago)...", seconds_since_sync));
                        if let Err(e) = self.sync_messages().await {
                            self.log("ERROR", &format!("Scheduled sync failed: {}", e));
                        }
                        last_sync = now;
                    }

                    // Periodic stats logging (every 5 minutes)
                    let seconds_since_stats = (now - last_stats_log).num_seconds();
                    if seconds_since_stats >= STATS_LOG_INTERVAL_SECS {
                        // Log process stats
                        let pid = std::process::id();

                        // Try to get thread count from /proc
                        let thread_count = std::fs::read_to_string(format!("/proc/{}/status", pid))
                            .ok()
                            .and_then(|status| {
                                status.lines()
                                    .find(|line| line.starts_with("Threads:"))
                                    .and_then(|line| line.split_whitespace().nth(1))
                                    .and_then(|s| s.parse::<usize>().ok())
                            })
                            .unwrap_or(0);

                        self.log("INFO", &format!(
                            "Stats: PID={}, Threads={}, Uptime={}min",
                            pid,
                            thread_count,
                            (now - self.status.lock().unwrap().started_at).num_minutes()
                        ));
                        last_stats_log = now;
                    }
                }
            }
        }

        self.log("INFO", "BioVault daemon shutting down");
        let _ = std::fs::remove_file(&self.status_path);
        Ok(())
    }
}

fn get_biovault_dir(_config: &Config) -> Result<PathBuf> {
    crate::config::get_biovault_home()
}

fn get_pid_file_path(config: &Config) -> Result<PathBuf> {
    let biovault_dir = get_biovault_dir(config)?;
    Ok(biovault_dir.join("daemon.pid"))
}

fn get_status_file_path(config: &Config) -> Result<PathBuf> {
    let biovault_dir = get_biovault_dir(config)?;
    Ok(biovault_dir.join("daemon.status"))
}

fn get_log_file_path(config: &Config) -> Result<PathBuf> {
    let biovault_dir = get_biovault_dir(config)?;
    let logs_dir = biovault_dir.join("logs");
    Ok(logs_dir.join("daemon.log"))
}

pub fn is_daemon_running(config: &Config) -> Result<bool> {
    let pid_path = get_pid_file_path(config)?;

    if !pid_path.exists() {
        return Ok(false);
    }

    let pid_str = std::fs::read_to_string(&pid_path)?;
    let pid: u32 = match pid_str.trim().parse() {
        Ok(p) => p,
        Err(_) => {
            // Invalid PID file, clean it up
            let _ = std::fs::remove_file(&pid_path);
            return Ok(false);
        }
    };

    // Check if process exists and is actually a bv daemon process
    let is_running = check_process_running(pid)?;

    if !is_running {
        // Clean up stale PID file
        let _ = std::fs::remove_file(&pid_path);
        let _ = std::fs::remove_file(get_status_file_path(config)?);
    }

    Ok(is_running)
}

// Partially unit testable - can verify it doesn't panic, but actual process checking
// depends on ps/tasklist commands and real PIDs. Integration tests needed for full coverage.
fn check_process_running(pid: u32) -> Result<bool> {
    #[cfg(unix)]
    {
        // First check if process exists
        let exists = unsafe {
            let result = libc::kill(pid as i32, 0);
            result == 0
        };

        if !exists {
            return Ok(false);
        }

        // Verify it's actually a bv daemon process
        let output = Command::new("ps")
            .args(["-p", &pid.to_string(), "-o", "command="])
            .output()?;

        if !output.status.success() {
            return Ok(false);
        }

        let cmd = String::from_utf8_lossy(&output.stdout);
        Ok(cmd.contains("bv daemon") || cmd.contains("biovault"))
    }

    #[cfg(windows)]
    {
        let output = Command::new("tasklist")
            .args(["/FI", &format!("PID eq {}", pid), "/NH", "/FO", "CSV"])
            .output()?;

        if !output.status.success() {
            return Ok(false);
        }

        let output_str = String::from_utf8_lossy(&output.stdout);
        Ok(output_str.contains(&pid.to_string()) && output_str.contains("bv"))
    }

    #[cfg(not(any(unix, windows)))]
    {
        Ok(false)
    }
}

// NOT unit testable - spawns daemon processes, requires full environment setup
// Test via integration tests instead
pub async fn start(config: &Config, foreground: bool) -> Result<()> {
    // Clean up stale PID files first
    cleanup_stale_pid_files(config)?;

    // Check if daemon is already running
    if is_daemon_running(config)? {
        let pid_path = get_pid_file_path(config)?;
        if let Ok(pid_str) = std::fs::read_to_string(&pid_path) {
            if let Ok(pid) = pid_str.trim().parse::<u32>() {
                println!("❌ Daemon is already running (PID: {})", pid);
                return Ok(());
            }
        }
        println!("❌ Daemon is already running");
        return Ok(());
    }

    let biovault_dir = get_biovault_dir(config)?;
    std::fs::create_dir_all(&biovault_dir)
        .with_context(|| format!("Failed to create biovault directory: {:?}", biovault_dir))?;

    if foreground {
        println!("🚀 Starting BioVault daemon in foreground mode");

        let pid = std::process::id();

        // Check if we're spawned from background start
        let pid_file_path = if let Ok(path_str) = std::env::var("BV_DAEMON_PID_FILE") {
            // We're a spawned daemon, use the provided PID file path
            Some(PathBuf::from(path_str))
        } else {
            // We're a manual foreground daemon, use the default PID path
            let path = get_pid_file_path(config)?;
            Some(path)
        };

        // Write PID file
        if let Some(ref pid_path) = pid_file_path {
            std::fs::write(pid_path, pid.to_string())
                .with_context(|| format!("Failed to write PID file: {:?}", pid_path))?;
        }

        let daemon = Daemon::new(config)?;

        // Wrap daemon.run() to catch and log any errors before exiting
        let result = match daemon.run().await {
            Ok(()) => {
                daemon.log("INFO", "Daemon stopped normally");
                Ok(())
            }
            Err(e) => {
                // Log the error in detail
                daemon.log("FATAL", &format!("Daemon crashed with error: {}", e));
                daemon.log("FATAL", &format!("Error chain: {:?}", e));

                // Log the backtrace
                let backtrace = e.backtrace();
                daemon.log("FATAL", &format!("Backtrace:\n{}", backtrace));

                Err(e)
            }
        };

        // Clean up PID file on exit
        if let Some(ref pid_path) = pid_file_path {
            let _ = std::fs::remove_file(pid_path);
        }

        result?;
    } else {
        println!("🚀 Starting BioVault daemon in background");

        let config_json = serde_json::to_string(config).context("Failed to serialize config")?;

        // Get the syftbox data dir now (before spawning) to ensure it's available to child
        let syftbox_data_dir = config.get_syftbox_data_dir()?;

        let current_exe =
            std::env::current_exe().context("Failed to get current executable path")?;

        // Redirect output to log file instead of /dev/null for debugging
        let log_path = get_log_file_path(config)?;

        // Ensure logs directory exists
        if let Some(log_dir) = log_path.parent() {
            std::fs::create_dir_all(log_dir)
                .with_context(|| format!("Failed to create logs directory: {:?}", log_dir))?;
        }

        let log_file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&log_path)
            .context("Failed to open log file for daemon spawn")?;

        let log_file_stderr = log_file.try_clone()?;

        // Pass the PID file path as an environment variable so the child can write it
        let pid_path = get_pid_file_path(config)?;

        let mut child = Command::new(current_exe)
            .args(["daemon", "start", "--foreground"])
            .env("BV_DAEMON_CONFIG", config_json)
            .env("BV_DAEMON_PID_FILE", pid_path.to_string_lossy().to_string())
            .env(
                "SYFTBOX_DATA_DIR",
                syftbox_data_dir.to_string_lossy().to_string(),
            )
            .stdin(Stdio::null())
            .stdout(Stdio::from(log_file))
            .stderr(Stdio::from(log_file_stderr))
            .spawn()
            .context("Failed to spawn daemon process")?;

        let pid = child.id();

        // Wait a moment for the child to start and write its PID file
        tokio::time::sleep(Duration::from_millis(1500)).await;

        // Check if child exited
        match child.try_wait() {
            Ok(Some(status)) => {
                return Err(anyhow::anyhow!(
                    "Daemon process exited with status: {}",
                    status
                ));
            }
            Ok(None) => {
                // Child is still running, verify PID file was written
                if pid_path.exists() {
                    println!("✅ Daemon started successfully (PID: {})", pid);
                    println!("📝 Use 'bv daemon logs' to view daemon logs");
                } else {
                    return Err(anyhow::anyhow!(
                        "Daemon started but PID file was not created"
                    ));
                }
            }
            Err(e) => {
                return Err(anyhow::anyhow!("Failed to check daemon status: {}", e));
            }
        }
    }

    Ok(())
}

fn cleanup_stale_pid_files(config: &Config) -> Result<()> {
    let pid_path = get_pid_file_path(config)?;
    let status_path = get_status_file_path(config)?;

    if !pid_path.exists() {
        return Ok(());
    }

    // Try to read and validate PID
    if let Ok(pid_str) = std::fs::read_to_string(&pid_path) {
        if let Ok(pid) = pid_str.trim().parse::<u32>() {
            // Check if process is actually running
            if let Ok(false) = check_process_running(pid) {
                // Process not running, clean up stale files
                let _ = std::fs::remove_file(&pid_path);
                let _ = std::fs::remove_file(&status_path);
            }
        } else {
            // Invalid PID file, clean it up
            let _ = std::fs::remove_file(&pid_path);
            let _ = std::fs::remove_file(&status_path);
        }
    }

    Ok(())
}

// Partially unit testable - tested with no log file case
// Full testing (reading actual logs, following) requires integration tests
pub async fn logs(config: &Config, follow: bool, lines: Option<usize>) -> Result<()> {
    let log_path = get_log_file_path(config)?;

    if !log_path.exists() {
        println!("📝 No log file found. Start the daemon with 'bv daemon start' to generate logs.");
        return Ok(());
    }

    if follow {
        println!("📖 Following daemon logs (Ctrl+C to stop):");
        println!("════════════════════════════════════════");

        let mut child = Command::new("tail")
            .args(["-f", &log_path.to_string_lossy()])
            .stdout(Stdio::piped())
            .spawn()
            .context("Failed to start tail command")?;

        if let Some(stdout) = child.stdout.take() {
            let reader = BufReader::new(stdout);

            for line in reader.lines() {
                match line {
                    Ok(content) => println!("{}", content),
                    Err(_) => break,
                }
            }
        }

        let _ = child.wait();
    } else {
        let tail_lines = lines.unwrap_or(50);
        println!("📖 Last {} lines of daemon logs:", tail_lines);
        println!("═══════════════════════════════════");

        let output = Command::new("tail")
            .args(["-n", &tail_lines.to_string(), &log_path.to_string_lossy()])
            .output()
            .context("Failed to read log file")?;

        print!("{}", String::from_utf8_lossy(&output.stdout));
    }

    Ok(())
}

pub fn get_daemon_status(config: &Config) -> Result<Option<DaemonStatus>> {
    let status_path = get_status_file_path(config)?;

    if !status_path.exists() {
        return Ok(None);
    }

    let status_json = std::fs::read_to_string(&status_path)?;
    let status: DaemonStatus =
        serde_json::from_str(&status_json).context("Failed to parse daemon status")?;

    Ok(Some(status))
}

// Partially unit testable - tested with no daemon running case
// Full testing (actually stopping a daemon) requires integration tests
pub async fn stop(config: &Config) -> Result<()> {
    if !is_daemon_running(config)? {
        println!("❌ Daemon is not running");
        return Ok(());
    }

    let pid_path = get_pid_file_path(config)?;
    let pid_str = std::fs::read_to_string(&pid_path)?;
    let pid: u32 = pid_str.trim().parse().context("Invalid PID file")?;

    // Platform-specific process termination
    #[cfg(unix)]
    {
        unsafe {
            let result = libc::kill(pid as i32, libc::SIGTERM);
            if result != 0 {
                return Err(anyhow::anyhow!("Failed to stop daemon"));
            }
        }
    }

    #[cfg(windows)]
    {
        let output = Command::new("taskkill")
            .args(["/F", "/PID", &pid.to_string()])
            .output()?;
        if !output.status.success() {
            return Err(anyhow::anyhow!("Failed to stop daemon"));
        }
    }

    #[cfg(not(any(unix, windows)))]
    {
        return Err(anyhow::anyhow!(
            "Stopping daemon not supported on this platform"
        ));
    }

    // Clean up PID and status files
    let _ = std::fs::remove_file(&pid_path);
    let _ = std::fs::remove_file(get_status_file_path(config)?);

    println!("✅ Daemon stopped successfully");
    Ok(())
}

fn check_systemd_available() -> Result<()> {
    // Runtime OS check instead of compile-time
    if !cfg!(target_os = "linux") {
        return Err(anyhow::anyhow!(
            "Service installation is only supported on Linux systems"
        ));
    }

    // Check if systemd is available
    let output = Command::new("systemctl")
        .arg("--version")
        .output()
        .context("systemctl not found. This system doesn't appear to use systemd")?;

    if !output.status.success() {
        return Err(anyhow::anyhow!(
            "systemd is not available on this system. Service installation requires systemd."
        ));
    }
    Ok(())
}

fn get_service_name(config: &Config) -> String {
    // Create unique service name per email/sbenv
    let safe_email = config.email.replace('@', "-at-").replace('.', "-");
    format!("biovault-daemon-{}.service", safe_email)
}

fn generate_systemd_service_content(config: &Config) -> Result<String> {
    let home_dir =
        dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;

    let user = std::env::var("USER").unwrap_or_else(|_| "nobody".to_string());

    // Get the data dir for this sbenv
    let data_dir = config.get_syftbox_data_dir()?;
    let data_dir_str = data_dir.to_string_lossy();

    // Check if we're in an sbenv
    let sbenv_file = data_dir.join(".sbenv");
    let is_sbenv = sbenv_file.exists();

    // Find the full path to bv (current executable)
    let bv_path = std::env::current_exe()
        .context("Failed to get current executable path")?
        .to_string_lossy()
        .to_string();

    // Generate the ExecStart command
    let exec_start = if is_sbenv {
        // Find the full path to sbenv
        let sbenv_path = Command::new("which")
            .arg("sbenv")
            .output()
            .ok()
            .and_then(|out| {
                if out.status.success() {
                    String::from_utf8(out.stdout)
                        .ok()
                        .map(|s| s.trim().to_string())
                } else {
                    None
                }
            })
            .unwrap_or_else(|| {
                // Default fallback to common installation paths
                let cargo_bin = format!("{}/.cargo/bin/sbenv", home_dir.display());
                let local_bin = format!("{}/.local/bin/sbenv", home_dir.display());
                if std::path::Path::new(&cargo_bin).exists() {
                    cargo_bin
                } else {
                    local_bin
                }
            });

        // Use sbenv exec to run bv within the sbenv context
        format!(
            "{} exec {} {} daemon start --foreground",
            sbenv_path, config.email, bv_path
        )
    } else {
        // Use the bv path directly
        format!("{} daemon start --foreground", bv_path)
    };

    // Set working directory to the data dir for sbenv context
    let working_dir = if is_sbenv {
        data_dir_str.to_string()
    } else {
        home_dir.display().to_string()
    };

    let service_content = format!(
        r#"[Unit]
Description=BioVault Daemon ({email})
After=network.target
Wants=network-online.target

[Service]
Type=simple
User={user}
Group={user}
WorkingDirectory={working_dir}
ExecStart={exec_start}
Restart=on-failure
RestartSec=10
StandardOutput=journal
StandardError=journal
SyslogIdentifier=biovault-{safe_email}
Environment="HOME={home_dir}"
Environment="PATH=/usr/local/bin:/usr/bin:/bin:{home_dir}/.local/bin:{home_dir}/.cargo/bin"
Environment="RUST_BACKTRACE=1"

# Security settings
PrivateTmp=true
NoNewPrivileges=true
ProtectSystem=full

[Install]
WantedBy=multi-user.target
"#,
        user = user,
        home_dir = home_dir.display(),
        email = config.email,
        safe_email = config.email.replace('@', "-at-").replace('.', "-"),
        exec_start = exec_start,
        working_dir = working_dir,
    );

    Ok(service_content)
}

// NOT unit testable - requires systemd, writes to system directories
// Test via integration tests on Linux systems only
pub async fn install_service(config: &Config) -> Result<()> {
    check_systemd_available()?;

    // Check if service is already installed
    let service_name = get_service_name(config);
    let check_output = Command::new("systemctl")
        .args(["status", &service_name])
        .output()?;

    if check_output.status.success() || check_output.status.code() == Some(3) {
        // Status code 3 means service exists but is inactive
        println!("⚠️  Service '{}' is already installed", service_name);
        println!("   Use 'bv daemon uninstall' first if you want to reinstall");
        return Ok(());
    }

    println!("📦 Installing BioVault daemon as systemd service...");

    // Generate service file content
    let service_content = generate_systemd_service_content(config)?;

    // Write service file to temporary location
    let temp_service_path = format!("/tmp/{}", service_name);
    std::fs::write(&temp_service_path, service_content)
        .context("Failed to write temporary service file")?;

    // Move service file to systemd directory (requires sudo)
    println!("🔐 Installing service (requires sudo)...");
    let install_output = Command::new("sudo")
        .args([
            "mv",
            &temp_service_path,
            &format!("/etc/systemd/system/{}", service_name),
        ])
        .output()
        .context("Failed to install service file. Make sure you have sudo privileges")?;

    if !install_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to install service: {}",
            String::from_utf8_lossy(&install_output.stderr)
        ));
    }

    // Reload systemd daemon
    println!("🔄 Reloading systemd daemon...");
    let reload_output = Command::new("sudo")
        .args(["systemctl", "daemon-reload"])
        .output()
        .context("Failed to reload systemd daemon")?;

    if !reload_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to reload systemd: {}",
            String::from_utf8_lossy(&reload_output.stderr)
        ));
    }

    // Enable service to start on boot
    println!("🚀 Enabling service to start on boot...");
    let enable_output = Command::new("sudo")
        .args(["systemctl", "enable", &service_name])
        .output()
        .context("Failed to enable service")?;

    if !enable_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to enable service: {}",
            String::from_utf8_lossy(&enable_output.stderr)
        ));
    }

    // Start the service
    println!("▶️  Starting service...");
    let start_output = Command::new("sudo")
        .args(["systemctl", "start", &service_name])
        .output()
        .context("Failed to start service")?;

    if !start_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to start service: {}",
            String::from_utf8_lossy(&start_output.stderr)
        ));
    }

    println!("✅ BioVault daemon installed and started successfully!");
    println!("\n📊 Service Management Commands:");
    println!("   • Status:  sudo systemctl status {}", service_name);
    println!("   • Stop:    sudo systemctl stop {}", service_name);
    println!("   • Start:   sudo systemctl start {}", service_name);
    println!("   • Restart: sudo systemctl restart {}", service_name);
    println!("   • Logs:    sudo journalctl -u {} -f", service_name);
    println!("\n   Or use 'bv daemon status' for a quick check");

    Ok(())
}

// NOT unit testable - requires systemd, modifies system services
// Test via integration tests on Linux systems only
pub async fn uninstall_service(config: &Config) -> Result<()> {
    check_systemd_available()?;

    let service_name = get_service_name(config);

    println!("🗑️  Uninstalling BioVault daemon service...");

    // Stop the service if running
    println!("⏹️  Stopping service...");
    let stop_output = Command::new("sudo")
        .args(["systemctl", "stop", &service_name])
        .output()?;

    if !stop_output.status.success() {
        // Service might not be running, continue anyway
        println!("   (Service was not running)");
    }

    // Disable the service
    println!("🚫 Disabling service...");
    let disable_output = Command::new("sudo")
        .args(["systemctl", "disable", &service_name])
        .output()?;

    if !disable_output.status.success() {
        println!("   (Service was not enabled)");
    }

    // Remove service file
    println!("🗑️  Removing service file...");
    let remove_output = Command::new("sudo")
        .args(["rm", "-f", &format!("/etc/systemd/system/{}", service_name)])
        .output()?;

    if !remove_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to remove service file: {}",
            String::from_utf8_lossy(&remove_output.stderr)
        ));
    }

    // Reload systemd daemon
    println!("🔄 Reloading systemd daemon...");
    let reload_output = Command::new("sudo")
        .args(["systemctl", "daemon-reload"])
        .output()?;

    if !reload_output.status.success() {
        return Err(anyhow::anyhow!(
            "Failed to reload systemd: {}",
            String::from_utf8_lossy(&reload_output.stderr)
        ));
    }

    println!("✅ BioVault daemon service uninstalled successfully!");
    Ok(())
}

pub async fn list_services() -> Result<()> {
    // Check if systemd is available (runtime check)
    if !cfg!(target_os = "linux") {
        println!("⚠️  Service listing is only supported on Linux systems");
        return Ok(());
    }

    // Check if systemctl is available
    let check = Command::new("systemctl").arg("--version").output();

    if check.is_err() || !check.as_ref().unwrap().status.success() {
        println!("⚠️  systemd is not available on this system");
        return Ok(());
    }

    println!("🔍 Searching for BioVault daemon services...\n");

    // List all biovault-daemon services
    let output = Command::new("systemctl")
        .args([
            "list-units",
            "--all",
            "--no-pager",
            "biovault-daemon-*.service",
        ])
        .output()?;

    if !output.status.success() {
        println!("⚠️  Failed to list services");
        return Ok(());
    }

    let output_str = String::from_utf8_lossy(&output.stdout);
    let lines: Vec<&str> = output_str.lines().collect();

    // Parse the systemctl output to extract service information
    let mut services = Vec::new();
    for line in lines.iter().skip(1) {
        // Skip header
        if line.contains("biovault-daemon-") && line.contains(".service") {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if let Some(name) = parts.first() {
                if name.starts_with("biovault-daemon-") {
                    // Extract email from service name
                    let email_part = name
                        .trim_start_matches("biovault-daemon-")
                        .trim_end_matches(".service")
                        .replace("-at-", "@")
                        .replace("-", ".");

                    // Get status
                    let status = if line.contains("active") && line.contains("running") {
                        "RUNNING"
                    } else if line.contains("failed") {
                        "FAILED"
                    } else if line.contains("inactive") || line.contains("dead") {
                        "STOPPED"
                    } else {
                        "UNKNOWN"
                    };

                    services.push((name.to_string(), email_part, status));
                }
            }
        }
    }

    if services.is_empty() {
        println!("📝 No BioVault daemon services found");
        println!("   Use 'bv daemon install' to install a service");
    } else {
        println!("📋 Found {} BioVault daemon service(s):\n", services.len());
        for (service_name, email, status) in services {
            let status_icon = match status {
                "RUNNING" => "",
                "FAILED" => "",
                "STOPPED" => "⏹️",
                _ => "",
            };
            println!("   {} {} ({})", status_icon, email, status);
            println!("      Service: {}", service_name);
            println!();
        }
        println!("💡 Commands:");
        println!("   • Status:  sudo systemctl status <service-name>");
        println!("   • Stop:    sudo systemctl stop <service-name>");
        println!("   • Start:   sudo systemctl start <service-name>");
        println!("   • Logs:    sudo journalctl -u <service-name> -f");
    }

    Ok(())
}

// Partially unit testable - non-Linux systems return early
// Full testing requires systemd and installed service (integration tests)
pub async fn service_status(config: &Config) -> Result<()> {
    // First check if daemon is running the old way (manual start)
    let manual_running = is_daemon_running(config).unwrap_or(false);

    if manual_running {
        println!("🤖 Daemon Status: RUNNING (manual mode)");
        if let Some(status) = get_daemon_status(config)? {
            println!("   • PID: {}", status.pid);
            println!(
                "   • Started: {}",
                status.started_at.format("%Y-%m-%d %H:%M:%S UTC")
            );
            if let Some(last_sync) = status.last_sync {
                println!(
                    "   • Last sync: {}",
                    last_sync.format("%Y-%m-%d %H:%M:%S UTC")
                );
            }
            println!("   • Messages processed: {}", status.message_count);
        }
        println!("\n   ℹ️  Note: Daemon was started manually with 'bv daemon start'");
        return Ok(());
    }

    // Check systemd service status (runtime check)
    if cfg!(target_os = "linux") {
        let service_name = get_service_name(config);
        let output = Command::new("systemctl")
            .args(["status", &service_name, "--no-pager"])
            .output()?;

        if output.status.success() || output.status.code() == Some(3) {
            // Parse the output to get status
            let output_str = String::from_utf8_lossy(&output.stdout);

            if output_str.contains("Active: active (running)") {
                println!("🤖 Daemon Status: RUNNING (systemd service)");
            } else if output_str.contains("Active: inactive") || output_str.contains("Active: dead")
            {
                println!("⚠️  Daemon Status: STOPPED");
            } else if output_str.contains("Active: failed") {
                println!("❌ Daemon Status: FAILED");
            } else {
                println!("❓ Daemon Status: UNKNOWN");
            }

            // Print the full systemctl status output
            println!("\n{}", output_str);

            if output_str.contains("could not be found") {
                println!(
                    "\n   ℹ️  Service is not installed. Use 'bv daemon install' to install it."
                );
            }
        } else if output.status.code() == Some(4) {
            println!("❌ Service '{}' not found", service_name);
            println!("   Use 'bv daemon install' to install the service");
        } else {
            println!("⚠️  Could not determine service status");
            println!("   Error: {}", String::from_utf8_lossy(&output.stderr));
        }
    } else {
        println!("⚠️  Daemon Status: NOT RUNNING");
        println!("   Service installation is only supported on Linux");
        println!("   Use 'bv daemon start' to run the daemon manually");
    }

    Ok(())
}

// NOT unit testable - requires systemd
// Test via integration tests on Linux systems only
pub async fn show_service(config: &Config) -> Result<()> {
    check_systemd_available()?;

    let service_name = get_service_name(config);
    let service_path = format!("/etc/systemd/system/{}", service_name);

    if !std::path::Path::new(&service_path).exists() {
        println!("❌ Service file not found: {}", service_path);
        println!("   Use 'bv daemon install' to install the service");
        return Ok(());
    }

    println!("📄 Service file: {}", service_path);
    println!("════════════════════════════════════════");

    let content = std::fs::read_to_string(&service_path)
        .with_context(|| format!("Failed to read service file: {}", service_path))?;

    println!("{}", content);

    Ok(())
}

// NOT unit testable - requires systemd, modifies system services
// Test via integration tests on Linux systems only
pub async fn reinstall_service(config: &Config) -> Result<()> {
    check_systemd_available()?;

    println!("🔄 Reinstalling BioVault daemon service...\n");

    // Check if service exists
    let service_name = get_service_name(config);
    let check_output = Command::new("systemctl")
        .args(["status", &service_name])
        .output()?;

    if check_output.status.success() || check_output.status.code() == Some(3) {
        // Service exists, uninstall it first
        println!("📤 Uninstalling existing service...");
        uninstall_service(config).await?;
        println!();
    }

    // Install the service
    println!("📥 Installing service...");
    install_service(config).await?;

    Ok(())
}

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

    struct TestDirGuard;

    impl TestDirGuard {
        fn new(temp: &TempDir) -> Self {
            crate::config::set_test_syftbox_data_dir(temp.path());
            let home = temp.path().join(".biovault");
            crate::config::set_test_biovault_home(&home);
            TestDirGuard
        }
    }

    impl Drop for TestDirGuard {
        fn drop(&mut self) {
            crate::config::clear_test_biovault_home();
            crate::config::clear_test_syftbox_data_dir();
        }
    }

    // ============================================================================
    // UNIT TESTING GUIDELINES FOR DAEMON MODULE
    // ============================================================================
    //
    // WHAT CAN BE UNIT TESTED:
    // ✅ Helper functions (get_*_path, get_service_name, etc.)
    // ✅ Data structures (DaemonStatus serialization, etc.)
    // ✅ Simple validation logic (check_systemd_available on non-Linux)
    // ✅ Edge cases with mock data (invalid PIDs, missing files)
    //
    // WHAT CANNOT BE UNIT TESTED (requires integration/e2e tests):
    // ❌ Daemon::new() - requires MessageSync, SyftBoxApp, database
    // ❌ Daemon methods (run, sync_messages, etc.) - require running daemon
    // ❌ start() - spawns actual processes
    // ❌ stop() - requires running daemon to stop
    // ❌ Service operations (install/uninstall/etc.) - require systemd
    // ❌ Process checking with real PIDs - depends on actual running processes
    // ❌ File watching and signal handling - require async runtime
    //
    // When adding tests, check if the function:
    // 1. Requires external processes/services → integration test
    // 2. Modifies system state → integration test
    // 3. Is pure logic/data manipulation → unit test
    // ============================================================================

    #[test]
    fn test_daemon_status_new() {
        let pid = 12345;
        let status = DaemonStatus::new(pid);
        assert_eq!(status.pid, pid);
        assert_eq!(status.status, "running");
        assert_eq!(status.message_count, 0);
        assert!(status.last_sync.is_none());
    }

    #[test]
    fn test_daemon_status_serialize() {
        let status = DaemonStatus::new(999);
        let json = serde_json::to_string(&status);
        assert!(json.is_ok());
    }

    #[test]
    fn test_get_service_name() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let name = get_service_name(&config);
        assert!(name.starts_with("biovault-daemon-"));
        assert!(name.contains("test"));
    }

    #[test]
    fn test_get_biovault_dir() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let dir = get_biovault_dir(&config);
        assert!(dir.is_ok());
    }

    #[test]
    fn test_get_pid_file_path() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let path = get_pid_file_path(&config);
        assert!(path.is_ok());
        assert!(path.unwrap().to_string_lossy().contains("daemon.pid"));
    }

    #[test]
    fn test_get_status_file_path() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let path = get_status_file_path(&config);
        assert!(path.is_ok());
        assert!(path.unwrap().to_string_lossy().contains("daemon.status"));
    }

    #[test]
    fn test_get_log_file_path() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let path = get_log_file_path(&config);
        assert!(path.is_ok());
        assert!(path.unwrap().to_string_lossy().contains("daemon.log"));
    }

    #[test]
    fn test_daemon_status_update_fields() {
        let mut status = DaemonStatus::new(999);
        assert_eq!(status.message_count, 0);
        assert!(status.last_sync.is_none());

        status.message_count = 5;
        status.last_sync = Some(Utc::now());

        assert_eq!(status.message_count, 5);
        assert!(status.last_sync.is_some());
    }

    #[test]
    fn test_daemon_status_debug() {
        let status = DaemonStatus::new(123);
        let debug_str = format!("{:?}", status);
        assert!(debug_str.contains("123"));
        assert!(debug_str.contains("running"));
    }

    #[test]
    fn test_daemon_status_deserialize() {
        let json = r#"{
            "pid": 456,
            "started_at": "2024-01-01T00:00:00Z",
            "last_sync": null,
            "message_count": 0,
            "status": "running"
        }"#;
        let status: Result<DaemonStatus, _> = serde_json::from_str(json);
        assert!(status.is_ok());
        let s = status.unwrap();
        assert_eq!(s.pid, 456);
        assert_eq!(s.status, "running");
    }

    #[test]
    #[cfg(unix)]
    fn test_check_process_running_invalid_pid() {
        // PID 999999 is very unlikely to exist
        let result = check_process_running(999999);
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    #[cfg(unix)]
    fn test_check_process_running_current_process() {
        // Current test process should be running
        let pid = std::process::id();
        let result = check_process_running(pid);
        // Function should return a result, even if error
        assert!(result.is_ok() || result.is_err());
    }

    #[test]
    fn test_is_daemon_running_no_pid_file() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = is_daemon_running(&config);
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    fn test_cleanup_stale_pid_files_no_file() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = cleanup_stale_pid_files(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_get_daemon_status_no_file() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = get_daemon_status(&config);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_check_systemd_available() {
        // Just verify it doesn't panic
        let _result = check_systemd_available();
    }

    #[test]
    fn test_generate_systemd_service_content() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = generate_systemd_service_content(&config);
        // May fail due to missing dirs/env, but shouldn't panic
        let _ = result;
    }

    #[test]
    fn test_cleanup_stale_pid_files_with_invalid_pid() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create invalid PID file
        let pid_path = get_pid_file_path(&config).unwrap();
        std::fs::create_dir_all(pid_path.parent().unwrap()).unwrap();
        std::fs::write(&pid_path, "not_a_number").unwrap();

        let result = cleanup_stale_pid_files(&config);
        assert!(result.is_ok());

        // Should have cleaned up the invalid file
        assert!(!pid_path.exists());
    }

    #[test]
    fn test_is_daemon_running_with_invalid_pid() {
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create invalid PID file
        let pid_path = get_pid_file_path(&config).unwrap();
        std::fs::create_dir_all(pid_path.parent().unwrap()).unwrap();
        std::fs::write(&pid_path, "not_a_pid").unwrap();

        let result = is_daemon_running(&config);
        assert!(result.is_ok());
        assert!(!result.unwrap());

        // Should have cleaned up the invalid file
        assert!(!pid_path.exists());
    }

    #[test]
    fn test_get_daemon_status_with_valid_file() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create valid status file
        let status_path = get_status_file_path(&config).unwrap();
        std::fs::create_dir_all(status_path.parent().unwrap()).unwrap();

        let status = DaemonStatus::new(123);
        let json = serde_json::to_string(&status).unwrap();
        std::fs::write(&status_path, json).unwrap();

        let result = get_daemon_status(&config);
        assert!(result.is_ok());
        let loaded_status = result.unwrap();
        assert!(loaded_status.is_some());
        assert_eq!(loaded_status.unwrap().pid, 123);
    }

    #[test]
    fn test_daemon_status_serialization_round_trip() {
        let status = DaemonStatus::new(789);
        let json = serde_json::to_string(&status).unwrap();
        let deserialized: DaemonStatus = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.pid, 789);
        assert_eq!(deserialized.status, "running");
        assert_eq!(deserialized.message_count, 0);
    }

    #[test]
    fn test_get_service_name_sanitizes_email() {
        let config = Config {
            email: "user@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };
        let name = get_service_name(&config);
        assert!(name.contains("user-at-example-com"));
        assert!(!name.contains('@'));
        assert!(!name.contains('.') || name.ends_with(".service"));
    }

    #[test]
    fn test_daemon_status_with_last_sync() {
        let mut status = DaemonStatus::new(555);
        let now = Utc::now();
        status.last_sync = Some(now);
        status.message_count = 10;

        let json = serde_json::to_string(&status).unwrap();
        let deserialized: DaemonStatus = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.message_count, 10);
        assert!(deserialized.last_sync.is_some());
    }

    #[test]
    fn test_path_helpers_consistency() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let biovault_dir = get_biovault_dir(&config).unwrap();
        let pid_path = get_pid_file_path(&config).unwrap();
        let status_path = get_status_file_path(&config).unwrap();
        let log_path = get_log_file_path(&config).unwrap();

        // All paths should be under biovault_dir
        assert!(pid_path.starts_with(&biovault_dir));
        assert!(status_path.starts_with(&biovault_dir));
        assert!(log_path.starts_with(&biovault_dir));
    }

    #[test]
    #[cfg(unix)]
    fn test_check_process_running_pid_1() {
        // PID 1 is init/systemd, should exist but won't be a bv process
        let result = check_process_running(1);
        assert!(result.is_ok());
        // Will return false because PID 1 is not a bv/biovault process
        assert!(!result.unwrap());
    }

    #[test]
    fn test_check_systemd_available_on_non_linux() {
        #[cfg(not(target_os = "linux"))]
        {
            let result = check_systemd_available();
            assert!(result.is_err());
        }
    }

    #[tokio::test]
    async fn test_stop_daemon_not_running() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = stop(&config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_logs_no_file() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = logs(&config, false, Some(10)).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_daemon_status_display() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Should work even when daemon not running
        let status_opt = get_daemon_status(&config);
        assert!(status_opt.is_ok());
    }

    #[tokio::test]
    #[cfg(not(target_os = "linux"))]
    async fn test_install_service_non_linux() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = install_service(&config).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    #[cfg(not(target_os = "linux"))]
    async fn test_uninstall_service_non_linux() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = uninstall_service(&config).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    #[cfg(not(target_os = "linux"))]
    async fn test_service_status_non_linux() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = service_status(&config).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    #[cfg(not(target_os = "linux"))]
    async fn test_show_service_non_linux() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = show_service(&config).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    #[cfg(not(target_os = "linux"))]
    async fn test_reinstall_service_non_linux() {
        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let result = reinstall_service(&config).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_generate_systemd_service_content_fields() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        if let Ok(content) = generate_systemd_service_content(&config) {
            assert!(content.contains("[Unit]"));
            assert!(content.contains("[Service]"));
            assert!(content.contains("[Install]"));
            assert!(content.contains("test@example.com") || content.contains("test-at-example"));
        }
    }

    #[test]
    fn test_daemon_status_fields_modification() {
        let mut status = DaemonStatus::new(444);

        // Test initial state
        assert_eq!(status.pid, 444);
        assert_eq!(status.message_count, 0);
        assert!(status.last_sync.is_none());
        assert_eq!(status.status, "running");

        // Modify fields
        status.message_count = 15;
        status.status = "syncing".to_string();
        let now = Utc::now();
        status.last_sync = Some(now);

        // Verify modifications
        assert_eq!(status.message_count, 15);
        assert_eq!(status.status, "syncing");
        assert!(status.last_sync.is_some());
    }

    #[test]
    fn test_get_daemon_status_invalid_json() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);
        let bv_home = tmp.path().join("bv_home");
        crate::config::set_test_biovault_home(&bv_home);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create invalid JSON status file
        let status_path = get_status_file_path(&config).unwrap();
        std::fs::create_dir_all(status_path.parent().unwrap()).unwrap();
        std::fs::write(&status_path, "not valid json").unwrap();

        let result = get_daemon_status(&config);
        assert!(result.is_err());
    }

    #[test]
    fn test_cleanup_stale_pid_files_with_stale_process() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create PID file with non-existent PID
        let pid_path = get_pid_file_path(&config).unwrap();
        std::fs::create_dir_all(pid_path.parent().unwrap()).unwrap();
        std::fs::write(&pid_path, "999999").unwrap();

        let result = cleanup_stale_pid_files(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_is_daemon_running_with_stale_pid() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create PID file with non-existent PID
        let pid_path = get_pid_file_path(&config).unwrap();
        std::fs::create_dir_all(pid_path.parent().unwrap()).unwrap();
        std::fs::write(&pid_path, "999999").unwrap();

        let result = is_daemon_running(&config);
        assert!(result.is_ok());
        assert!(!result.unwrap());
    }

    #[test]
    #[cfg(unix)]
    fn test_check_process_running_edge_cases() {
        // Test with PID 0 (invalid) - may return error or false
        let result = check_process_running(0);
        let _ = result; // Just ensure it doesn't panic

        // Test with max PID - may return error or false
        let result = check_process_running(99999);
        let _ = result; // Just ensure it doesn't panic
    }

    #[test]
    fn test_service_name_multiple_emails() {
        let configs = vec![
            ("user@example.com", "user-at-example-com"),
            ("test.user@domain.org", "test-user-at-domain-org"),
            ("name+tag@email.com", "name+tag-at-email-com"),
        ];

        for (email, expected_part) in configs {
            let config = Config {
                email: email.to_string(),
                syftbox_config: None,
                version: None,
                binary_paths: None,
                syftbox_credentials: None,
            };
            let name = get_service_name(&config);
            assert!(
                name.contains(expected_part),
                "Service name '{}' doesn't contain '{}'",
                name,
                expected_part
            );
            assert!(name.starts_with("biovault-daemon-"));
            assert!(name.ends_with(".service"));
        }
    }

    #[test]
    fn test_path_helpers_different_emails() {
        let tmp = tempfile::TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config1 = Config {
            email: "user1@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let config2 = Config {
            email: "user2@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let dir1 = get_biovault_dir(&config1).unwrap();
        let dir2 = get_biovault_dir(&config2).unwrap();

        // Both should get same biovault dir (shared)
        assert_eq!(dir1, dir2);
    }

    // NOTE: test_start_already_running removed - start() requires full daemon setup
    // including MessageSync and SyftBox, making it unsuitable for unit tests.
    // This should be tested via integration/e2e tests instead.

    #[test]
    fn test_cleanup_stale_pid_files_preserves_running_daemon() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Create PID file with current process ID (which is running)
        let pid_path = get_pid_file_path(&config).unwrap();
        std::fs::create_dir_all(pid_path.parent().unwrap()).unwrap();
        std::fs::write(&pid_path, std::process::id().to_string()).unwrap();

        let result = cleanup_stale_pid_files(&config);
        assert!(result.is_ok());

        // Clean up
        let _ = std::fs::remove_file(&pid_path);
    }

    #[test]
    fn test_daemon_status_json_format() {
        let status = DaemonStatus::new(12345);
        let json = serde_json::to_string_pretty(&status).unwrap();

        assert!(json.contains("\"pid\": 12345"));
        assert!(json.contains("\"status\": \"running\""));
        assert!(json.contains("\"message_count\": 0"));
        assert!(json.contains("\"started_at\""));
    }

    #[test]
    fn test_get_pid_file_path_creates_parent() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);
        let bv_home = tmp.path().join("bv_home");
        crate::config::set_test_biovault_home(&bv_home);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let pid_path = get_pid_file_path(&config).unwrap();
        assert!(pid_path.parent().is_some());
        assert!(pid_path.starts_with(&bv_home));
    }

    #[test]
    fn test_get_log_file_path_structure() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let log_path = get_log_file_path(&config).unwrap();
        assert!(log_path.to_string_lossy().contains("logs"));
        assert!(log_path.to_string_lossy().contains("daemon.log"));
    }

    #[tokio::test]
    async fn test_logs_with_lines_parameter() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        // Test with different line counts
        let result1 = logs(&config, false, Some(10)).await;
        assert!(result1.is_ok());

        let result2 = logs(&config, false, Some(100)).await;
        assert!(result2.is_ok());

        let result3 = logs(&config, false, None).await;
        assert!(result3.is_ok());
    }

    // NOTE: test_is_daemon_running_with_valid_current_pid removed
    // is_daemon_running() calls check_process_running() which uses ps/tasklist
    // and requires the process to match "bv" or "biovault" in the command line.
    // Test processes don't match this pattern, making this unsuitable for unit tests.
    // Covered by test_is_daemon_running_no_pid_file and test_is_daemon_running_with_stale_pid instead.

    #[test]
    fn test_get_status_file_path_uniqueness() {
        use tempfile::TempDir;
        let tmp = TempDir::new().unwrap();
        let _guard = TestDirGuard::new(&tmp);

        let config = Config {
            email: "test@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let status_path = get_status_file_path(&config).unwrap();
        let pid_path = get_pid_file_path(&config).unwrap();

        // Should be different files
        assert_ne!(status_path, pid_path);
        // But in same directory
        assert_eq!(status_path.parent(), pid_path.parent());
    }

    #[test]
    fn test_daemon_status_started_at_is_recent() {
        let before = Utc::now();
        let status = DaemonStatus::new(999);
        let after = Utc::now();

        assert!(status.started_at >= before);
        assert!(status.started_at <= after);
    }

    #[test]
    fn test_service_name_no_special_chars() {
        let config = Config {
            email: "user@example.com".to_string(),
            syftbox_config: None,
            version: None,
            binary_paths: None,
            syftbox_credentials: None,
        };

        let name = get_service_name(&config);

        // Should not contain @ or . (except in .service extension)
        let name_without_ext = name.strip_suffix(".service").unwrap();
        assert!(!name_without_ext.contains('@'));
        assert!(!name_without_ext.contains('.'));
    }

    #[test]
    #[cfg(unix)]
    fn test_check_process_running_returns_bool() {
        // Just verify it returns a bool without panicking
        let result1 = check_process_running(1);
        assert!(result1.is_ok());
        // Result is a bool, no need to assert it's true or false

        let result2 = check_process_running(999999);
        assert!(result2.is_ok());
        // Result is a bool, function completes without panic
    }
}