minutes-core 0.18.8

Core library for minutes — audio capture, transcription, and meeting memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
use serde::{Deserialize, Serialize};
use std::ffi::OsString;
use std::path::{Path, PathBuf};

// ──────────────────────────────────────────────────────────────
// Config loading precedence:
//   Compiled defaults → config file override → CLI flag override
//
// Config file is OPTIONAL. minutes works without one.
// ──────────────────────────────────────────────────────────────

/// Desktop-only fallback env var used when the Tauri app hydrates an
/// OpenAI-compatible gateway key from macOS Keychain for non-local endpoints.
pub const OPENAI_COMPATIBLE_DESKTOP_API_KEY_ENV: &str = "MINUTES_OPENAI_COMPATIBLE_API_KEY";

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub output_dir: PathBuf,
    pub transcription: TranscriptionConfig,
    pub diarization: DiarizationConfig,
    pub summarization: SummarizationConfig,
    pub search: SearchConfig,
    pub daily_notes: DailyNotesConfig,
    pub security: SecurityConfig,
    pub watch: WatchConfig,
    pub assistant: AssistantConfig,
    pub privacy: PrivacyConfig,
    pub consent: ConsentConfig,
    pub screen_context: ScreenContextConfig,
    pub desktop_context: DesktopContextConfig,
    pub calendar: CalendarConfig,
    pub call_detection: CallDetectionConfig,
    pub identity: IdentityConfig,
    pub vault: VaultConfig,
    pub dictation: DictationConfig,
    pub voice: VoiceConfig,
    pub live_transcript: LiveTranscriptConfig,
    pub recording: RecordingConfig,
    pub retention: RetentionConfig,
    pub hooks: HooksConfig,
    pub knowledge: KnowledgeConfig,
    pub palette: PaletteConfig,
}

/// Command palette configuration.
///
/// The palette is the keyboard-first command surface introduced in v0.11.
/// Both fresh installs and upgrades default `shortcut_enabled` to
/// `true`. The Tauri desktop app fires a one-shot system notification
/// on the first launch that registers the shortcut so users with a
/// real conflict (VS Code Delete Line, JetBrains Push, etc.) hear
/// about the new binding immediately and can disable it from the
/// Settings UI in one click. The first-run marker file lives at
/// `~/.minutes/palette_first_run_shown` and only commits after the
/// notification is dispatched successfully — see
/// `commands::maybe_show_palette_first_run_notice` for the details.
///
/// An earlier draft of this struct used a different design where
/// `Config::load_with_migrations` flipped `shortcut_enabled` to
/// `false` for upgraders. Dogfood feedback rejected that as
/// undiscoverable: opt-in via `config.toml` is invisible, and the
/// settings UI for the palette didn't exist yet. The current design
/// pairs default-on with a visible Settings UI surface and a
/// first-run notification.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PaletteConfig {
    /// Whether the global palette shortcut is enabled.
    pub shortcut_enabled: bool,
    /// The palette shortcut string (e.g., "CmdOrCtrl+Shift+K").
    pub shortcut: String,
}

impl Default for PaletteConfig {
    fn default() -> Self {
        // Both fresh installs and upgrades use this value. The
        // upgrade migration in `load_with_migrations` only persists
        // the section to disk so it's discoverable in `config.toml`;
        // it does NOT flip the bool.
        Self {
            shortcut_enabled: true,
            shortcut: "CmdOrCtrl+Shift+K".into(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct VoiceConfig {
    pub enabled: bool,
    pub match_threshold: f32,
}

impl Default for VoiceConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            match_threshold: 0.65,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TranscriptionConfig {
    /// Transcription engine: "whisper" (default), "parakeet", or
    /// "apple-speech" (experimental live-transcript-only path on macOS 26+).
    pub engine: String,
    pub model: String,
    pub model_path: PathBuf,
    pub min_words: usize,
    pub language: Option<String>,
    /// Silero VAD model name (resolved under model_path, e.g. "silero-v6.2.0" -> ggml-silero-v6.2.0.bin).
    /// Set to empty string to disable VAD (falls back to energy-based silence stripping).
    pub vad_model: String,
    /// VAD engine for the recording sidecar.
    ///
    /// **`"ort-silero"` (default, known-risk experimental, not
    /// recommended as a general default)**. A 20-WAV stratified
    /// screen of internal meeting audio truncated to 5 min each
    /// found 6/20 samples had at least one substantive regression
    /// vs whisper-silero. The Wilson 95% CI on the per-WAV rate is
    /// [15%, 52%], with a 30% point estimate. Per-utterance the
    /// rate is approximately 1.4% as secondary context. Regression
    /// types observed: named-entity loss (`"Claude"` -> `"cloth"`,
    /// real participant names redacted from this comment ->
    /// nonsense words), nonword hallucination at chunk boundaries,
    /// and content-word loss in the first ~30s of recordings.
    ///
    /// Mechanism: streaming Silero via ort, O(new_audio) per call.
    /// Requires the `vad-ort` build feature AND
    /// `silero-vad-v6.2.0.onnx` in `model_path`. About 2x faster
    /// than whisper-silero on the recording sidecar's hot path
    /// (median 2.16x on the same 20-WAV screen). Recommended only
    /// for users who explicitly accept the regression tradeoff.
    /// FSM tuning (candidates: max-chunk cap or chunk-boundary
    /// smoothing) is needed before promotion to the general
    /// default; see `PLAN-vad-refactor.md` and the harness at
    /// `crates/core/examples/dogfood_vad_engines.rs`.
    ///
    /// **`"whisper-silero"`**: whisper-rs's bundled Silero,
    /// full-buffer rescan per 100ms call. Slower; did not show the
    /// longer-chunk regression class in the 20-WAV screen. Set this
    /// in `~/.config/minutes/config.toml` to opt out of ort-silero
    /// on a per-process basis.
    ///
    /// **Fallback chain**: when `"ort-silero"` is requested but the
    /// `vad-ort` feature is off OR the ONNX is missing, the
    /// dispatcher logs a warning and falls through to
    /// `"whisper-silero"`. Unknown values log and fall through to
    /// `"whisper-silero"` as well. Energy is the dispatcher's
    /// emergency fallback; not a user-selectable engine here.
    pub vad_engine: String,
    /// Enable noise reduction via nnnoiseless (RNNoise) before transcription.
    /// Requires the `denoise` feature flag. Default: true.
    pub noise_reduction: bool,
    /// Path or name of the parakeet.cpp binary (resolved via PATH if not absolute).
    pub parakeet_binary: String,
    /// Parakeet model type: "tdt-ctc-110m", "tdt-600m".
    pub parakeet_model: String,
    /// Maximum number of knowledge-graph phrases to pass via `--boost`.
    /// Set to 0 to disable phrase boosting. Default: off until tuned further.
    pub parakeet_boost_limit: usize,
    /// Score passed to parakeet.cpp `--boost-score` when boost phrases are active.
    pub parakeet_boost_score: f32,
    /// Enable parakeet.cpp fp16 inference on the GPU path.
    ///
    /// This lowers memory use, but on the current process-per-transcription
    /// runtime it can add noticeable cold-start latency because the model is
    /// cast to fp16 on each run.
    pub parakeet_fp16: bool,
    /// Warm Parakeet example-server sidecar: `None` (default) = auto.
    ///
    /// Auto enables the sidecar when parakeet is the selected engine (batch or
    /// live) AND the `example-server` binary resolves; otherwise the cold
    /// per-utterance subprocess path is used. Explicit `true`/`false` in
    /// config.toml overrides auto in either direction (#295). Use
    /// `parakeet_sidecar::sidecar_enabled_effective()` to read the decision;
    /// never branch on this raw field.
    ///
    /// On-disk forms: absent or `"auto"` = auto; `true`/`"on"` = forced on;
    /// `"off"` = forced off. A legacy bool `false` is treated as auto, because
    /// pre-0.18.8 full-struct saves wrote `= false` into every config file
    /// while the key had no UI — it was a serializer artifact, never intent
    /// (#295). Deliberate forced-off therefore serializes as the string
    /// `"off"`, which the legacy serializer could never have written.
    #[serde(
        default,
        deserialize_with = "de_sidecar_tristate",
        serialize_with = "ser_sidecar_tristate",
        skip_serializing_if = "Option::is_none"
    )]
    pub parakeet_sidecar_enabled: Option<bool>,
    /// Clear the persistent parakeet fp16 blacklist before the next sidecar start.
    ///
    /// This is useful after upgrading the parakeet binary to a version that may
    /// have fixed prior fp16 startup crashes.
    pub parakeet_fp16_blacklist_reset: bool,
    /// SentencePiece vocab filename (resolved under model_path/parakeet/).
    ///
    /// If left at the default generic name, Minutes still prefers model-specific
    /// tokenizer files such as `tdt-ctc-110m.tokenizer.vocab` when they exist.
    pub parakeet_vocab: String,
    /// Cap (in seconds) on streaming-whisper partial transcriptions.
    ///
    /// Streaming whisper re-transcribes the entire accumulated utterance every
    /// 2s for full-context partials. Cost is O(buffer_len) per partial, so a
    /// long uninterrupted monologue will eventually take longer to transcribe
    /// than the partial interval and saturate the CPU. When the accumulated
    /// buffer exceeds this many seconds, partial passes are skipped — the
    /// utterance still finalizes correctly via VAD/silence detection or the
    /// caller's own utterance cap (`dictation.max_utterance_secs` or
    /// `live_transcript.max_utterance_secs`); the live transcript just stops
    /// refreshing during the long stretch.
    ///
    /// Default 30s matches the design note in `streaming_whisper.rs`. Raise
    /// for long-form dictation; lower if you still see CPU pressure.
    pub partial_max_secs: u32,
}

pub const VALID_PARAKEET_MODELS: &[&str] = &["tdt-ctc-110m", "tdt-600m"];

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DiarizationConfig {
    pub engine: String,
    pub model_path: PathBuf,
    /// Cosine similarity threshold for speaker matching (0.0–1.0).
    /// Lower values merge more aggressively; higher values create more speakers.
    pub threshold: f32,
    /// Speaker embedding model: "cam++" (default) or "cam++-lm".
    /// CAM++_LM has ~12% lower EER on benchmarks but produces lower cosine
    /// similarities, so `voice.match_threshold` must be lowered (~0.1–0.2)
    /// for voice enrollment matching to work reliably.
    pub embedding_model: String,
    /// Correlation threshold (0.0–1.0) above which stem-based diarization
    /// collapses voice + system stems to a single speaker. The check
    /// assumes high cross-stem correlation means one person bleeding into
    /// both sources (self-monitor / headphone leak), but it misfires for
    /// open-speaker mic setups (Studio Display Mic, laptop mic, desk USB
    /// mic near speakers) where the mic acoustically picks up multi-
    /// speaker system audio from a Zoom/Meet call. Raise to 1.0 or higher
    /// to disable the collapse and rely on per-window energy attribution.
    /// Default 0.85 preserves historical behavior.
    pub stem_correlation_threshold: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SummarizationConfig {
    pub engine: String,
    pub agent_command: String,
    /// Timeout for the `engine = "agent"` subprocess call (in seconds).
    /// For long transcripts on local LLM agents (e.g. opencode running
    /// against a 60k+ char input), the default 300s can be too short.
    /// Issue #243: when this budget is exceeded the pipeline emits a
    /// `processing_warnings` entry and promotes status to `degraded`.
    pub agent_timeout_secs: u64,
    pub chunk_max_tokens: usize,
    pub ollama_url: String,
    pub ollama_model: String,
    pub openai_compatible_base_url: String,
    pub openai_compatible_model: String,
    pub openai_compatible_api_key_env: String,
    pub mistral_model: String,
    pub language: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SearchConfig {
    pub engine: String,
    pub qmd_collection: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DailyNotesConfig {
    pub enabled: bool,
    pub path: PathBuf,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SecurityConfig {
    pub allowed_audio_dirs: Vec<PathBuf>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct WatchConfig {
    pub paths: Vec<PathBuf>,
    pub extensions: Vec<String>,
    pub r#type: String,
    pub diarize: bool,
    pub delete_source: bool,
    pub settle_delay_ms: u64,
    /// Files shorter than this duration route as Memo (skip diarization).
    /// Set to 0 to disable duration-based routing (use `type` config instead).
    pub dictation_threshold_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ScreenContextConfig {
    pub enabled: bool,
    pub interval_secs: u64,
    pub keep_after_summary: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct DesktopContextConfig {
    pub enabled: bool,
    pub capture_window_titles: bool,
    pub capture_browser_context: bool,
    pub allowed_apps: Vec<String>,
    pub denied_apps: Vec<String>,
    // Domain allow/deny lists were removed in the #295-era drift sweep: the
    // fields were parsed and settable but enforced nowhere (browser capture
    // is window-title-only in v1; URLs and domains are never collected).
    // Reintroduce together with actual enforcement if browser-context
    // capture ever graduates.
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PrivacyConfig {
    pub hide_from_screen_share: bool,
}

/// Consent affordance for meeting capture.
///
/// Minutes records full local transcripts; some meeting contexts require
/// participant notice before capture. This config controls the pre-record
/// reminder/gate and the disclosure script. It is a privacy aid, not a
/// determination about requirements.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ConsentConfig {
    /// off | remind | require. Default `remind`.
    pub mode: ConsentMode,
    /// One-line script the user can read aloud or paste before recording.
    pub disclosure_script: String,
    /// Optional default basis stamped into frontmatter when the user does not
    /// pass one, such as a team with notice baked into every calendar invite.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_basis: Option<String>,
}

/// Pre-record consent prompt behavior.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ConsentMode {
    Off,
    #[default]
    Remind,
    Require,
}

/// Retention policy for raw audio artifacts.
///
/// Product stance: markdown transcripts and structured memory are the durable
/// library. Raw audio is a temporary recovery/reprocessing layer unless a user
/// explicitly pins it.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RetentionConfig {
    /// Keep successful recording audio for this many days by default.
    pub successful_audio_days: u32,
    /// Keep failed/needs-review audio longer so the user can recover it.
    pub failed_audio_days: u32,
    /// Honor `audio_retention: pinned` in meeting frontmatter.
    pub keep_pinned_audio: bool,
    /// Whether future cleanup runners may apply the policy automatically.
    ///
    /// The current implementation only previews cleanup candidates; destructive
    /// apply paths must opt in explicitly.
    pub auto_cleanup: bool,
    /// Whether startup is allowed to trigger automatic cleanup.
    pub cleanup_on_startup: bool,
    /// Surface a storage warning when raw audio exceeds this many GiB.
    pub warn_above_gb: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AssistantConfig {
    pub agent: String,
    pub agent_args: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CalendarConfig {
    pub enabled: bool,
}

impl Default for CalendarConfig {
    fn default() -> Self {
        Self { enabled: true }
    }
}

impl Default for PrivacyConfig {
    fn default() -> Self {
        Self {
            hide_from_screen_share: true,
        }
    }
}

impl Default for ConsentConfig {
    fn default() -> Self {
        Self {
            mode: ConsentMode::Remind,
            disclosure_script: "Heads up — I'm using Minutes to transcribe this conversation locally on my device for my own notes. Let me know if you'd prefer I didn't.".into(),
            default_basis: None,
        }
    }
}

impl Default for RetentionConfig {
    fn default() -> Self {
        Self {
            successful_audio_days: 30,
            failed_audio_days: 90,
            keep_pinned_audio: true,
            auto_cleanup: false,
            cleanup_on_startup: false,
            warn_above_gb: 2,
        }
    }
}

impl Default for DesktopContextConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            capture_window_titles: true,
            capture_browser_context: false,
            allowed_apps: vec![],
            denied_apps: vec![],
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct CallDetectionConfig {
    pub enabled: bool,
    pub poll_interval_secs: u64,
    pub cooldown_minutes: u64,
    pub apps: Vec<String>,
    /// When a call that the detector started a recording for ends, show a
    /// countdown banner that auto-stops the recording. Default: false.
    /// Named for the behavior (an assistive prompt, not a silent hard stop);
    /// people often keep recording 30-90s past hangup for takeaways.
    pub stop_when_call_ends: bool,
    /// Seconds the user has to cancel auto-stop before it fires.
    /// Only meaningful when `stop_when_call_ends` is true. Default: 30.
    pub call_end_stop_countdown_secs: u64,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct IdentityConfig {
    pub name: Option<String>,
    /// Single primary email. Retained for backwards compatibility with
    /// existing `config.toml` files. New configs should prefer `emails`.
    pub email: Option<String>,
    /// All email addresses the user sends from. Folded onto the canonical
    /// person entity so calendar attendees arriving as
    /// `you@work.com`/`you@personal.com` don't spawn duplicate people.
    pub emails: Vec<String>,
    /// Alternate name forms (nicknames, formal variants) for the user.
    /// Example: name="Mat", aliases=["Mathieu", "Matthew"]. Used for the
    /// same fold as `emails` so `mathieu@x.com` → `Mathieu` → canonical
    /// `Mat`.
    pub aliases: Vec<String>,
}

impl IdentityConfig {
    /// Every string that should map to the user's canonical entity:
    /// the legacy `email`, all `emails`, and all `aliases`. Duplicates
    /// and empties are filtered. Case-insensitive de-duplication.
    pub fn all_user_aliases(&self) -> Vec<String> {
        let mut seen = std::collections::HashSet::new();
        let mut out = Vec::new();
        let mut push = |s: &str| {
            let trimmed = s.trim();
            if trimmed.is_empty() {
                return;
            }
            if seen.insert(trimmed.to_ascii_lowercase()) {
                out.push(trimmed.to_string());
            }
        };
        if let Some(email) = &self.email {
            push(email);
        }
        for email in &self.emails {
            push(email);
        }
        for alias in &self.aliases {
            push(alias);
        }
        out
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DictationConfig {
    pub backend: String,
    pub destination: String,
    pub accumulate: bool,
    pub daily_note_log: bool,
    pub cleanup_engine: String,
    pub auto_paste: bool,
    pub auto_paste_restore: bool,
    pub silence_timeout_ms: u64,
    pub max_utterance_secs: u64,
    pub destination_file: String,
    pub destination_command: String,
    pub model: String,
    pub shortcut_enabled: bool,
    pub shortcut: String,
    pub hotkey_enabled: bool,
    pub hotkey_keycode: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct VaultConfig {
    pub enabled: bool,
    /// Root path of the markdown vault (e.g., ~/Documents/life)
    pub path: PathBuf,
    /// Subdirectory inside vault where meetings are placed (e.g., "areas/meetings")
    pub meetings_subdir: String,
    /// Sync strategy: "auto", "symlink", "copy", or "direct"
    pub strategy: String,
}

impl Default for DictationConfig {
    fn default() -> Self {
        Self {
            backend: "whisper".into(),
            destination: "clipboard".into(),
            accumulate: true,
            daily_note_log: true,
            cleanup_engine: String::new(),
            auto_paste: false,
            auto_paste_restore: true,
            silence_timeout_ms: 2000,
            max_utterance_secs: 120,
            destination_file: String::new(),
            destination_command: String::new(),
            model: "base".into(),
            shortcut_enabled: false,
            shortcut: "CmdOrCtrl+Shift+Space".into(),
            hotkey_enabled: false,
            hotkey_keycode: 57, // Caps Lock
        }
    }
}

impl Default for VaultConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            path: PathBuf::new(),
            meetings_subdir: "areas/meetings".into(),
            strategy: "auto".into(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RecordingConfig {
    /// Seconds of continuous silence before sending a reminder notification.
    /// Set to 0 to disable. Default: 300 (5 minutes).
    pub silence_reminder_secs: u64,
    /// Audio level (0–100) below which audio is considered silence.
    /// The level comes from RMS energy of the mic input. Default: 3.
    pub silence_threshold: u32,
    /// Seconds of continuous silence before auto-stopping the recording.
    /// Set to 0 to disable. Default: 1800 (30 minutes).
    pub silence_auto_stop_secs: u64,
    /// Maximum recording duration in seconds. Auto-stops at this limit.
    /// Set to 0 to disable. Default: 28800 (8 hours).
    pub max_duration_secs: u64,
    /// Minimum free disk space (MB) before auto-stopping. Set to 0 to disable.
    /// Default: 500.
    pub min_disk_space_mb: u64,
    /// Audio input device name override. When set, Minutes uses this device
    /// instead of the system default. Use `minutes devices` to list available names.
    pub device: Option<String>,
    /// Automatically infer call intent when a known call app is detected.
    /// Default: false. Process-based detection has high false-positive rates
    /// (e.g. Zoom running but not in a call). Users trigger call capture
    /// explicitly via the call detection banner instead.
    pub auto_call_intent: bool,
    /// Allow Minutes to start a call capture even when the selected input
    /// looks like a plain microphone rather than a system-audio route.
    pub allow_degraded_call_capture: bool,
    /// System-audio capture backend. "cpal" keeps the current loopback-device
    /// path. "core-audio-tap" opts into Apple's macOS Process Tap backend.
    pub capture_backend: String,
    /// Multi-source capture: explicit voice + call device names.
    /// When set, `device` is ignored. CLI `--source` flags override this.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sources: Option<SourcesConfig>,
}

/// Multi-source capture configuration.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SourcesConfig {
    /// Voice (microphone) device name, or "default" for system default.
    pub voice: Option<String>,
    /// Call (system audio) device name, or "auto" to detect loopback devices.
    pub call: Option<String>,
}

impl Default for RecordingConfig {
    fn default() -> Self {
        Self {
            silence_reminder_secs: 300,
            silence_threshold: 3,
            silence_auto_stop_secs: 1800,
            max_duration_secs: 28800,
            min_disk_space_mb: 500,
            device: None,
            auto_call_intent: false,
            allow_degraded_call_capture: false,
            capture_backend: "cpal".into(),
            sources: None,
        }
    }
}

/// Knowledge base integration — Karpathy-style LLM wiki maintained from meeting data.
/// After each meeting, extract facts about people and decisions, update person profiles,
/// append to a chronological log, and maintain an index. Opt-in (disabled by default).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct KnowledgeConfig {
    /// Enable knowledge base updates after each meeting.
    pub enabled: bool,
    /// Root path of the knowledge base (e.g., ~/wiki or ~/Documents/life).
    pub path: PathBuf,
    /// Output adapter: "wiki" (flat markdown), "para" (PARA areas/people/), "obsidian" (wiki + [[links]]).
    pub adapter: String,
    /// Fact extraction engine: "agent" (shells out to agent_command), "ollama", "none" (structured data only).
    /// "none" extracts only from YAML frontmatter (decisions, action_items, entities) — no LLM call, zero hallucination risk.
    pub engine: String,
    /// Agent CLI to invoke for extraction. Default: "claude".
    pub agent_command: String,
    /// Chronological append-only log filename inside knowledge path.
    pub log_file: String,
    /// Content-oriented index filename inside knowledge path.
    pub index_file: String,
    /// Minimum confidence for facts to be written. "explicit" (safest), "strong", "inferred", "tentative".
    /// Facts below this threshold are logged but not written to person profiles.
    pub min_confidence: String,
}

impl Default for KnowledgeConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            path: PathBuf::new(),
            adapter: "wiki".into(),
            engine: "none".into(),
            agent_command: "claude".into(),
            log_file: "log.md".into(),
            index_file: "index.md".into(),
            min_confidence: "strong".into(),
        }
    }
}

/// Hooks configuration — shell commands triggered by pipeline events.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct HooksConfig {
    /// Shell command to run after a recording is processed.
    /// The transcript file path is appended as the last argument.
    /// Example: "/path/to/script.sh" → executed as: /path/to/script.sh /path/to/meeting.md
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub post_record: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct LiveTranscriptConfig {
    /// Standalone live transcript backend selection.
    ///
    /// - `"inherit"` (default): follow `transcription.engine`
    /// - `"whisper"`: force Whisper for standalone live transcript
    /// - `"parakeet"`: force Parakeet for standalone live transcript
    /// - `"apple-speech"`: experimental macOS standalone-live-only path
    pub backend: String,
    /// Whisper model to use for live transcription.
    /// Empty string means "use the dictation model".
    pub model: String,
    /// Maximum utterance length in seconds before force-finalizing.
    pub max_utterance_secs: u64,
    /// Whether to save raw WAV alongside JSONL for post-meeting reprocessing.
    pub save_wav: bool,
    /// Whether the keyboard shortcut is enabled.
    pub shortcut_enabled: bool,
    /// The keyboard shortcut string (e.g., "CmdOrCtrl+Shift+L").
    pub shortcut: String,
}

impl Default for LiveTranscriptConfig {
    fn default() -> Self {
        Self {
            backend: LIVE_TRANSCRIPT_BACKEND_INHERIT.into(),
            model: String::new(), // empty = use dictation model
            max_utterance_secs: 30,
            save_wav: true,
            shortcut_enabled: false,
            shortcut: "CmdOrCtrl+Shift+L".into(),
        }
    }
}

pub const LIVE_TRANSCRIPT_BACKEND_INHERIT: &str = "inherit";
pub const VALID_LIVE_TRANSCRIPT_BACKENDS: &[&str] = &[
    LIVE_TRANSCRIPT_BACKEND_INHERIT,
    "whisper",
    "parakeet",
    "apple-speech",
];

impl Default for ScreenContextConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            interval_secs: 30,
            keep_after_summary: false,
        }
    }
}

impl Default for AssistantConfig {
    fn default() -> Self {
        Self {
            agent: "claude".into(),
            agent_args: vec![],
        }
    }
}

impl Default for CallDetectionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            poll_interval_secs: 1,
            cooldown_minutes: 5,
            apps: vec!["zoom.us".into(), "Microsoft Teams".into(), "Webex".into()],
            stop_when_call_ends: false,
            call_end_stop_countdown_secs: 30,
        }
    }
}

/// Deserialize the sidecar tri-state: bool `true`/`"on"` => forced on,
/// `"off"` => forced off, `"auto"` => auto, legacy bool `false` => auto (#295).
fn de_sidecar_tristate<'de, D>(deserializer: D) -> Result<Option<bool>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Raw {
        Bool(bool),
        Text(String),
    }
    match Option::<Raw>::deserialize(deserializer)? {
        None => Ok(None),
        Some(Raw::Bool(true)) => Ok(Some(true)),
        // Legacy serializer artifact: every pre-0.18.8 desktop save wrote
        // `parakeet_sidecar_enabled = false` while the key had no UI.
        Some(Raw::Bool(false)) => Ok(None),
        Some(Raw::Text(text)) => match text.trim().to_ascii_lowercase().as_str() {
            "" | "auto" => Ok(None),
            "on" | "true" => Ok(Some(true)),
            "off" | "false" => Ok(Some(false)),
            other => Err(serde::de::Error::custom(format!(
                "unknown parakeet_sidecar_enabled '{other}'. Valid: auto, on, off"
            ))),
        },
    }
}

/// Serialize the sidecar tri-state: forced on as bool `true` (matches docs),
/// forced off as the string `"off"` so it can never be mistaken for the
/// legacy serializer artifact. `None` is skipped entirely.
fn ser_sidecar_tristate<S>(value: &Option<bool>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: serde::Serializer,
{
    match value {
        Some(true) => serializer.serialize_bool(true),
        Some(false) => serializer.serialize_str("off"),
        None => serializer.serialize_none(),
    }
}

// ── Defaults ─────────────────────────────────────────────────

fn home_dir() -> PathBuf {
    // Check env vars directly first — dirs::home_dir() on Windows uses
    // SHGetKnownFolderPath which ignores runtime env var changes, breaking
    // test isolation via with_temp_home().
    if let Some(home) = std::env::var_os("HOME") {
        return PathBuf::from(home);
    }
    #[cfg(windows)]
    if let Some(up) = std::env::var_os("USERPROFILE") {
        return PathBuf::from(up);
    }
    dirs::home_dir().unwrap_or_else(|| PathBuf::from("/tmp"))
}

fn minutes_dir() -> PathBuf {
    home_dir().join(".minutes")
}

fn config_base_dir_from(xdg_config_home: Option<OsString>, home: PathBuf) -> PathBuf {
    match xdg_config_home {
        Some(path) if !path.is_empty() => PathBuf::from(path),
        _ => home.join(".config"),
    }
}

fn config_base_dir() -> PathBuf {
    config_base_dir_from(std::env::var_os("XDG_CONFIG_HOME"), home_dir())
}

#[cfg(test)]
fn config_path_from(xdg_config_home: Option<OsString>, home: PathBuf) -> PathBuf {
    config_base_dir_from(xdg_config_home, home)
        .join("minutes")
        .join("config.toml")
}

impl Default for Config {
    fn default() -> Self {
        Self {
            output_dir: home_dir().join("meetings"),
            transcription: TranscriptionConfig::default(),
            diarization: DiarizationConfig::default(),
            summarization: SummarizationConfig::default(),
            search: SearchConfig::default(),
            daily_notes: DailyNotesConfig::default(),
            security: SecurityConfig::default(),
            watch: WatchConfig::default(),
            assistant: AssistantConfig::default(),
            privacy: PrivacyConfig::default(),
            consent: ConsentConfig::default(),
            screen_context: ScreenContextConfig::default(),
            desktop_context: DesktopContextConfig::default(),
            calendar: CalendarConfig::default(),
            call_detection: CallDetectionConfig::default(),
            identity: IdentityConfig::default(),
            vault: VaultConfig::default(),
            dictation: DictationConfig::default(),
            voice: VoiceConfig::default(),
            live_transcript: LiveTranscriptConfig::default(),
            recording: RecordingConfig::default(),
            retention: RetentionConfig::default(),
            hooks: HooksConfig::default(),
            knowledge: KnowledgeConfig::default(),
            palette: PaletteConfig::default(),
        }
    }
}

impl Default for TranscriptionConfig {
    fn default() -> Self {
        Self {
            engine: "whisper".into(),
            model: "small".into(),
            model_path: minutes_dir().join("models"),
            min_words: 3,
            language: None,
            vad_model: "silero-v6.2.0".into(),
            vad_engine: "ort-silero".into(),
            noise_reduction: true,
            parakeet_binary: "parakeet".into(),
            parakeet_model: "tdt-600m".into(),
            parakeet_boost_limit: 0,
            parakeet_boost_score: 2.0,
            parakeet_fp16: true,
            parakeet_sidecar_enabled: None,
            parakeet_fp16_blacklist_reset: false,
            parakeet_vocab: "tdt-600m.tokenizer.vocab".into(),
            partial_max_secs: 30,
        }
    }
}

impl Default for DiarizationConfig {
    fn default() -> Self {
        Self {
            engine: "auto".into(),
            model_path: minutes_dir().join("models").join("diarization"),
            threshold: 0.4,
            embedding_model: "cam++".into(),
            stem_correlation_threshold: 0.85,
        }
    }
}

impl Default for SummarizationConfig {
    fn default() -> Self {
        Self {
            engine: "none".into(),
            agent_command: "claude".into(),
            agent_timeout_secs: 300,
            chunk_max_tokens: 4000,
            ollama_url: "http://localhost:11434".into(),
            ollama_model: "llama3.2".into(),
            openai_compatible_base_url: "http://localhost:11434/v1".into(),
            openai_compatible_model: "llama3.2".into(),
            openai_compatible_api_key_env: String::new(),
            mistral_model: "mistral-large-latest".into(),
            language: "auto".into(),
        }
    }
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            engine: "builtin".into(),
            qmd_collection: None,
        }
    }
}

impl Default for DailyNotesConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            path: home_dir().join("meetings").join("daily"),
        }
    }
}

// SecurityConfig::default() derives empty vec for allowed_audio_dirs.
// Empty = allow all paths (permissive default for local CLI use).
// Set explicitly in config.toml for MCP/networked use:
//   allowed_audio_dirs = ["~/.minutes/inbox", "~/meetings"]

impl Default for WatchConfig {
    fn default() -> Self {
        Self {
            paths: vec![minutes_dir().join("inbox")],
            extensions: vec![
                "m4a".into(),
                "wav".into(),
                "mp3".into(),
                "ogg".into(),
                "webm".into(),
            ],
            r#type: "memo".into(),
            diarize: false,
            delete_source: false,
            settle_delay_ms: 2000,
            dictation_threshold_secs: 120,
        }
    }
}

// ── Loading ──────────────────────────────────────────────────

impl Config {
    /// Effective backend for the standalone live transcript path.
    ///
    /// `live_transcript.backend = "inherit"` follows `transcription.engine`,
    /// except for the legacy `transcription.engine = "apple-speech"` case,
    /// which older configs used to express the standalone-live-only Apple
    /// experiment. We keep honoring that value here so non-Tauri consumers
    /// preserve behavior even before the migration has rewritten the file.
    pub fn effective_live_transcript_backend(&self) -> &str {
        let backend = self.live_transcript.backend.trim();
        if backend.is_empty() || backend == LIVE_TRANSCRIPT_BACKEND_INHERIT {
            if self
                .transcription
                .engine
                .eq_ignore_ascii_case("apple-speech")
            {
                "apple-speech"
            } else {
                &self.transcription.engine
            }
        } else {
            &self.live_transcript.backend
        }
    }

    pub fn standalone_live_backend_setting(&self) -> &str {
        let backend = self.live_transcript.backend.trim();
        if backend.is_empty() {
            LIVE_TRANSCRIPT_BACKEND_INHERIT
        } else {
            &self.live_transcript.backend
        }
    }

    /// Standard config file location.
    pub fn config_path() -> PathBuf {
        config_base_dir().join("minutes").join("config.toml")
    }

    /// Load config from file, falling back to defaults.
    /// If the config file doesn't exist, returns defaults silently.
    /// If the config file exists but is invalid, logs a warning and returns defaults.
    pub fn load() -> Self {
        let path = Self::config_path();
        Self::load_from(&path)
    }

    /// Load the config file with first-run and upgrade migrations applied.
    ///
    /// This is the entry point the Tauri desktop app uses at startup. The
    /// CLI and non-app consumers can continue to use [`Self::load`] if they
    /// do not need migration side effects.
    ///
    /// Currently runs:
    /// - **Palette section persistence**: if the config file exists but
    ///   has no `[palette]` section, write the section out at its default
    ///   values so the user has a discoverable surface for the new
    ///   command palette. The compiled defaults enable the shortcut on
    ///   both fresh installs and upgrades. The Tauri desktop app fires a
    ///   one-shot system notification on the first launch that registers
    ///   the new shortcut so VS Code / JetBrains / Firefox users who
    ///   already have `⌘⇧K` bound aren't silently hijacked — they're
    ///   informed and can disable the shortcut from the settings UI in
    ///   one click.
    ///
    /// Note: an earlier draft of this migration force-disabled the
    /// shortcut on upgrade. That made the feature undiscoverable
    /// because the only way to enable it was to hand-edit `config.toml`,
    /// which (per dogfood feedback) nobody does. The current design
    /// prefers discoverability + a visible escape hatch over silent
    /// caution. The first-run notification + the settings UI panel are
    /// the consent mechanism, not opt-in defaults.
    ///
    /// Fresh installs (file does not exist) skip every migration and
    /// take the compiled defaults verbatim — the desktop app will
    /// create the config later via `cmd_set_setting` if the user
    /// changes anything.
    pub fn load_with_migrations() -> Self {
        let path = Self::config_path();
        Self::load_with_migrations_from(&path)
    }

    /// Testable form of [`Self::load_with_migrations`]. Reads from
    /// `path`, runs migrations, and writes the migrated config back if
    /// it changed.
    pub fn load_with_migrations_from(path: &Path) -> Self {
        let file_existed = path.exists();
        let raw_toml = if file_existed {
            std::fs::read_to_string(path).ok()
        } else {
            None
        };
        let raw_compat = raw_toml
            .as_deref()
            .map(inspect_raw_toml_compat)
            .unwrap_or_default();

        let mut config = Self::load_from(path);
        let mut migrated_toml: Option<String> = None;

        // Apple Speech migration: older configs overloaded
        // `transcription.engine = "apple-speech"` to mean "standalone live
        // transcript should try Apple Speech". That was always a product/model
        // mismatch because batch and recording-sidecar flows never actually
        // used Apple Speech. Normalize those existing configs to:
        //
        //   [transcription]
        //   engine = "whisper"
        //
        //   [live_transcript]
        //   backend = "apple-speech"
        //
        // We intentionally persist the migrated config back to disk so the
        // desktop app stops carrying the old overload forward after the first
        // upgraded launch.
        if file_existed
            && config
                .transcription
                .engine
                .eq_ignore_ascii_case("apple-speech")
            && raw_toml.as_deref().is_some_and(|raw| {
                !raw_toml_has_setting_in_section(raw, "live_transcript", "backend")
            })
        {
            config.transcription.engine = "whisper".into();
            config.live_transcript.backend = "apple-speech".into();
            migrated_toml = toml::to_string_pretty(&config).ok();
            tracing::info!(
                "live transcript backend migration: moved legacy apple-speech engine setting into [live_transcript].backend at {}",
                path.display()
            );
        }

        // Summarization upgrade safety:
        //
        // 1. Preserve the historical `"auto"` engine for existing sparse
        //    configs that never wrote `[summarization].engine`. This stays
        //    in-memory so we do not force-rewrite otherwise healthy files and
        //    drop comments / unknown keys just to preserve legacy behavior.
        // 2. Clear the desktop-only key env marker out of shared config files.
        if file_existed {
            if raw_compat.preserve_legacy_auto_summarization {
                tracing::info!(
                    "summarization migration: preserving legacy auto engine for sparse config at {}",
                    path.display()
                );
            }

            if raw_compat.clear_desktop_openai_compatible_env_marker {
                tracing::info!(
                    "summarization migration: clearing desktop-only key env marker from shared config at {}",
                    path.display()
                );
                migrated_toml = toml::to_string_pretty(&config).ok();
            }
        }

        // Palette section persistence: if the config file exists but
        // has no `[palette]` section, write the default section out
        // verbatim. We do NOT flip `shortcut_enabled` away from its
        // default — see the doc comment on `load_with_migrations` for
        // why opt-in-on-upgrade is the wrong default.
        //
        // The point of this branch is to make the section visible in
        // the user's `config.toml` so they can find it next time they
        // open the file, AND to give the desktop app's first-run
        // notification logic a stable place to know "the user has now
        // seen this section persisted." `toml::from_str` silently
        // fills missing sections with `Default`, so the parsed struct
        // alone cannot distinguish "user opted out" from "field never
        // seen" — only a text check on the raw TOML can.
        if file_existed && migrated_toml.is_none() {
            if let Some(raw) = raw_toml.as_deref() {
                if !raw_toml_has_section(raw, "palette") {
                    migrated_toml = Some(append_palette_section(raw, &config.palette));
                    tracing::info!(
                        "palette migration: persisting [palette] section in existing config at {}",
                        path.display()
                    );
                }
            }
        }

        if let Some(migrated_toml) = migrated_toml {
            if let Err(e) = std::fs::write(path, migrated_toml) {
                tracing::warn!(
                    "failed to persist config migration to {}: {}",
                    path.display(),
                    e
                );
            }
        }

        config
    }

    /// Load config from a specific path. Used for testing and by
    /// [`Self::load_with_migrations_from`].
    pub fn load_from(path: &Path) -> Self {
        if !path.exists() {
            return Self::default();
        }

        match std::fs::read_to_string(path) {
            Ok(contents) => match toml::from_str(&contents) {
                Ok(mut config) => {
                    apply_raw_toml_compat(&mut config, inspect_raw_toml_compat(&contents));
                    config
                }
                Err(e) => {
                    tracing::warn!(
                        "invalid config at {}: {}. Using defaults.",
                        path.display(),
                        e
                    );
                    Self::default()
                }
            },
            Err(e) => {
                tracing::warn!(
                    "could not read config at {}: {}. Using defaults.",
                    path.display(),
                    e
                );
                Self::default()
            }
        }
    }

    /// Save config to the standard config file location.
    /// Creates the config directory and file if they don't exist.
    pub fn save(&self) -> std::io::Result<()> {
        let path = Self::config_path();
        Self::save_to(self, &path)
    }

    /// Save config to a specific path.
    pub fn save_to(&self, path: &Path) -> std::io::Result<()> {
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let contents = toml::to_string_pretty(self)
            .map_err(|e| std::io::Error::other(format!("TOML serialize: {}", e)))?;
        std::fs::write(path, contents)?;
        tracing::info!(path = %path.display(), "config saved");
        Ok(())
    }

    /// Ensure required directories exist.
    pub fn ensure_dirs(&self) -> std::io::Result<()> {
        std::fs::create_dir_all(&self.output_dir)?;
        std::fs::create_dir_all(self.output_dir.join("memos"))?;
        if self.daily_notes.enabled {
            std::fs::create_dir_all(&self.daily_notes.path)?;
        }
        std::fs::create_dir_all(minutes_dir())?;
        std::fs::create_dir_all(minutes_dir().join("inbox"))?;
        std::fs::create_dir_all(minutes_dir().join("inbox").join("processed"))?;
        std::fs::create_dir_all(minutes_dir().join("inbox").join("failed"))?;
        std::fs::create_dir_all(minutes_dir().join("logs"))?;

        // Block macOS Spotlight from indexing sensitive transcript data
        for dir in [&self.output_dir, &minutes_dir()] {
            let marker = dir.join(".metadata_never_index");
            if !marker.exists() {
                std::fs::write(&marker, "").ok();
            }
        }

        Ok(())
    }

    /// Path to the minutes state directory (~/.minutes/).
    pub fn minutes_dir() -> PathBuf {
        minutes_dir()
    }
}

#[derive(Debug, Clone, Copy, Default)]
struct RawTomlCompat {
    preserve_legacy_auto_summarization: bool,
    clear_desktop_openai_compatible_env_marker: bool,
}

fn inspect_raw_toml_compat(raw: &str) -> RawTomlCompat {
    RawTomlCompat {
        preserve_legacy_auto_summarization: !raw_toml_has_setting_in_section(
            raw,
            "summarization",
            "engine",
        ),
        clear_desktop_openai_compatible_env_marker: raw_toml_setting_equals_in_section(
            raw,
            "summarization",
            "openai_compatible_api_key_env",
            OPENAI_COMPATIBLE_DESKTOP_API_KEY_ENV,
        ),
    }
}

fn apply_raw_toml_compat(config: &mut Config, compat: RawTomlCompat) {
    if compat.preserve_legacy_auto_summarization {
        config.summarization.engine = "auto".into();
    }
    if compat.clear_desktop_openai_compatible_env_marker {
        config.summarization.openai_compatible_api_key_env.clear();
    }
}

pub fn openai_compatible_base_url_is_local(base_url: &str) -> bool {
    let trimmed = base_url.trim();
    if trimmed.is_empty() {
        return false;
    }

    let without_scheme = trimmed
        .split_once("://")
        .map(|(_, rest)| rest)
        .unwrap_or(trimmed);
    let authority = without_scheme.split('/').next().unwrap_or(without_scheme);
    let host_port = authority.rsplit('@').next().unwrap_or(authority);
    let host = if let Some(stripped) = host_port.strip_prefix('[') {
        stripped.split(']').next().unwrap_or(stripped)
    } else {
        host_port.split(':').next().unwrap_or(host_port)
    };

    matches!(
        host.to_ascii_lowercase().as_str(),
        "localhost" | "127.0.0.1" | "0.0.0.0" | "::1"
    )
}

/// Return `true` iff the raw TOML text contains a top-level `[section]`
/// header. This is a deliberately primitive text check — we cannot use
/// `toml::from_str` to answer this question because serde's `#[serde(default)]`
/// silently fills missing sections with their default values, so a parsed
/// struct never tells you whether a key was present in the file.
///
/// The check:
/// - Ignores leading whitespace
/// - Skips lines that start with `#` (comments)
/// - Does not try to understand inline tables, dotted keys, or array tables
///   like `[[section]]` — those are not how Minutes writes its config, and
///   `toml::to_string_pretty` always emits bare `[section]` headers for the
///   sections we own
fn raw_toml_has_section(raw: &str, section: &str) -> bool {
    let target = format!("[{}]", section);
    for line in raw.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }
        if trimmed == target {
            return true;
        }
    }
    false
}

fn raw_toml_has_setting_in_section(raw: &str, section: &str, key: &str) -> bool {
    let target = format!("[{}]", section);
    let key_prefix = format!("{} =", key);
    let mut in_section = false;
    for line in raw.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }
        if trimmed.starts_with('[') {
            in_section = trimmed == target;
            continue;
        }
        if in_section && trimmed.starts_with(&key_prefix) {
            return true;
        }
    }
    false
}

fn raw_toml_setting_equals_in_section(raw: &str, section: &str, key: &str, expected: &str) -> bool {
    let target = format!("[{}]", section);
    let key_prefix = format!("{} =", key);
    let expected_value = toml::Value::String(expected.to_string()).to_string();
    let mut in_section = false;
    for line in raw.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with('#') {
            continue;
        }
        if trimmed.starts_with('[') {
            in_section = trimmed == target;
            continue;
        }
        if in_section && trimmed.starts_with(&key_prefix) {
            return trimmed[key_prefix.len()..].trim() == expected_value;
        }
    }
    false
}

fn append_palette_section(raw: &str, palette: &PaletteConfig) -> String {
    let mut output = raw.trim_end_matches('\n').to_string();
    if !output.is_empty() {
        output.push_str("\n\n");
    }
    output.push_str("[palette]\n");
    output.push_str(&format!(
        "shortcut_enabled = {}\n",
        palette.shortcut_enabled
    ));
    output.push_str(&format!(
        "shortcut = {}\n",
        toml::Value::String(palette.shortcut.clone())
    ));
    output
}

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

    #[test]
    fn default_config_is_valid() {
        let config = Config::default();
        assert_eq!(config.transcription.engine, "whisper");
        assert_eq!(
            config.live_transcript.backend,
            LIVE_TRANSCRIPT_BACKEND_INHERIT
        );
        assert_eq!(config.transcription.model, "small");
        assert_eq!(config.transcription.min_words, 3);
        // The recording sidecar's default VAD engine is the streaming
        // ort-Silero impl. The dispatcher falls through to whisper-Silero
        // when the `vad-ort` build feature is off or the ONNX is missing,
        // so users on older builds see no behavior change. Pinning the
        // string here means a future refactor cannot silently revert
        // the default without a failing test.
        assert_eq!(config.transcription.vad_engine, "ort-silero");
        assert_eq!(config.transcription.vad_model, "silero-v6.2.0");
        assert_eq!(config.transcription.parakeet_binary, "parakeet");
        assert_eq!(config.transcription.parakeet_model, "tdt-600m");
        assert_eq!(config.transcription.parakeet_boost_limit, 0);
        assert_eq!(config.transcription.parakeet_boost_score, 2.0);
        assert!(config.transcription.parakeet_fp16);
        assert!(config.transcription.parakeet_sidecar_enabled.is_none());
        assert_eq!(
            config.transcription.parakeet_vocab,
            "tdt-600m.tokenizer.vocab"
        );
        assert_eq!(config.diarization.engine, "auto");
        assert_eq!(config.summarization.engine, "none");
        assert_eq!(config.search.engine, "builtin");
        assert!(!config.daily_notes.enabled);
        assert_eq!(config.dictation.backend, "whisper");
        assert!(config.dictation.accumulate);
        assert!(config.call_detection.enabled);
        assert_eq!(config.watch.settle_delay_ms, 2000);
        assert!(!config.watch.extensions.is_empty());
        assert!(!config.recording.auto_call_intent);
        assert!(!config.recording.allow_degraded_call_capture);
        assert_eq!(config.recording.capture_backend, "cpal");
        assert_eq!(config.consent.mode, ConsentMode::Remind);
        assert!(config.consent.default_basis.is_none());
        assert!(config.consent.disclosure_script.contains("Minutes"));
    }

    #[test]
    fn consent_config_deserializes_modes_and_defaults() {
        let parsed: Config = toml::from_str(
            r#"
            [consent]
            mode = "require"
            disclosure_script = "Please acknowledge recording."
            default_basis = "notice_in_invite"
            "#,
        )
        .unwrap();

        assert_eq!(parsed.consent.mode, ConsentMode::Require);
        assert_eq!(
            parsed.consent.disclosure_script,
            "Please acknowledge recording."
        );
        assert_eq!(
            parsed.consent.default_basis.as_deref(),
            Some("notice_in_invite")
        );
    }

    #[test]
    fn missing_consent_config_uses_defaults() {
        let parsed: Config = toml::from_str("").unwrap();

        assert_eq!(parsed.consent.mode, ConsentMode::Remind);
        assert!(parsed.consent.default_basis.is_none());
    }

    #[test]
    fn missing_config_file_returns_defaults() {
        let config = Config::load_from(Path::new("/nonexistent/config.toml"));
        assert_eq!(config.transcription.model, "small");
    }

    #[test]
    fn config_path_falls_back_to_home_dot_config_when_xdg_unset() {
        let home = PathBuf::from("/tmp/test-home");
        let path = config_path_from(None, home.clone());

        assert_eq!(path, home.join(".config/minutes/config.toml"));
    }

    #[test]
    fn config_path_uses_xdg_config_home_when_set() {
        let path = config_path_from(
            Some(OsString::from("/tmp/test-config")),
            PathBuf::from("/tmp/test-home"),
        );

        assert_eq!(path, PathBuf::from("/tmp/test-config/minutes/config.toml"));
    }

    #[test]
    fn config_path_falls_back_when_xdg_config_home_is_empty() {
        let home = PathBuf::from("/tmp/test-home");
        let path = config_path_from(Some(OsString::new()), home.clone());

        assert_eq!(path, home.join(".config/minutes/config.toml"));
    }

    #[test]
    fn partial_config_merges_with_defaults() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "large-v3"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.model, "large-v3");
        // Other fields should be defaults
        assert_eq!(config.transcription.min_words, 3);
        assert_eq!(config.diarization.engine, "auto");
        assert!(!config.daily_notes.enabled);
        assert!(config.dictation.accumulate);
    }

    #[test]
    fn default_language_is_none() {
        let config = Config::default();
        assert_eq!(config.transcription.language, None);
    }

    #[test]
    fn language_can_be_set_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
language = "es"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.language, Some("es".into()));
    }

    #[test]
    fn omitted_language_defaults_to_none() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "tiny"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.language, None);
    }

    #[test]
    fn invalid_toml_returns_defaults() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(&config_path, "this is not valid toml {{{").unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.model, "small");
        assert!(config.dictation.accumulate);
    }

    #[test]
    fn parakeet_config_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
engine = "parakeet"
parakeet_model = "tdt-600m"
parakeet_binary = "/usr/local/bin/parakeet"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.engine, "parakeet");
        assert_eq!(config.transcription.parakeet_model, "tdt-600m");
        assert_eq!(
            config.transcription.parakeet_binary,
            "/usr/local/bin/parakeet"
        );
        assert!(config.transcription.parakeet_sidecar_enabled.is_none());
        // Other fields should be defaults
        assert_eq!(config.transcription.model, "small");
        assert_eq!(config.transcription.min_words, 3);
    }

    #[test]
    fn omitted_engine_defaults_to_whisper() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "tiny"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.engine, "whisper");
        assert_eq!(config.transcription.parakeet_binary, "parakeet");
    }

    #[test]
    fn effective_live_transcript_backend_inherits_batch_engine_by_default() {
        let mut config = Config::default();
        config.transcription.engine = "parakeet".into();

        assert_eq!(config.standalone_live_backend_setting(), "inherit");
        assert_eq!(config.effective_live_transcript_backend(), "parakeet");
    }

    #[test]
    fn effective_live_transcript_backend_preserves_legacy_apple_engine_configs() {
        let mut config = Config::default();
        config.transcription.engine = "apple-speech".into();

        assert_eq!(config.standalone_live_backend_setting(), "inherit");
        assert_eq!(config.effective_live_transcript_backend(), "apple-speech");
    }

    #[test]
    fn live_transcript_backend_can_be_set_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
engine = "whisper"

[live_transcript]
backend = "apple-speech"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.live_transcript.backend, "apple-speech");
        assert_eq!(config.effective_live_transcript_backend(), "apple-speech");
        assert_eq!(config.transcription.engine, "whisper");
    }

    #[test]
    fn parakeet_sidecar_flag_can_be_enabled_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
parakeet_sidecar_enabled = true
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.transcription.parakeet_sidecar_enabled, Some(true));
    }

    #[test]
    fn parakeet_fp16_blacklist_reset_flag_can_be_enabled_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
parakeet_fp16_blacklist_reset = true
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert!(config.transcription.parakeet_fp16_blacklist_reset);
    }

    #[test]
    fn dictation_accumulate_can_be_disabled_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[dictation]
accumulate = false
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert!(!config.dictation.accumulate);
    }

    #[test]
    fn dictation_backend_can_be_selected_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[dictation]
backend = "apple-speech"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.dictation.backend, "apple-speech");
        assert_eq!(config.transcription.engine, "whisper");
    }

    #[test]
    fn dictation_backend_accepts_parakeet_without_changing_batch_engine() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[dictation]
backend = "parakeet"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.dictation.backend, "parakeet");
        assert_eq!(config.transcription.engine, "whisper");
    }

    // ── Call detection: stop-when-call-ends opt-in ────────────

    #[test]
    fn stop_when_call_ends_is_off_by_default() {
        let config = Config::default();
        assert!(!config.call_detection.stop_when_call_ends);
        assert_eq!(config.call_detection.call_end_stop_countdown_secs, 30);
    }

    #[test]
    fn stop_when_call_ends_round_trips_through_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[call_detection]
enabled = true
stop_when_call_ends = true
call_end_stop_countdown_secs = 45
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert!(config.call_detection.stop_when_call_ends);
        assert_eq!(config.call_detection.call_end_stop_countdown_secs, 45);
        // Sibling fields still populated from defaults.
        assert_eq!(config.call_detection.poll_interval_secs, 1);
        assert!(!config.call_detection.apps.is_empty());
    }

    #[test]
    fn stop_when_call_ends_omitted_keeps_default_off() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[call_detection]
enabled = true
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert!(!config.call_detection.stop_when_call_ends);
        assert_eq!(config.call_detection.call_end_stop_countdown_secs, 30);
    }

    // ── Palette config + upgrade migration ────────────────────

    #[test]
    fn palette_default_is_enabled() {
        let config = Config::default();
        assert!(config.palette.shortcut_enabled);
        assert_eq!(config.palette.shortcut, "CmdOrCtrl+Shift+K");
    }

    #[test]
    fn raw_toml_has_section_matches_top_level_headers() {
        assert!(raw_toml_has_section("[palette]\nx = 1\n", "palette"));
        assert!(raw_toml_has_section("# header\n[palette]", "palette"));
        assert!(raw_toml_has_section(
            "[other]\nx=1\n\n[palette]\ny=2\n",
            "palette"
        ));
    }

    #[test]
    fn raw_toml_has_section_ignores_commented_headers() {
        assert!(!raw_toml_has_section("# [palette]\n", "palette"));
        assert!(!raw_toml_has_section("  # [palette]\n", "palette"));
    }

    #[test]
    fn raw_toml_has_section_rejects_non_matching_sections() {
        assert!(!raw_toml_has_section("[dictation]\n", "palette"));
        assert!(!raw_toml_has_section("[palette.inner]\n", "palette"));
    }

    #[test]
    fn raw_toml_has_setting_in_section_matches_exact_key() {
        let raw = r#"
[live_transcript]
backend = "apple-speech"
shortcut = "CmdOrCtrl+Shift+L"
"#;

        assert!(raw_toml_has_setting_in_section(
            raw,
            "live_transcript",
            "backend"
        ));
        assert!(!raw_toml_has_setting_in_section(
            raw,
            "live_transcript",
            "missing"
        ));
    }

    #[test]
    fn append_palette_section_preserves_existing_text() {
        let raw = "# keep this comment\nunknown_key = 7\n";
        let appended = append_palette_section(raw, &PaletteConfig::default());

        assert!(appended.starts_with("# keep this comment\nunknown_key = 7\n"));
        assert!(raw_toml_has_section(&appended, "palette"));
        assert!(appended.contains("shortcut_enabled = true"));
        assert!(appended.contains("shortcut = \"CmdOrCtrl+Shift+K\""));
    }

    #[test]
    fn fresh_install_keeps_palette_enabled() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        // No file exists — fresh install path.
        let config = Config::load_with_migrations_from(&config_path);
        assert!(
            config.palette.shortcut_enabled,
            "fresh install should default palette shortcut to ENABLED"
        );
        // And the migration should NOT have created a file out of thin air.
        // Fresh installs leave config creation to a later save() call.
        assert!(
            !config_path.exists(),
            "migration should not materialize a config file on fresh install"
        );
    }

    #[test]
    fn upgrade_persists_palette_section_at_default_enabled() {
        // Existing config without a [palette] section: the migration
        // writes the section out at the compiled default, which is
        // ENABLED. Discoverability beats silent opt-out. The desktop
        // app's first-run notification (registered separately on the
        // first launch that sees the migration ran) gives users an
        // explicit consent surface.
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "small"
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert!(
            config.palette.shortcut_enabled,
            "upgrade path should keep palette shortcut ENABLED at the default"
        );

        // The migration should have persisted the section to disk so
        // the user can find it next time they open the file AND so
        // the next load is a stable fixpoint.
        let reloaded = std::fs::read_to_string(&config_path).unwrap();
        assert!(
            raw_toml_has_section(&reloaded, "palette"),
            "migration should persist a [palette] section to disk"
        );
        assert!(
            reloaded.contains("[transcription]\nmodel = \"small\""),
            "migration should preserve existing config text, got:\n{}",
            reloaded
        );
        assert!(
            reloaded.contains("shortcut_enabled = true"),
            "persisted migration must encode shortcut_enabled = true, got:\n{}",
            reloaded
        );

        // Second load must be a stable fixpoint.
        let second = Config::load_with_migrations_from(&config_path);
        assert!(second.palette.shortcut_enabled);
    }

    #[test]
    fn upgrade_respects_explicit_palette_section() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "small"

[palette]
shortcut_enabled = true
shortcut = "CmdOrCtrl+Shift+K"
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert!(
            config.palette.shortcut_enabled,
            "explicit [palette] section must not be overridden by migration"
        );

        // And the on-disk file should be unchanged (no write storm).
        let reloaded = std::fs::read_to_string(&config_path).unwrap();
        assert!(reloaded.contains("shortcut_enabled = true"));
    }

    #[test]
    fn upgrade_respects_user_disabled_palette_section() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[palette]
shortcut_enabled = false
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert!(!config.palette.shortcut_enabled);
    }

    #[test]
    fn upgrade_preserves_comments_and_unknown_keys_when_adding_palette() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            "# top comment\nmystery = \"keep-me\"\n\n[transcription]\nmodel = \"small\"\n",
        )
        .unwrap();

        let _ = Config::load_with_migrations_from(&config_path);
        let reloaded = std::fs::read_to_string(&config_path).unwrap();

        assert!(reloaded.contains("# top comment"));
        assert!(reloaded.contains("mystery = \"keep-me\""));
        assert!(raw_toml_has_section(&reloaded, "palette"));
    }

    #[test]
    fn upgrade_migrates_legacy_apple_engine_into_live_backend() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
engine = "apple-speech"
model = "small"
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert_eq!(config.transcription.engine, "whisper");
        assert_eq!(config.live_transcript.backend, "apple-speech");
        assert_eq!(config.effective_live_transcript_backend(), "apple-speech");

        let reloaded = std::fs::read_to_string(&config_path).unwrap();
        assert!(reloaded.contains("engine = \"whisper\""));
        assert!(reloaded.contains("[live_transcript]"));
        assert!(reloaded.contains("backend = \"apple-speech\""));
    }

    #[test]
    fn upgrade_does_not_override_explicit_live_backend_setting() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
engine = "apple-speech"

[live_transcript]
backend = "whisper"
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert_eq!(config.transcription.engine, "apple-speech");
        assert_eq!(config.live_transcript.backend, "whisper");
        assert_eq!(config.effective_live_transcript_backend(), "whisper");
    }

    #[test]
    fn upgrade_preserves_legacy_auto_summarization_when_engine_is_missing() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "small"

[palette]
shortcut_enabled = true
"#,
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert_eq!(config.summarization.engine, "auto");

        let reloaded = std::fs::read_to_string(&config_path).unwrap();
        assert!(reloaded.contains("[transcription]\nmodel = \"small\""));
        assert!(reloaded.contains("[palette]\nshortcut_enabled = true"));
        assert!(!reloaded.contains("[summarization]"));
    }

    #[test]
    fn load_from_preserves_legacy_auto_summarization_when_engine_is_missing() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[transcription]
model = "small"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.summarization.engine, "auto");
    }

    #[test]
    fn upgrade_clears_desktop_only_openai_compatible_env_marker() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            format!(
                r#"
[summarization]
engine = "openai-compatible"
openai_compatible_base_url = "https://openrouter.ai/api/v1"
openai_compatible_model = "openai/gpt-4o-mini"
openai_compatible_api_key_env = "{}"
"#,
                OPENAI_COMPATIBLE_DESKTOP_API_KEY_ENV
            ),
        )
        .unwrap();

        let config = Config::load_with_migrations_from(&config_path);
        assert!(config
            .summarization
            .openai_compatible_api_key_env
            .is_empty());

        let reloaded = std::fs::read_to_string(&config_path).unwrap();
        assert!(reloaded.contains("openai_compatible_api_key_env = \"\""));
    }

    #[test]
    fn load_from_clears_desktop_only_openai_compatible_env_marker() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            format!(
                r#"
[summarization]
engine = "openai-compatible"
openai_compatible_api_key_env = "{}"
"#,
                OPENAI_COMPATIBLE_DESKTOP_API_KEY_ENV
            ),
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert!(config
            .summarization
            .openai_compatible_api_key_env
            .is_empty());
    }

    #[test]
    fn openai_compatible_base_url_detects_local_hosts() {
        assert!(openai_compatible_base_url_is_local(
            "http://localhost:11434/v1"
        ));
        assert!(openai_compatible_base_url_is_local(
            "http://127.0.0.1:11434/v1"
        ));
        assert!(openai_compatible_base_url_is_local("http://[::1]:11434/v1"));
        assert!(!openai_compatible_base_url_is_local(
            "https://openrouter.ai/api/v1"
        ));
    }

    #[test]
    fn summarization_language_defaults_to_auto() {
        let config = Config::default();
        assert_eq!(config.summarization.language, "auto");
    }

    #[test]
    fn summarization_language_can_be_set_from_toml() {
        let dir = TempDir::new().unwrap();
        let config_path = dir.path().join("config.toml");
        std::fs::write(
            &config_path,
            r#"
[summarization]
language = "fr"
"#,
        )
        .unwrap();

        let config = Config::load_from(&config_path);
        assert_eq!(config.summarization.language, "fr");
    }
}

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

    fn parse(snippet: &str) -> Config {
        toml::from_str(&format!("[transcription]\n{snippet}\n")).unwrap()
    }

    #[test]
    fn absent_key_is_auto() {
        assert_eq!(
            parse("engine = \"parakeet\"")
                .transcription
                .parakeet_sidecar_enabled,
            None
        );
    }

    #[test]
    fn legacy_bool_false_is_treated_as_auto() {
        // Pre-0.18.8 full-struct saves wrote `= false` into every config (#295).
        assert_eq!(
            parse("parakeet_sidecar_enabled = false")
                .transcription
                .parakeet_sidecar_enabled,
            None
        );
    }

    #[test]
    fn bool_true_forces_on() {
        assert_eq!(
            parse("parakeet_sidecar_enabled = true")
                .transcription
                .parakeet_sidecar_enabled,
            Some(true)
        );
    }

    #[test]
    fn string_off_forces_off_and_roundtrips() {
        let config = parse("parakeet_sidecar_enabled = \"off\"");
        assert_eq!(config.transcription.parakeet_sidecar_enabled, Some(false));
        let out = toml::to_string_pretty(&config).unwrap();
        assert!(out.contains("parakeet_sidecar_enabled = \"off\""));
        // And the roundtrip survives a re-parse (deliberate off is durable).
        let again: Config = toml::from_str(&out).unwrap();
        assert_eq!(again.transcription.parakeet_sidecar_enabled, Some(false));
    }

    #[test]
    fn auto_serializes_to_no_key() {
        let config = Config::default();
        let out = toml::to_string_pretty(&config).unwrap();
        assert!(!out.contains("parakeet_sidecar_enabled"));
    }

    #[test]
    fn string_auto_and_on_parse() {
        assert_eq!(
            parse("parakeet_sidecar_enabled = \"auto\"")
                .transcription
                .parakeet_sidecar_enabled,
            None
        );
        assert_eq!(
            parse("parakeet_sidecar_enabled = \"on\"")
                .transcription
                .parakeet_sidecar_enabled,
            Some(true)
        );
    }
}

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

    #[test]
    fn integer_value_is_rejected() {
        let result = toml::from_str::<Config>("[transcription]\nparakeet_sidecar_enabled = 0\n");
        assert!(result.is_err(), "integer must not silently coerce");
    }

    #[test]
    fn datetime_value_is_rejected() {
        let result =
            toml::from_str::<Config>("[transcription]\nparakeet_sidecar_enabled = 2026-06-10\n");
        assert!(result.is_err(), "datetime must not silently coerce");
    }

    #[test]
    fn unknown_string_is_rejected() {
        let result =
            toml::from_str::<Config>("[transcription]\nparakeet_sidecar_enabled = \"sometimes\"\n");
        assert!(result.is_err(), "unknown strings must error, not default");
    }
}