fnox 1.23.0

A flexible secret management tool supporting multiple providers and encryption methods
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
use crate::env;
use crate::error::{FnoxError, Result};
use crate::settings::Settings;
use crate::source_registry;
use crate::spanned::SpannedValue;
use clap::ValueEnum;
use indexmap::IndexMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use strum::VariantNames;

/// Default config filename, used as the clap default for `--config`.
pub const DEFAULT_CONFIG_FILENAME: &str = "fnox.toml";

/// Returns all config filenames in load order (first = lowest priority, last = highest priority).
///
/// Order: main configs → profile configs → local configs
/// Within each group, non-dotfiles come first (lower priority); dotfiles follow (higher priority).
pub fn all_config_filenames(profile: Option<&str>) -> Vec<String> {
    let mut files = vec![
        DEFAULT_CONFIG_FILENAME.to_string(),
        ".fnox.toml".to_string(),
    ];
    if let Some(p) = profile.filter(|p| *p != "default") {
        files.push(format!("fnox.{p}.toml"));
        files.push(format!(".fnox.{p}.toml"));
    }
    files.push("fnox.local.toml".to_string());
    files.push(".fnox.local.toml".to_string());
    files
}

/// Returns the local override filename for a supported config basename.
///
/// Only `fnox.toml` and `.fnox.toml` have corresponding local override files.
pub fn local_override_filename(path: &Path) -> Option<&'static str> {
    match path.file_name().and_then(|name| name.to_str()) {
        Some("fnox.toml") => Some("fnox.local.toml"),
        Some(".fnox.toml") => Some(".fnox.local.toml"),
        _ => None,
    }
}

/// Find the most appropriate existing config file in `dir` for writing.
///
/// When a non-default profile is active, prefers the profile-specific file
/// (e.g. `fnox.staging.toml`) if it exists, so secrets stay scoped to that
/// profile. Otherwise falls back to the lowest-priority existing file.
/// If no config files exist yet, returns `fnox.toml`.
pub fn find_local_config(dir: &Path, profile: Option<&str>) -> PathBuf {
    // If a non-default profile is specified, prefer its config file first
    if let Some(p) = profile.filter(|p| *p != "default") {
        for name in [format!("fnox.{p}.toml"), format!(".fnox.{p}.toml")] {
            let path = dir.join(&name);
            if path.exists() {
                return path;
            }
        }
    }

    // Fall back to lowest-priority existing base file.
    // When a profile is active, exclude local files (fnox.local.toml, .fnox.local.toml)
    // to avoid silently routing profile-scoped secrets into a gitignored local-override file.
    let is_profiled = profile.is_some_and(|p| p != "default");
    for name in &["fnox.toml", ".fnox.toml"] {
        let path = dir.join(name);
        if path.exists() {
            return path;
        }
    }
    if !is_profiled {
        for name in &["fnox.local.toml", ".fnox.local.toml"] {
            let path = dir.join(name);
            if path.exists() {
                return path;
            }
        }
    }
    dir.join(DEFAULT_CONFIG_FILENAME)
}

// Re-export ProviderConfig from providers module
pub use crate::providers::ProviderConfig;

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Config {
    /// Import paths to other config files
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub import: Vec<String>,

    /// Root configuration - stops recursion at this level
    #[serde(default, skip_serializing_if = "is_false")]
    pub root: bool,

    /// Lease backend configurations (for default profile)
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub leases: IndexMap<String, crate::lease_backends::LeaseBackendConfig>,

    /// Provider configurations (for default profile)
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub providers: IndexMap<String, ProviderConfig>,

    /// Default provider name for default profile
    #[serde(default, skip_serializing_if = "Option::is_none")]
    default_provider: Option<SpannedValue<String>>,

    /// Default profile secrets (top level)
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub secrets: IndexMap<String, SecretConfig>,

    /// Named profiles
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub profiles: IndexMap<String, ProfileConfig>,

    /// Age encryption key file path (optional, can also be set via env var or CLI flag)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub age_key_file: Option<PathBuf>,

    /// Default if_missing behavior for all secrets in this config
    #[serde(skip_serializing_if = "Option::is_none")]
    pub if_missing: Option<IfMissing>,

    /// Whether to prompt for authentication when provider auth fails (default: true in TTY)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_auth: Option<bool>,

    /// MCP server configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mcp: Option<McpConfig>,

    /// Track which config file each provider came from (not serialized)
    #[serde(skip)]
    pub provider_sources: HashMap<String, PathBuf>,

    /// Track which config file each secret came from (not serialized)
    #[serde(skip)]
    pub secret_sources: HashMap<String, PathBuf>,

    /// Track which config file the default_provider came from (not serialized)
    #[serde(skip)]
    pub default_provider_source: Option<PathBuf>,

    /// The project root directory — the nearest directory to cwd that contains
    /// a config file. Used for scoping the lease ledger per-project.
    #[serde(skip)]
    pub project_dir: Option<PathBuf>,
}

/// Cached sync data for a secret (provider + encrypted value)
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct SyncConfig {
    pub provider: String,
    pub value: String,
}

/// Configuration for a single secret
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SecretConfig {
    /// Description of the secret
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// What to do if the secret is missing (error, warn, or ignore)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub if_missing: Option<IfMissing>,

    /// Default value to use if provider fails or secret is not found
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,

    /// Provider to fetch from (age, aws-kms, 1password, aws, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    provider: Option<SpannedValue<String>>,

    /// Value for the provider (secret name, encrypted blob, etc.)
    #[serde(skip_serializing_if = "Option::is_none")]
    value: Option<SpannedValue<String>>,

    /// Whether to inject this secret into env vars (default: true)
    /// When false, the secret is only accessible via `fnox get`
    #[serde(default = "default_true", skip_serializing_if = "is_true")]
    pub env: bool,

    /// Write secret to a temporary file and set env var to the file path instead of the secret value
    #[serde(default, skip_serializing_if = "is_false")]
    pub as_file: bool,
    /// JSON path to extract from the secret value (supports dot notation: "nested.key")
    /// When set, the secret value is parsed as JSON and the specified path is extracted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub json_path: Option<String>,

    /// 1-indexed line number to extract from the secret value.
    /// When set, the secret value is split on newlines and the Nth line is returned.
    /// Useful for providers whose entries pack multiple related values into a
    /// single secret (e.g. one value per line). Mutually exclusive with `json_path`.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(range(min = 1))]
    pub line: Option<usize>,

    /// Cached sync data (provider + encrypted value from `fnox sync`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sync: Option<SyncConfig>,

    /// Path to the config file where this secret was defined (not serialized)
    #[serde(skip)]
    pub source_path: Option<PathBuf>,

    /// Whether this secret was loaded from a [profiles.X.secrets] section (not serialized).
    /// When false, the secret was loaded from a root-level [secrets] section.
    #[serde(skip)]
    pub source_is_profile: bool,
}

/// Configuration for a profile
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ProfileConfig {
    /// Lease backend configurations for this profile
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub leases: IndexMap<String, crate::lease_backends::LeaseBackendConfig>,

    /// Provider configurations for this profile
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub providers: IndexMap<String, ProviderConfig>,

    /// Default provider name for this profile
    #[serde(default, skip_serializing_if = "Option::is_none")]
    default_provider: Option<SpannedValue<String>>,

    /// Secrets for this profile
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub secrets: IndexMap<String, SecretConfig>,

    /// Track which config file each provider came from (not serialized)
    #[serde(skip)]
    pub provider_sources: HashMap<String, PathBuf>,

    /// Track which config file each secret came from (not serialized)
    #[serde(skip)]
    pub secret_sources: HashMap<String, PathBuf>,

    /// Track which config file the default_provider came from (not serialized)
    #[serde(skip)]
    pub default_provider_source: Option<PathBuf>,
}

/// Available MCP tools
#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum McpTool {
    GetSecret,
    Exec,
}

impl McpTool {
    /// Returns the tool name as it appears in MCP protocol
    pub fn tool_name(&self) -> &'static str {
        match self {
            McpTool::GetSecret => "get_secret",
            McpTool::Exec => "exec",
        }
    }
}

/// MCP server configuration
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[derive(Default)]
pub struct McpConfig {
    /// Which MCP tools to expose (default: ["get_secret", "exec"])
    #[serde(default, skip_serializing_if = "Option::is_none", rename = "tools")]
    tools_raw: Option<Vec<McpTool>>,

    /// Timeout in seconds for exec tool subprocess (default: 300, minimum: 1)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[schemars(range(min = 1))]
    pub exec_timeout_secs: Option<u64>,

    /// Whether to redact secret values from exec tool output (default: true).
    /// When enabled, resolved secret values are replaced with [REDACTED] in
    /// stdout/stderr before returning to the agent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub redact_output: Option<bool>,

    /// Optional allowlist of secret names visible to the MCP server.
    /// When set, only these secrets are resolved and available via get_secret/exec.
    /// When None, all profile secrets are available.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secrets: Option<Vec<String>>,
}

impl McpConfig {
    fn default_tools() -> Vec<McpTool> {
        vec![McpTool::GetSecret, McpTool::Exec]
    }

    /// Whether `tools` was explicitly set in the config file
    pub fn tools_explicitly_set(&self) -> bool {
        self.tools_raw.is_some()
    }

    /// Returns the effective tools list (default if not explicitly set)
    pub fn tools(&self) -> Vec<McpTool> {
        self.tools_raw.clone().unwrap_or_else(Self::default_tools)
    }

    /// Set the tools list explicitly
    pub fn set_tools(&mut self, tools: Vec<McpTool>) {
        self.tools_raw = Some(tools);
    }

    /// Whether exec output redaction is enabled (default: true)
    pub fn redact_output(&self) -> bool {
        self.redact_output.unwrap_or(true)
    }

    /// Filter a secrets map to only include allowed secrets.
    /// Returns the map unchanged if no allowlist is set.
    pub fn filter_secrets(
        &self,
        secrets: IndexMap<String, SecretConfig>,
    ) -> IndexMap<String, SecretConfig> {
        match &self.secrets {
            None => secrets,
            Some(allowlist) => {
                let allowed: std::collections::HashSet<&str> =
                    allowlist.iter().map(|s| s.as_str()).collect();
                secrets
                    .into_iter()
                    .filter(|(k, _)| allowed.contains(k.as_str()))
                    .collect()
            }
        }
    }
}

#[derive(
    Debug, Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq, Eq, ValueEnum, VariantNames,
)]
#[serde(rename_all = "lowercase")]
pub enum IfMissing {
    Error,
    Warn,
    Ignore,
}

impl Config {
    /// Load configuration using the appropriate strategy
    pub fn load_smart<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path_ref = path.as_ref();

        // If the path is one of the default config filenames, use recursive loading
        let default_filenames = all_config_filenames(None);
        if default_filenames.iter().any(|f| path_ref == Path::new(f)) {
            Self::load_with_recursion(path_ref)
        } else {
            // For explicit paths, resolve relative paths against current directory first
            let resolved_path = if path_ref.is_relative() {
                env::current_dir()
                    .map_err(|e| {
                        FnoxError::Config(format!("Failed to get current directory: {}", e))
                    })?
                    .join(path_ref)
            } else {
                path_ref.to_path_buf()
            };
            // For explicit paths, use direct loading
            Self::load(resolved_path)
        }
    }

    /// Load configuration from a file
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        use miette::{NamedSource, SourceSpan};

        let path = path.as_ref();
        let content = fs::read_to_string(path).map_err(|source| FnoxError::ConfigReadFailed {
            path: path.to_path_buf(),
            source,
        })?;

        // Register the source for error reporting
        source_registry::register(path, content.clone());

        let mut config: Config = toml_edit::de::from_str(&content).map_err(|e| {
            // Try to create a source-aware error with span highlighting
            if let Some(span) = e.span() {
                FnoxError::ConfigParseErrorWithSource {
                    message: e.message().to_string(),
                    src: Arc::new(NamedSource::new(
                        path.display().to_string(),
                        Arc::new(content),
                    )),
                    span: SourceSpan::new(span.start.into(), span.end - span.start),
                }
            } else {
                // Fall back to the basic error if no span available
                FnoxError::ConfigParseError { source: e }
            }
        })?;

        // Set source paths for all secrets and providers
        config.set_source_paths(path);

        Ok(config)
    }

    /// Load configuration with recursive directory search and merging
    fn load_with_recursion<P: AsRef<Path>>(_start_path: P) -> Result<Self> {
        // Start from current working directory and search upwards
        let current_dir = env::current_dir()
            .map_err(|e| FnoxError::Config(format!("Failed to get current directory: {}", e)))?;

        match Self::load_recursive(&current_dir, false) {
            Ok((_config, found)) if !found => {
                // No config file was found anywhere in the directory tree
                Err(FnoxError::ConfigNotFound {
                    message: format!(
                        "No configuration file found in {} or any parent directory",
                        current_dir.display()
                    ),
                    help: "Run 'fnox init' to create a configuration file".to_string(),
                })
            }
            Ok((mut config, _)) => {
                // Find the nearest directory to cwd that contains a config file.
                // This is the project root used for scoping the lease ledger.
                config.project_dir = Self::find_project_dir(&current_dir);
                Ok(config)
            }
            Err(e) => Err(e),
        }
    }

    /// Recursively search for fnox.toml files and merge them
    /// Returns (config, found_any) where found_any indicates if any config file was found
    fn load_recursive(dir: &Path, found_any: bool) -> Result<(Self, bool)> {
        // Get current profile from Settings (respects: CLI flag > Env var > Default)
        let profile = crate::settings::Settings::get().profile.clone();
        let filenames = all_config_filenames(Some(&profile));

        // Load all existing config files in order (later files override earlier ones)
        let mut config = Self::new();
        let mut found = found_any;

        for filename in &filenames {
            let path = dir.join(filename);
            if path.exists() {
                let file_config = Self::load(&path)?;
                config = Self::merge_configs(config, file_config)?;
                found = true;
            }
        }

        // If this config marks root, stop recursion but still load global config
        if config.root {
            // Load imports if any
            for import_path in &config.import.clone() {
                let import_config = Self::load_import(import_path, dir)?;
                config = Self::merge_configs(import_config, config)?;
            }
            // Load global config as the base even for root configs
            let (global_config, global_found) = Self::load_global()?;
            if global_found {
                config = Self::merge_configs(global_config, config)?;
                found = true;
            }
            return Ok((config, found));
        }

        // Load imports first (they get overridden by local config)
        for import_path in &config.import.clone() {
            let import_config = Self::load_import(import_path, dir)?;
            config = Self::merge_configs(import_config, config)?;
        }

        // If we have a parent directory, recurse up and merge
        if let Some(parent_dir) = dir.parent() {
            let (parent_config, parent_found) = Self::load_recursive(parent_dir, found)?;
            config = Self::merge_configs(parent_config, config)?;
            found = found || parent_found;
        } else {
            // At the filesystem root, try to load global config as base
            let (global_config, global_found) = Self::load_global()?;
            if global_found {
                config = Self::merge_configs(global_config, config)?;
                found = true;
            }
        }

        Ok((config, found))
    }

    /// Find the nearest directory to `start` that contains a config file.
    /// Walks upward from `start` and returns the first match.
    fn find_project_dir(start: &Path) -> Option<PathBuf> {
        let profile = crate::settings::Settings::get().profile.clone();
        let filenames = all_config_filenames(Some(&profile));
        let mut dir = Some(start);
        while let Some(d) = dir {
            for filename in &filenames {
                if d.join(filename).exists() {
                    return Some(d.to_path_buf());
                }
            }
            dir = d.parent();
        }
        None
    }

    /// Get the path to the global config file
    pub fn global_config_path() -> PathBuf {
        env::FNOX_CONFIG_DIR.join("config.toml")
    }

    /// Load global configuration from FNOX_CONFIG_DIR/config.toml
    /// This is the lowest priority config, overridden by all project-level configs
    fn load_global() -> Result<(Self, bool)> {
        let global_config_path = Self::global_config_path();

        if global_config_path.exists() {
            tracing::debug!(
                "Loading global config from {}",
                global_config_path.display()
            );
            let config = Self::load(&global_config_path)?;
            Ok((config, true))
        } else {
            Ok((Self::new(), false))
        }
    }

    /// Load an imported config file
    fn load_import(import_path: &str, base_dir: &Path) -> Result<Self> {
        let path = PathBuf::from(import_path);

        // Handle relative paths - they're relative to the base config's directory
        let absolute_path = if path.is_absolute() {
            path
        } else {
            base_dir.join(path)
        };

        if !absolute_path.exists() {
            return Err(FnoxError::Config(format!(
                "Import file not found: {}",
                absolute_path.display()
            )));
        }

        Self::load(&absolute_path)
    }

    /// Merge two configs, with second config taking precedence
    fn merge_configs(base: Config, overlay: Config) -> Result<Config> {
        let mut merged = base;

        // Merge imports (overlay takes precedence, but keep unique paths)
        for import_path in overlay.import {
            if !merged.import.contains(&import_path) {
                merged.import.push(import_path);
            }
        }

        // root flag: if either is true, result is true
        merged.root = merged.root || overlay.root;

        // Merge age_key_file (overlay takes precedence)
        if overlay.age_key_file.is_some() {
            merged.age_key_file = overlay.age_key_file;
        }

        // Merge if_missing (overlay takes precedence)
        if overlay.if_missing.is_some() {
            merged.if_missing = overlay.if_missing;
        }

        // Merge prompt_auth (overlay takes precedence)
        if overlay.prompt_auth.is_some() {
            merged.prompt_auth = overlay.prompt_auth;
        }

        // Merge mcp (overlay takes precedence, field-by-field to avoid
        // silently re-enabling tools when overlay only sets exec_timeout_secs)
        if let Some(overlay_mcp) = overlay.mcp {
            let base_mcp = merged.mcp.get_or_insert_with(McpConfig::default);
            if overlay_mcp.tools_explicitly_set() {
                base_mcp.set_tools(overlay_mcp.tools());
            }
            if overlay_mcp.exec_timeout_secs.is_some() {
                base_mcp.exec_timeout_secs = overlay_mcp.exec_timeout_secs;
            }
            if overlay_mcp.redact_output.is_some() {
                base_mcp.redact_output = overlay_mcp.redact_output;
            }
            // Replace entirely — a partial overlay should not silently
            // re-expose secrets that the base config restricted.
            if overlay_mcp.secrets.is_some() {
                base_mcp.secrets = overlay_mcp.secrets;
            }
        }

        // Merge default_provider and its source (overlay takes precedence)
        if overlay.default_provider.is_some() {
            merged.default_provider = overlay.default_provider;
            merged.default_provider_source = overlay.default_provider_source;
        }

        // Merge lease backends (overlay takes precedence)
        for (name, lease) in overlay.leases {
            merged.leases.insert(name, lease);
        }

        // Merge providers (overlay takes precedence)
        for (name, provider) in overlay.providers {
            merged.providers.insert(name, provider);
        }

        // Merge provider sources (overlay takes precedence)
        for (name, source) in overlay.provider_sources {
            merged.provider_sources.insert(name, source);
        }

        // Merge secrets (overlay takes precedence)
        for (name, secret) in overlay.secrets {
            merged.secrets.insert(name, secret);
        }

        // Merge secret sources (overlay takes precedence)
        for (name, source) in overlay.secret_sources {
            merged.secret_sources.insert(name, source);
        }

        // Merge profiles (overlay takes precedence)
        for (name, profile) in overlay.profiles {
            if let Some(existing_profile) = merged.profiles.get_mut(&name) {
                // Merge existing profile
                for (lease_name, lease) in profile.leases {
                    existing_profile.leases.insert(lease_name, lease);
                }
                for (provider_name, provider) in profile.providers {
                    existing_profile.providers.insert(provider_name, provider);
                }
                for (provider_name, source) in &profile.provider_sources {
                    existing_profile
                        .provider_sources
                        .insert(provider_name.clone(), source.clone());
                }
                for (secret_name, secret) in profile.secrets {
                    existing_profile.secrets.insert(secret_name, secret);
                }
                for (secret_name, source) in &profile.secret_sources {
                    existing_profile
                        .secret_sources
                        .insert(secret_name.clone(), source.clone());
                }
                // Merge default_provider and its source (overlay takes precedence)
                if profile.default_provider.is_some() {
                    existing_profile.default_provider = profile.default_provider;
                    existing_profile.default_provider_source = profile.default_provider_source;
                }
            } else {
                merged.profiles.insert(name, profile);
            }
        }

        Ok(merged)
    }

    /// Save configuration to a file
    /// Uses toml_edit to preserve insertion order from IndexMap
    /// and format secrets as inline tables
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        // Clone and clean up empty profiles before saving
        let mut clean_config = self.clone();
        clean_config
            .profiles
            .retain(|_, profile| !profile.is_empty());

        // First serialize with to_string_pretty to get proper structure
        let pretty_string = toml_edit::ser::to_string_pretty(&clean_config)?;

        // Parse it back as a document so we can modify it
        let mut doc = pretty_string
            .parse::<toml_edit::DocumentMut>()
            .map_err(|e| FnoxError::Config(format!("Failed to parse TOML: {}", e)))?;

        // Convert secrets to inline tables
        Self::convert_secrets_to_inline(&mut doc)?;

        fs::write(path.as_ref(), doc.to_string()).map_err(|source| {
            FnoxError::ConfigWriteFailed {
                path: path.as_ref().to_path_buf(),
                source,
            }
        })?;
        Ok(())
    }

    /// Convert all tables in [secrets] and [profiles.*.secrets] to inline tables
    fn convert_secrets_to_inline(doc: &mut toml_edit::DocumentMut) -> Result<()> {
        use toml_edit::{InlineTable, Item};

        // Convert top-level [secrets]
        if let Some(secrets_item) = doc.get_mut("secrets")
            && let Some(secrets_table) = secrets_item.as_table_mut()
        {
            let keys: Vec<String> = secrets_table.iter().map(|(k, _)| k.to_string()).collect();
            for key in keys {
                if let Some(item) = secrets_table.get_mut(&key)
                    && let Some(table) = item.as_table()
                {
                    let mut inline = InlineTable::new();
                    for (k, v) in table.iter() {
                        if let Some(value) = v.as_value() {
                            inline.insert(k, value.clone());
                        }
                    }
                    inline.fmt();
                    *item = Item::Value(toml_edit::Value::InlineTable(inline));
                }
            }
        }

        // Convert [profiles.*.secrets]
        if let Some(profiles_item) = doc.get_mut("profiles")
            && let Some(profiles_table) = profiles_item.as_table_mut()
        {
            let profile_names: Vec<String> =
                profiles_table.iter().map(|(k, _)| k.to_string()).collect();
            for profile_name in profile_names {
                if let Some(profile_item) = profiles_table.get_mut(&profile_name)
                    && let Some(profile_table) = profile_item.as_table_mut()
                    && let Some(secrets_item) = profile_table.get_mut("secrets")
                    && let Some(secrets_table) = secrets_item.as_table_mut()
                {
                    let keys: Vec<String> =
                        secrets_table.iter().map(|(k, _)| k.to_string()).collect();
                    for key in keys {
                        if let Some(item) = secrets_table.get_mut(&key)
                            && let Some(table) = item.as_table()
                        {
                            let mut inline = InlineTable::new();
                            for (k, v) in table.iter() {
                                if let Some(value) = v.as_value() {
                                    inline.insert(k, value.clone());
                                }
                            }
                            inline.fmt();
                            *item = Item::Value(toml_edit::Value::InlineTable(inline));
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Save a single secret update back to its source file
    /// Always saves to the default_target (local config file), creating a local
    /// override if the secret exists in a parent config. This aligns with the
    /// hierarchical config model where child configs override parent configs.
    ///
    /// This method preserves comments and formatting in the TOML file by
    /// directly manipulating the document AST rather than re-serializing.
    pub fn save_secret_to_source(
        &self,
        secret_name: &str,
        secret_config: &SecretConfig,
        profile: &str,
        default_target: &Path,
    ) -> Result<()> {
        use toml_edit::{DocumentMut, Item, Value};

        let target_file = default_target.to_path_buf();

        // Load existing document or create new one (preserves comments)
        let mut doc = if target_file.exists() {
            let content =
                fs::read_to_string(&target_file).map_err(|source| FnoxError::ConfigReadFailed {
                    path: target_file.clone(),
                    source,
                })?;
            content.parse::<DocumentMut>().map_err(|e| {
                FnoxError::Config(format!(
                    "Failed to parse TOML in {}: {}",
                    target_file.display(),
                    e
                ))
            })?
        } else {
            DocumentMut::new()
        };

        // Get or create the secrets table
        let secrets_table = if profile == "default" {
            if doc.get("secrets").is_none() {
                doc["secrets"] = Item::Table(toml_edit::Table::new());
            }
            doc["secrets"].as_table_mut().unwrap()
        } else {
            if doc.get("profiles").is_none() {
                doc["profiles"] = Item::Table(toml_edit::Table::new());
            }
            let profiles = doc["profiles"].as_table_mut().unwrap();
            if profiles.get(profile).is_none() {
                profiles[profile] = Item::Table(toml_edit::Table::new());
            }
            let profile_table = profiles[profile].as_table_mut().unwrap();
            if profile_table.get("secrets").is_none() {
                profile_table["secrets"] = Item::Table(toml_edit::Table::new());
            }
            profile_table["secrets"].as_table_mut().unwrap()
        };

        // Update/insert the secret as inline table
        let inline = secret_config.to_inline_table();
        secrets_table[secret_name] = Item::Value(Value::InlineTable(inline));

        // Remove trailing space from key to match format: KEY= { ... } instead of KEY = { ... }
        if let Some(mut key) = secrets_table.key_mut(secret_name) {
            key.leaf_decor_mut().set_suffix("");
        }

        // Write back (preserves all comments and formatting)
        fs::write(&target_file, doc.to_string()).map_err(|source| {
            FnoxError::ConfigWriteFailed {
                path: target_file,
                source,
            }
        })?;

        Ok(())
    }

    /// Remove a single secret from a config file, preserving comments and formatting.
    ///
    /// This method directly manipulates the TOML document AST rather than
    /// re-serializing, so all comments, whitespace, and formatting are preserved.
    pub fn remove_secret_from_source(
        secret_name: &str,
        profile: &str,
        target_file: &Path,
    ) -> Result<bool> {
        use toml_edit::DocumentMut;

        let content =
            fs::read_to_string(target_file).map_err(|source| FnoxError::ConfigReadFailed {
                path: target_file.to_path_buf(),
                source,
            })?;
        let mut doc = content.parse::<DocumentMut>().map_err(|e| {
            FnoxError::Config(format!(
                "Failed to parse TOML in {}: {}",
                target_file.display(),
                e
            ))
        })?;

        // Navigate to the secrets table
        let removed = if profile == "default" {
            doc.get_mut("secrets")
                .and_then(|s| s.as_table_mut())
                .map(|t| t.remove(secret_name).is_some())
                .unwrap_or(false)
        } else {
            doc.get_mut("profiles")
                .and_then(|p| p.as_table_mut())
                .and_then(|p| p.get_mut(profile))
                .and_then(|p| p.as_table_mut())
                .and_then(|p| p.get_mut("secrets"))
                .and_then(|s| s.as_table_mut())
                .map(|t| t.remove(secret_name).is_some())
                .unwrap_or(false)
        };

        if removed {
            fs::write(target_file, doc.to_string()).map_err(|source| {
                FnoxError::ConfigWriteFailed {
                    path: target_file.to_path_buf(),
                    source,
                }
            })?;
        }

        Ok(removed)
    }

    /// Save multiple secrets to a config file, preserving comments and formatting.
    ///
    /// This is the batch equivalent of `save_secret_to_source`, used by `fnox import`.
    pub fn save_secrets_to_source(
        secrets: &IndexMap<String, SecretConfig>,
        profile: &str,
        target_file: &Path,
    ) -> Result<()> {
        use toml_edit::{DocumentMut, Item, Value};

        // Load existing document or create new one (preserves comments)
        let mut doc = if target_file.exists() {
            let content =
                fs::read_to_string(target_file).map_err(|source| FnoxError::ConfigReadFailed {
                    path: target_file.to_path_buf(),
                    source,
                })?;
            content.parse::<DocumentMut>().map_err(|e| {
                FnoxError::Config(format!(
                    "Failed to parse TOML in {}: {}",
                    target_file.display(),
                    e
                ))
            })?
        } else {
            DocumentMut::new()
        };

        // Get or create the secrets table
        let secrets_table = if profile == "default" {
            if doc.get("secrets").is_none() {
                doc["secrets"] = Item::Table(toml_edit::Table::new());
            }
            doc["secrets"].as_table_mut().unwrap()
        } else {
            if doc.get("profiles").is_none() {
                doc["profiles"] = Item::Table(toml_edit::Table::new());
            }
            let profiles = doc["profiles"].as_table_mut().unwrap();
            if profiles.get(profile).is_none() {
                profiles[profile] = Item::Table(toml_edit::Table::new());
            }
            let profile_table = profiles[profile].as_table_mut().unwrap();
            if profile_table.get("secrets").is_none() {
                profile_table["secrets"] = Item::Table(toml_edit::Table::new());
            }
            profile_table["secrets"].as_table_mut().unwrap()
        };

        // Insert/update each secret as an inline table
        for (name, config) in secrets {
            let inline = config.to_inline_table();

            // Update existing values in-place to preserve decor/comments on the entry
            if let Some(item) = secrets_table.get_mut(name.as_str()) {
                if let Item::Value(Value::InlineTable(existing_inline)) = item {
                    *existing_inline = inline;
                } else {
                    *item = Item::Value(Value::InlineTable(inline));
                }
            } else {
                secrets_table[name.as_str()] = Item::Value(Value::InlineTable(inline));

                if let Some(mut key) = secrets_table.key_mut(name.as_str()) {
                    key.leaf_decor_mut().set_suffix("");
                }
            }
        }

        // Write back (preserves all comments and formatting)
        fs::write(target_file, doc.to_string()).map_err(|source| FnoxError::ConfigWriteFailed {
            path: target_file.to_path_buf(),
            source,
        })?;

        Ok(())
    }

    /// Create a new default configuration
    pub fn new() -> Self {
        Self {
            import: Vec::new(),
            root: false,
            leases: IndexMap::new(),
            providers: IndexMap::new(),
            default_provider: None,
            secrets: IndexMap::new(),
            profiles: IndexMap::new(),
            age_key_file: None,
            if_missing: None,
            prompt_auth: None,
            mcp: None,
            provider_sources: HashMap::new(),
            secret_sources: HashMap::new(),
            default_provider_source: None,
            project_dir: None,
        }
    }

    /// Get the profile to use (from flag or env var, defaulting to "default")
    pub fn get_profile(profile_flag: Option<&str>) -> String {
        profile_flag
            .map(String::from)
            .or_else(|| (*env::FNOX_PROFILE).clone())
            .unwrap_or_else(|| "default".to_string())
    }

    /// Determine if we should prompt for authentication when provider auth fails.
    /// Priority: env var > config > default (true)
    /// Returns true only if prompting is enabled AND we're in a TTY.
    pub fn should_prompt_auth(&self) -> bool {
        // Check env var first
        let enabled = (*env::FNOX_PROMPT_AUTH)
            .or(self.prompt_auth)
            .unwrap_or(true);

        // Only prompt if enabled AND we're in a TTY
        enabled && atty::is(atty::Stream::Stdin)
    }

    /// Get secrets for the default profile (mutable)
    pub fn get_default_secrets_mut(&mut self) -> &mut IndexMap<String, SecretConfig> {
        &mut self.secrets
    }

    /// Get secrets for a specific profile (mutable)
    pub fn get_profile_secrets_mut(
        &mut self,
        profile: &str,
    ) -> &mut IndexMap<String, SecretConfig> {
        &mut self
            .profiles
            .entry(profile.to_string())
            .or_default()
            .secrets
    }

    /// Get effective secrets (default or profile)
    /// For non-default profiles, this merges top-level secrets with profile-specific secrets,
    /// with profile secrets taking precedence.
    ///
    /// Note: If a profile doesn't exist in [profiles], it's treated as "default".
    /// This allows fnox.$FNOX_PROFILE.toml files to work without requiring a [profiles] section.
    pub fn get_secrets(&self, profile: &str) -> Result<IndexMap<String, SecretConfig>> {
        if profile == "default" {
            return Ok(self.secrets.clone());
        }

        let mut secrets = if Settings::get().no_defaults {
            // Profile-only mode: do not merge top-level secrets.
            IndexMap::new()
        } else {
            // Start with top-level secrets as base
            self.secrets.clone()
        };

        // Get profile-specific secrets and merge/override (if profile exists)
        if let Some(profile_config) = self.profiles.get(profile) {
            // Profile-specific secrets override top-level ones
            secrets.extend(profile_config.secrets.clone());
        }
        // If profile doesn't exist in [profiles], that's OK - just use top-level secrets
        // This allows fnox.$FNOX_PROFILE.toml to work without requiring [profiles.xxx]
        Ok(secrets)
    }

    /// Look up a single secret by key without cloning the secrets map.
    ///
    /// Mirrors the precedence used by [`Self::get_secrets`]: profile-specific
    /// secrets take precedence, falling back to top-level secrets unless
    /// `no_defaults` is set.
    pub fn get_secret(&self, profile: &str, key: &str) -> Option<&SecretConfig> {
        if profile != "default"
            && let Some(profile_config) = self.profiles.get(profile)
            && let Some(secret) = profile_config.secrets.get(key)
        {
            return Some(secret);
        }

        if profile != "default" && Settings::get().no_defaults {
            return None;
        }

        self.secrets.get(key)
    }

    /// Get effective secrets (default or profile, mutable)
    pub fn get_secrets_mut(&mut self, profile: &str) -> &mut IndexMap<String, SecretConfig> {
        if profile == "default" {
            self.get_default_secrets_mut()
        } else {
            self.get_profile_secrets_mut(profile)
        }
    }

    /// Get effective lease backends for a profile
    pub fn get_leases(
        &self,
        profile: &str,
    ) -> IndexMap<String, crate::lease_backends::LeaseBackendConfig> {
        let mut leases = self.leases.clone();

        if profile != "default"
            && let Some(profile_config) = self.profiles.get(profile)
        {
            leases.extend(profile_config.leases.clone());
        }

        leases
    }

    /// Get effective providers for a profile
    pub fn get_providers(&self, profile: &str) -> IndexMap<String, ProviderConfig> {
        let mut providers = self.providers.clone(); // Start with global providers

        if profile != "default"
            && let Some(profile_config) = self.profiles.get(profile)
        {
            providers.extend(profile_config.providers.clone());
        }

        providers
    }

    /// Get the default provider for a profile
    /// Returns the configured default_provider, or auto-selects if there's only one provider
    pub fn get_default_provider(&self, profile: &str) -> Result<Option<String>> {
        let providers = self.get_providers(profile);

        // If no providers configured and this is a root config, return None
        if providers.is_empty() && self.root {
            return Ok(None);
        }

        // If no providers configured, that's an error
        if providers.is_empty() {
            return Err(FnoxError::Config(
                "No providers configured. Add at least one provider to fnox.toml".to_string(),
            ));
        }

        // Check for profile-specific default provider
        if profile != "default"
            && let Some(profile_config) = self.profiles.get(profile)
            && let Some(default_provider_name) = profile_config.default_provider()
        {
            // Validate that the default provider exists
            if !providers.contains_key(default_provider_name) {
                // Try to get source info for better error reporting
                if let Some(source_path) = &profile_config.default_provider_source
                    && let (Some(src), Some(span)) = (
                        source_registry::get_named_source(source_path),
                        profile_config.default_provider_span(),
                    )
                {
                    return Err(FnoxError::DefaultProviderNotFoundWithSource {
                        provider: default_provider_name.to_string(),
                        profile: profile.to_string(),
                        src,
                        span: span.into(),
                    });
                }
                return Err(FnoxError::Config(format!(
                    "Default provider '{}' not found in profile '{}'",
                    default_provider_name, profile
                )));
            }
            return Ok(Some(default_provider_name.to_string()));
        }

        // Check for global default provider (for default profile or as fallback)
        if let Some(default_provider_name) = self.default_provider() {
            // Validate that the default provider exists
            if !providers.contains_key(default_provider_name) {
                // Try to get source info for better error reporting
                if let Some(source_path) = &self.default_provider_source
                    && let (Some(src), Some(span)) = (
                        source_registry::get_named_source(source_path),
                        self.default_provider_span(),
                    )
                {
                    return Err(FnoxError::DefaultProviderNotFoundWithSource {
                        provider: default_provider_name.to_string(),
                        profile: profile.to_string(),
                        src,
                        span: span.into(),
                    });
                }
                return Err(FnoxError::Config(format!(
                    "Default provider '{}' not found in configuration",
                    default_provider_name
                )));
            }
            return Ok(Some(default_provider_name.to_string()));
        }

        // If there's exactly one provider, auto-select it
        if providers.len() == 1 {
            let provider_name = providers.keys().next().unwrap().clone();
            tracing::debug!(
                "Auto-selecting provider '{}' as it's the only one configured",
                provider_name
            );
            return Ok(Some(provider_name));
        }

        // Multiple providers, no default configured
        Ok(None)
    }

    /// Set source paths for all secrets and providers in this config
    fn set_source_paths(&mut self, path: &Path) {
        // Set source paths for default profile secrets
        for (key, secret) in self.secrets.iter_mut() {
            secret.source_path = Some(path.to_path_buf());
            self.secret_sources.insert(key.clone(), path.to_path_buf());
        }

        // Set source paths for default profile providers
        for (provider_name, _) in self.providers.iter() {
            self.provider_sources
                .insert(provider_name.clone(), path.to_path_buf());
        }

        // Set source path for default_provider if set
        if self.default_provider().is_some() {
            self.default_provider_source = Some(path.to_path_buf());
        }

        // Set source paths for named profiles
        for (_profile_name, profile) in self.profiles.iter_mut() {
            for (key, secret) in profile.secrets.iter_mut() {
                secret.source_path = Some(path.to_path_buf());
                secret.source_is_profile = true;
                profile
                    .secret_sources
                    .insert(key.clone(), path.to_path_buf());
            }

            for (provider_name, _) in profile.providers.iter() {
                profile
                    .provider_sources
                    .insert(provider_name.clone(), path.to_path_buf());
            }

            // Set source path for profile's default_provider if set
            if profile.default_provider().is_some() {
                profile.default_provider_source = Some(path.to_path_buf());
            }
        }
    }

    /// Check if a secret has an empty value that should be flagged as a validation issue.
    /// Returns a ValidationIssue if the secret has an empty value and is not using plain provider.
    fn check_empty_value(
        &self,
        key: &str,
        secret: &SecretConfig,
        profile: &str,
    ) -> Option<crate::error::ValidationIssue> {
        // Early return if value is not an empty string
        let Some(value) = secret.value() else {
            return None; // No value specified - not an issue
        };
        if !value.is_empty() {
            return None; // Non-empty value - not an issue
        }

        // At this point, value is an empty string
        // Allow empty values for plain provider (empty string is a valid secret value)
        if self.is_plain_provider(secret.provider(), profile) {
            return None;
        }
        let message = if profile == "default" {
            format!("Secret '{}' has an empty value", key)
        } else {
            format!(
                "Secret '{}' in profile '{}' has an empty value",
                key, profile
            )
        };
        Some(crate::error::ValidationIssue::with_help(
            message,
            "Set a value for this secret or remove it from the configuration",
        ))
    }

    /// Check if a secret uses the plain provider (where empty values are valid).
    /// Returns true if the provider is "plain" type.
    fn is_plain_provider(&self, secret_provider: Option<&str>, profile: &str) -> bool {
        // Get providers for this profile first (needed for auto-selection)
        let providers = self.get_providers(profile);

        // Determine which provider name to use
        let provider_name = secret_provider
            .map(String::from)
            .or_else(|| {
                // Try profile's default_provider first (only for non-default profiles)
                if profile != "default" {
                    self.profiles
                        .get(profile)
                        .and_then(|p| p.default_provider().map(|s| s.to_string()))
                } else {
                    None
                }
            })
            .or_else(|| self.default_provider().map(|s| s.to_string()))
            .or_else(|| {
                // Auto-select if exactly one provider exists (matching get_default_provider behavior)
                if providers.len() == 1 {
                    providers.keys().next().cloned()
                } else {
                    None
                }
            });

        let Some(provider_name) = provider_name else {
            return false;
        };

        // Look up the provider config
        providers
            .get(&provider_name)
            .is_some_and(|p| p.provider_type() == "plain")
    }

    /// Validate the configuration
    /// Collects all validation issues and returns them together using #[related]
    pub fn validate(&self) -> Result<()> {
        use crate::error::ValidationIssue;

        // If root=true and no providers AND no secrets, that's OK (empty config)
        if self.root
            && self.providers.is_empty()
            && self.profiles.is_empty()
            && self.secrets.is_empty()
        {
            return Ok(());
        }

        let mut issues = Vec::new();

        // Check for secrets with empty values (likely a mistake, but allowed for plain provider)
        for (key, secret) in &self.secrets {
            if let Some(issue) = self.check_empty_value(key, secret, "default") {
                issues.push(issue);
            }
        }

        // Check that there's at least one provider if there are any secrets
        if self.providers.is_empty() && self.profiles.is_empty() && !self.secrets.is_empty() {
            issues.push(ValidationIssue::with_help(
                "No providers configured",
                "Add at least one provider to fnox.toml",
            ));
        }

        // If default_provider is set, validate it exists
        if let Some(default_provider_name) = self.default_provider()
            && !self.providers.contains_key(default_provider_name)
        {
            // Try to get source info for better error reporting
            if let Some(source_path) = &self.default_provider_source
                && let (Some(src), Some(span)) = (
                    source_registry::get_named_source(source_path),
                    self.default_provider_span(),
                )
            {
                return Err(FnoxError::DefaultProviderNotFoundWithSource {
                    provider: default_provider_name.to_string(),
                    profile: "default".to_string(),
                    src,
                    span: span.into(),
                });
            }
            issues.push(ValidationIssue::with_help(
                format!(
                    "Default provider '{}' not found in configuration",
                    default_provider_name
                ),
                format!(
                    "Add [providers.{}] to your config or remove the default_provider setting",
                    default_provider_name
                ),
            ));
        }

        // Validate each profile
        for (profile_name, profile_config) in &self.profiles {
            let providers = self.get_providers(profile_name);

            // Check for profile secrets with empty values (likely a mistake, but allowed for plain provider)
            for (key, secret) in &profile_config.secrets {
                if let Some(issue) = self.check_empty_value(key, secret, profile_name) {
                    issues.push(issue);
                }
            }

            // Each profile must have at least one provider (inherited or its own), unless root=true
            if providers.is_empty() && !self.root {
                issues.push(ValidationIssue::with_help(
                    format!("Profile '{}' has no providers configured", profile_name),
                    format!(
                        "Add [profiles.{}.providers.<name>] or inherit from top-level providers",
                        profile_name
                    ),
                ));
            }

            // If profile has default_provider set, validate it exists
            if let Some(default_provider_name) = profile_config.default_provider()
                && !providers.contains_key(default_provider_name)
            {
                // Try to get source info for better error reporting
                if let Some(source_path) = &profile_config.default_provider_source
                    && let (Some(src), Some(span)) = (
                        source_registry::get_named_source(source_path),
                        profile_config.default_provider_span(),
                    )
                {
                    return Err(FnoxError::DefaultProviderNotFoundWithSource {
                        provider: default_provider_name.to_string(),
                        profile: profile_name.clone(),
                        src,
                        span: span.into(),
                    });
                }
                issues.push(ValidationIssue::with_help(
                    format!(
                        "Default provider '{}' not found in profile '{}'",
                        default_provider_name, profile_name
                    ),
                    format!(
                        "Add [profiles.{}.providers.{}] or remove the default_provider setting",
                        profile_name, default_provider_name
                    ),
                ));
            }
        }

        if issues.is_empty() {
            Ok(())
        } else {
            Err(FnoxError::ConfigValidationFailed { issues })
        }
    }

    /// Get the default provider name, if set.
    pub fn default_provider(&self) -> Option<&str> {
        self.default_provider
            .as_ref()
            .map(|s: &SpannedValue<String>| s.value().as_str())
    }

    /// Get the default provider's source span (byte range in the config file).
    /// Returns None if the default_provider wasn't set or was created programmatically.
    pub fn default_provider_span(&self) -> Option<Range<usize>> {
        self.default_provider
            .as_ref()
            .and_then(|s: &SpannedValue<String>| s.span())
    }

    /// Set the default provider name (without span information).
    pub fn set_default_provider(&mut self, provider: Option<String>) {
        self.default_provider = provider.map(SpannedValue::without_span);
    }
}

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

impl SecretConfig {
    /// Create a new secret config with just metadata
    pub fn new() -> Self {
        Self {
            description: None,
            if_missing: None,
            default: None,
            provider: None,
            value: None,
            env: true,
            as_file: false,
            json_path: None,
            line: None,
            sync: None,
            source_path: None,
            source_is_profile: false,
        }
    }

    /// Return a copy that will resolve to the original provider value,
    /// skipping post-processing and cached sync values.
    pub fn for_raw_resolve(&self) -> Self {
        let mut config = self.clone();
        config.json_path = None;
        config.line = None;
        config.sync = None;
        config.default = None;
        config
    }

    /// Convert this secret config to a TOML inline table for saving
    pub fn to_inline_table(&self) -> toml_edit::InlineTable {
        let mut inline = toml_edit::InlineTable::new();

        if let Some(provider) = self.provider() {
            inline.insert("provider", toml_edit::Value::from(provider));
        }
        if let Some(value) = self.value() {
            inline.insert("value", toml_edit::Value::from(value));
        }
        if let Some(ref json_path) = self.json_path {
            inline.insert("json_path", toml_edit::Value::from(json_path.as_str()));
        }
        if let Some(line) = self.line {
            inline.insert("line", toml_edit::Value::from(line as i64));
        }
        if let Some(ref description) = self.description {
            inline.insert("description", toml_edit::Value::from(description.as_str()));
        }
        if let Some(ref default) = self.default {
            inline.insert("default", toml_edit::Value::from(default.as_str()));
        }
        if let Some(if_missing) = self.if_missing {
            let if_missing_str = match if_missing {
                IfMissing::Error => "error",
                IfMissing::Warn => "warn",
                IfMissing::Ignore => "ignore",
            };
            inline.insert("if_missing", toml_edit::Value::from(if_missing_str));
        }
        if !self.env {
            inline.insert("env", toml_edit::Value::from(false));
        }
        if self.as_file {
            inline.insert("as_file", toml_edit::Value::from(true));
        }
        if let Some(ref sync) = self.sync {
            let mut sync_table = toml_edit::InlineTable::new();
            sync_table.insert("provider", toml_edit::Value::from(sync.provider.as_str()));
            sync_table.insert("value", toml_edit::Value::from(sync.value.as_str()));
            sync_table.fmt();
            inline.insert("sync", toml_edit::Value::InlineTable(sync_table));
        }

        inline.fmt();
        inline
    }

    /// Check if this secret has any value (provider, value, or default)
    pub fn has_value(&self) -> bool {
        self.provider().is_some() || self.value().is_some() || self.default.is_some()
    }

    /// Get the provider name, if set.
    pub fn provider(&self) -> Option<&str> {
        self.provider.as_ref().map(|s| s.value().as_str())
    }

    /// Get the provider's source span (byte range in the config file).
    /// Returns None if the provider wasn't set or was created programmatically.
    pub fn provider_span(&self) -> Option<Range<usize>> {
        self.provider.as_ref().and_then(|s| s.span())
    }

    /// Set the provider name (without span information).
    pub fn set_provider(&mut self, provider: Option<String>) {
        self.provider = provider.map(SpannedValue::without_span);
    }

    /// Get the value, if set.
    pub fn value(&self) -> Option<&str> {
        self.value
            .as_ref()
            .map(|s: &SpannedValue<String>| s.value().as_str())
    }

    /// Set the value (without span information).
    pub fn set_value(&mut self, value: Option<String>) {
        self.value = value.map(SpannedValue::without_span);
    }
}

impl ProfileConfig {
    /// Create a new profile config
    pub fn new() -> Self {
        Self {
            leases: IndexMap::new(),
            providers: IndexMap::new(),
            default_provider: None,
            secrets: IndexMap::new(),
            provider_sources: HashMap::new(),
            secret_sources: HashMap::new(),
            default_provider_source: None,
        }
    }

    /// Check if the profile is effectively empty (no serializable content)
    pub fn is_empty(&self) -> bool {
        self.leases.is_empty()
            && self.providers.is_empty()
            && self.secrets.is_empty()
            && self.default_provider().is_none()
    }

    /// Get the default provider name, if set.
    pub fn default_provider(&self) -> Option<&str> {
        self.default_provider
            .as_ref()
            .map(|s: &SpannedValue<String>| s.value().as_str())
    }

    /// Get the default provider's source span (byte range in the config file).
    /// Returns None if the default_provider wasn't set or was created programmatically.
    pub fn default_provider_span(&self) -> Option<Range<usize>> {
        self.default_provider
            .as_ref()
            .and_then(|s: &SpannedValue<String>| s.span())
    }
}

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

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

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

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

fn default_true() -> bool {
    true
}

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

    #[test]
    fn test_empty_import_not_serialized() {
        let config = Config::new();
        let toml = toml_edit::ser::to_string_pretty(&config).unwrap();
        assert!(
            !toml.contains("import"),
            "Empty import should not be serialized"
        );
    }

    #[test]
    fn test_non_empty_import_is_serialized() {
        let mut config = Config::new();
        config.import.push("other.toml".to_string());
        let toml = toml_edit::ser::to_string_pretty(&config).unwrap();
        assert!(
            toml.contains("import"),
            "Non-empty import should be serialized"
        );
        assert!(
            toml.contains("other.toml"),
            "Import value should be present"
        );
    }

    #[test]
    fn test_empty_profiles_not_serialized() {
        let config = Config::new();
        let toml = toml_edit::ser::to_string_pretty(&config).unwrap();
        assert!(
            !toml.contains("profiles"),
            "Empty profiles should not be serialized"
        );
    }

    #[test]
    fn test_non_empty_profiles_is_serialized() {
        let mut config = Config::new();

        // Add a provider and secret to the prod profile
        let mut prod_profile = ProfileConfig::new();
        prod_profile.providers.insert(
            "plain".to_string(),
            ProviderConfig::Plain { auth_command: None },
        );
        let mut secret = SecretConfig::new();
        secret.set_value(Some("test-value".to_string()));
        prod_profile
            .secrets
            .insert("TEST_SECRET".to_string(), secret);

        config.profiles.insert("prod".to_string(), prod_profile);
        let toml = toml_edit::ser::to_string_pretty(&config).unwrap();

        // Print the TOML for debugging
        eprintln!("Generated TOML:\n{}", toml);

        assert!(
            toml.contains("profiles"),
            "Non-empty profiles should be serialized"
        );
        assert!(toml.contains("prod"), "Profile name should be present");

        // Check that we don't have a standalone [profiles] header
        // We should only have [profiles.prod] style headers
        assert!(
            !toml.contains("[profiles]\n"),
            "Should not have standalone [profiles] header"
        );
    }

    #[test]
    fn test_local_override_filename_matches_standard_config_names() {
        assert_eq!(
            local_override_filename(Path::new("nested/fnox.toml")),
            Some("fnox.local.toml")
        );
        assert_eq!(
            local_override_filename(Path::new("nested/.fnox.toml")),
            Some(".fnox.local.toml")
        );
    }

    #[test]
    fn test_local_override_filename_rejects_non_standard_config_names() {
        assert_eq!(
            local_override_filename(Path::new("nested/custom.toml")),
            None
        );
        assert_eq!(
            local_override_filename(Path::new("nested/fnox.dev.toml")),
            None
        );
    }

    #[test]
    fn test_empty_profile_not_serialized() {
        use std::io::Read;

        let mut config = Config::new();
        // Add an empty profile (no providers, no secrets)
        config
            .profiles
            .insert("prod".to_string(), ProfileConfig::new());

        // Use save() which cleans up empty profiles
        let temp_file = std::env::temp_dir().join("fnox_test_empty_profile.toml");
        config.save(&temp_file).unwrap();

        let mut toml = String::new();
        std::fs::File::open(&temp_file)
            .unwrap()
            .read_to_string(&mut toml)
            .unwrap();
        std::fs::remove_file(&temp_file).ok();

        eprintln!("Generated TOML with empty profile:\n{}", toml);

        // Empty profiles should not appear in the output at all
        // Because save() cleans them up
        assert!(
            !toml.contains("[profiles"),
            "Empty profile should not be serialized"
        );
        assert!(
            !toml.contains("prod"),
            "Empty profile name should not appear"
        );
    }

    #[test]
    fn test_no_defaults_profile_only_secrets() {
        crate::settings::Settings::reset_for_tests();
        crate::settings::Settings::set_cli_snapshot(crate::settings::CliSnapshot {
            age_key_file: None,
            profile: Some("prod".to_string()),
            if_missing: None,
            no_defaults: true,
        });

        let mut config = Config::new();
        config
            .secrets
            .insert("DEFAULT_ONLY".to_string(), SecretConfig::new());

        let mut prod_profile = ProfileConfig::new();
        prod_profile
            .secrets
            .insert("PROD_ONLY".to_string(), SecretConfig::new());
        config.profiles.insert("prod".to_string(), prod_profile);

        let secrets = config.get_secrets("prod").unwrap();
        assert!(secrets.contains_key("PROD_ONLY"));
        assert!(!secrets.contains_key("DEFAULT_ONLY"));
    }

    #[test]
    fn test_no_defaults_profile_without_section_is_empty() {
        crate::settings::Settings::reset_for_tests();
        crate::settings::Settings::set_cli_snapshot(crate::settings::CliSnapshot {
            age_key_file: None,
            profile: Some("prod".to_string()),
            if_missing: None,
            no_defaults: true,
        });

        let mut config = Config::new();
        config
            .secrets
            .insert("DEFAULT_ONLY".to_string(), SecretConfig::new());

        let secrets = config.get_secrets("prod").unwrap();
        assert!(secrets.is_empty());
    }

    #[test]
    fn test_find_local_config_no_files() {
        let dir = tempfile::tempdir().unwrap();
        let result = super::find_local_config(dir.path(), None);
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_only_fnox_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), None);
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_only_local_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.local.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), None);
        assert_eq!(result, dir.path().join("fnox.local.toml"));
    }

    #[test]
    fn test_find_local_config_both_exist() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.toml"), "").unwrap();
        std::fs::write(dir.path().join("fnox.local.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), None);
        // Should pick fnox.toml (lowest priority)
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_only_dotfile() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join(".fnox.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), None);
        assert_eq!(result, dir.path().join(".fnox.toml"));
    }

    #[test]
    fn test_find_local_config_profile() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.staging.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), Some("staging"));
        assert_eq!(result, dir.path().join("fnox.staging.toml"));
    }

    #[test]
    fn test_find_local_config_profile_with_base() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.toml"), "").unwrap();
        std::fs::write(dir.path().join("fnox.staging.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), Some("staging"));
        // Profile-specific file is preferred when profile is active
        assert_eq!(result, dir.path().join("fnox.staging.toml"));
    }

    #[test]
    fn test_find_local_config_default_profile_with_base() {
        // Default profile should still pick fnox.toml (lowest priority)
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.toml"), "").unwrap();
        std::fs::write(dir.path().join("fnox.local.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), Some("default"));
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_profile_only_base_exists() {
        // Profile specified but only base config exists — fall back to it
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), Some("staging"));
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_profile_skips_local_file() {
        // When a profile is active and only fnox.local.toml exists,
        // should NOT write there — fall through to creating fnox.toml
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.local.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), Some("staging"));
        assert_eq!(result, dir.path().join("fnox.toml"));
    }

    #[test]
    fn test_find_local_config_no_profile_uses_local_file() {
        // Without a profile, fnox.local.toml is a valid write target
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fnox.local.toml"), "").unwrap();
        let result = super::find_local_config(dir.path(), None);
        assert_eq!(result, dir.path().join("fnox.local.toml"));
    }

    #[test]
    fn filter_secrets_none_allowlist_returns_all() {
        let cfg = McpConfig::default(); // secrets: None
        let mut m = IndexMap::new();
        m.insert("A".to_string(), SecretConfig::new());
        m.insert("B".to_string(), SecretConfig::new());
        let result = cfg.filter_secrets(m.clone());
        assert_eq!(
            result.keys().collect::<Vec<_>>(),
            m.keys().collect::<Vec<_>>()
        );
    }

    #[test]
    fn filter_secrets_empty_allowlist_returns_empty() {
        let cfg = McpConfig {
            secrets: Some(vec![]),
            ..Default::default()
        };
        let mut m = IndexMap::new();
        m.insert("A".to_string(), SecretConfig::new());
        assert!(cfg.filter_secrets(m).is_empty());
    }

    #[test]
    fn filter_secrets_subset() {
        let cfg = McpConfig {
            secrets: Some(vec!["A".into()]),
            ..Default::default()
        };
        let mut m = IndexMap::new();
        m.insert("A".to_string(), SecretConfig::new());
        m.insert("B".to_string(), SecretConfig::new());
        let result = cfg.filter_secrets(m);
        assert!(result.contains_key("A"));
        assert!(!result.contains_key("B"));
    }

    #[test]
    fn filter_secrets_unknown_allowlist_entry_ignored() {
        let cfg = McpConfig {
            secrets: Some(vec!["A".into(), "NONEXISTENT".into()]),
            ..Default::default()
        };
        let mut m = IndexMap::new();
        m.insert("A".to_string(), SecretConfig::new());
        let result = cfg.filter_secrets(m);
        assert_eq!(result.len(), 1);
        assert!(result.contains_key("A"));
    }

    #[test]
    fn mcp_secrets_overlay_replaces_base_not_appends() {
        let base = Config {
            mcp: Some(McpConfig {
                secrets: Some(vec!["A".into()]),
                ..Default::default()
            }),
            ..Config::new()
        };
        let overlay = Config {
            mcp: Some(McpConfig {
                secrets: Some(vec!["B".into()]),
                ..Default::default()
            }),
            ..Config::new()
        };
        let merged = Config::merge_configs(base, overlay).unwrap();
        assert_eq!(
            merged.mcp.unwrap().secrets,
            Some(vec!["B".into()]),
            "overlay must replace, not append, the base allowlist"
        );
    }

    #[test]
    fn mcp_secrets_overlay_without_secrets_preserves_base() {
        let base = Config {
            mcp: Some(McpConfig {
                secrets: Some(vec!["A".into()]),
                ..Default::default()
            }),
            ..Config::new()
        };
        let overlay = Config {
            mcp: Some(McpConfig {
                ..Default::default()
            }),
            ..Config::new()
        };
        let merged = Config::merge_configs(base, overlay).unwrap();
        assert_eq!(merged.mcp.unwrap().secrets, Some(vec!["A".into()]));
    }

    #[test]
    fn test_for_raw_resolve_strips_post_processing_fields() {
        let mut secret = SecretConfig::new();
        secret.set_provider(Some("plain".to_string()));
        secret.set_value(Some(r#"{"user":"admin"}"#.to_string()));
        secret.default = Some("fallback".to_string());
        secret.json_path = Some("user".to_string());
        secret.line = Some(2);
        secret.sync = Some(SyncConfig {
            provider: "age".to_string(),
            value: "encrypted-blob".to_string(),
        });

        let raw = secret.for_raw_resolve();

        assert!(raw.default.is_none());
        assert!(raw.json_path.is_none());
        assert!(raw.line.is_none());
        assert!(raw.sync.is_none());
    }

    #[test]
    fn test_secret_config_line_roundtrip() {
        let toml_input = r#"
[secrets]
USERNAME = { provider = "pass", value = "master", line = 2 }
"#;
        let parsed: Config = toml_edit::de::from_str(toml_input).unwrap();
        let secret = parsed.secrets.get("USERNAME").unwrap();
        assert_eq!(secret.line, Some(2));

        let inline = secret.to_inline_table();
        let rendered = inline.to_string();
        assert!(
            rendered.contains("line = 2"),
            "expected serialized output to contain `line = 2`, got: {rendered}"
        );
    }

    #[test]
    fn test_for_raw_resolve_preserves_non_post_processing_fields() {
        let mut secret = SecretConfig::new();
        secret.set_provider(Some("plain".to_string()));
        secret.set_value(Some("my-secret".to_string()));
        secret.description = Some("A test secret".to_string());
        secret.if_missing = Some(IfMissing::Warn);
        secret.env = false;
        secret.as_file = true;
        secret.source_path = Some(PathBuf::from("/tmp/fnox.toml"));
        secret.source_is_profile = true;
        secret.default = Some("default-val".to_string());
        secret.json_path = Some("key".to_string());
        secret.sync = Some(SyncConfig {
            provider: "age".to_string(),
            value: "blob".to_string(),
        });

        let raw = secret.for_raw_resolve();

        assert_eq!(raw.provider(), Some("plain"));
        assert_eq!(raw.value(), Some("my-secret"));
        assert_eq!(raw.description.as_deref(), Some("A test secret"));
        assert_eq!(raw.if_missing, Some(IfMissing::Warn));
        assert!(!raw.env);
        assert!(raw.as_file);
        assert_eq!(
            raw.source_path.as_deref(),
            Some(Path::new("/tmp/fnox.toml"))
        );
        assert!(raw.source_is_profile);
    }

    #[test]
    fn test_for_raw_resolve_with_no_post_processing_fields() {
        let mut secret = SecretConfig::new();
        secret.set_provider(Some("plain".to_string()));
        secret.set_value(Some("simple-value".to_string()));

        let raw = secret.for_raw_resolve();

        assert_eq!(raw.provider(), Some("plain"));
        assert_eq!(raw.value(), Some("simple-value"));
        assert!(raw.default.is_none());
        assert!(raw.json_path.is_none());
        assert!(raw.sync.is_none());
    }
}