minutes-core 0.23.0

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
use crate::config::Config;
use rusqlite::{params, Connection, OptionalExtension, Transaction, TransactionBehavior};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Connection type used by the local voice database APIs.
pub type VoiceConnection = Connection;

#[cfg(feature = "diarize")]
const SOLO_SAMPLE_RATE: u32 = 16_000;
#[cfg(feature = "diarize")]
const SOLO_WINDOW_SECONDS: f64 = 3.0;
#[cfg(feature = "diarize")]
const SOLO_WINDOW_HOP_SECONDS: f64 = 1.5;
#[cfg(any(feature = "diarize", test))]
const SOLO_MIN_SPEECH_SECONDS: f64 = 5.0;
#[cfg(any(feature = "diarize", test))]
const SOLO_MIN_WINDOW_CONSISTENCY: f32 = 0.70;
#[cfg(any(feature = "diarize", test))]
const SOLO_MIN_SNR_DB: f32 = 8.0;
#[cfg(any(feature = "diarize", test))]
const SOLO_MAX_CLIPPING_FRACTION: f32 = 0.05;

// ──────────────────────────────────────────────────────────────
// Voice profile storage and matching.
//
// Stored in ~/.minutes/voices.db — separate from graph.db
// (which is a rebuildable cache that wipes on rebuild).
// ──────────────────────────────────────────────────────────────

/// Resolve the model version tag for the currently configured embedding model.
/// Falls back to the cam++-lm version string if the config value is unrecognized.
pub fn model_version(config: &Config) -> &'static str {
    crate::diarize::embedding_model_for_config(config).version
}

#[derive(Debug, Error)]
pub enum VoiceError {
    #[error("SQLite error: {0}")]
    Sqlite(#[from] rusqlite::Error),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("voice clip rejected: {reason}")]
    LowQuality { reason: String },
    #[error("{0}")]
    Other(String),
}

/// Quality evidence produced alongside a manually captured voice embedding.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VoiceQuality {
    /// Estimated signal-to-noise ratio in decibels.
    pub snr: f32,
    /// Fraction of decoded samples at or above the clipping threshold.
    pub clipping: f32,
    /// Lowest pairwise cosine similarity among the accepted windows.
    pub window_consistency: f32,
}

/// One quality-gated embedding produced from a known-single-speaker WAV.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SoloEmbedding {
    pub embedding: Vec<f32>,
    pub dim: usize,
    pub model_id: String,
    pub speech_seconds: f64,
    pub segment_count: u32,
    pub quality: VoiceQuality,
}

/// User-facing summary of an active model-scoped voice enrollment.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VoiceEnrollmentSummary {
    pub person_slug: String,
    pub name: String,
    pub model_id: String,
    pub sample_count: u32,
    pub updated_at: String,
    pub last_match_similarity: Option<f32>,
    pub last_match_margin: Option<f32>,
}

/// Evidence from comparing one probe against compatible active profiles.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VoiceMatchEvidence {
    pub model_id: String,
    pub winner_slug: Option<String>,
    pub winner_name: Option<String>,
    pub similarity: Option<f32>,
    pub runner_up_similarity: Option<f32>,
    pub margin: Option<f32>,
    pub threshold: f32,
    pub accepted: bool,
    pub reason: String,
}

/// Counts returned after a complete local biometric-data sweep.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct DeleteVoiceDataReport {
    pub profiles_deleted: usize,
    pub samples_deleted: usize,
    pub active_profiles_deleted: usize,
    pub sidecars_deleted: usize,
    pub sqlite_aux_files_deleted: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct VoiceProfile {
    pub person_slug: String,
    pub name: String,
    pub enrolled_at: String,
    pub updated_at: String,
    pub sample_count: u32,
    pub source: String,
    pub model_version: String,
}

pub struct VoiceProfileWithEmbedding {
    pub person_slug: String,
    pub name: String,
    pub embedding: Vec<f32>,
    pub sample_count: u32,
}

pub fn db_path() -> PathBuf {
    let base = dirs::home_dir()
        .expect("home directory must exist")
        .join(".minutes");
    std::fs::create_dir_all(&base).ok();
    base.join("voices.db")
}

pub fn open_db() -> Result<Connection, VoiceError> {
    open_db_at(&db_path())
}

pub fn open_db_at(path: &Path) -> Result<Connection, VoiceError> {
    let conn = Connection::open(path)?;
    conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;")?;
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS voice_profiles (
            id INTEGER PRIMARY KEY,
            person_slug TEXT UNIQUE NOT NULL,
            name TEXT NOT NULL,
            embedding BLOB NOT NULL,
            enrolled_at TEXT NOT NULL,
            updated_at TEXT NOT NULL,
            sample_count INTEGER DEFAULT 1,
            source TEXT NOT NULL,
            model_version TEXT NOT NULL
        );

        CREATE TABLE IF NOT EXISTS voice_samples (
            id INTEGER PRIMARY KEY,
            person_slug TEXT NOT NULL,
            name TEXT NOT NULL,
            embedding BLOB NOT NULL,
            embedding_dim INTEGER NOT NULL,
            model_id TEXT NOT NULL,
            normalization TEXT NOT NULL DEFAULT 'l2',
            trust_class TEXT NOT NULL,
            meeting_path TEXT,
            sidecar_speaker TEXT,
            capture_source TEXT,
            speech_seconds REAL NOT NULL DEFAULT 0,
            segment_count INTEGER NOT NULL DEFAULT 0,
            quality_json TEXT,
            similarity REAL,
            top2_margin REAL,
            threshold_version TEXT,
            sensitivity TEXT NOT NULL DEFAULT 'normal',
            created_at TEXT NOT NULL,
            revoked_at TEXT
        );
        CREATE INDEX IF NOT EXISTS idx_voice_samples_slug_model
            ON voice_samples(person_slug, model_id);

        CREATE TABLE IF NOT EXISTS voice_active_profiles (
            person_slug TEXT NOT NULL,
            model_id TEXT NOT NULL,
            name TEXT NOT NULL,
            embedding BLOB NOT NULL,
            embedding_dim INTEGER NOT NULL,
            sample_count INTEGER NOT NULL,
            updated_at TEXT NOT NULL,
            PRIMARY KEY (person_slug, model_id)
        );

        CREATE TRIGGER IF NOT EXISTS voice_samples_prevent_delete
        BEFORE DELETE ON voice_samples
        BEGIN
            SELECT RAISE(ABORT, 'voice samples are immutable');
        END;

        CREATE TRIGGER IF NOT EXISTS voice_samples_prevent_content_update
        BEFORE UPDATE OF person_slug, name, embedding, embedding_dim, model_id,
            normalization, trust_class, meeting_path, sidecar_speaker,
            capture_source, speech_seconds, segment_count, quality_json,
            similarity, top2_margin, threshold_version, sensitivity, created_at
        ON voice_samples
        BEGIN
            SELECT RAISE(ABORT, 'voice samples are immutable');
        END;

        CREATE TRIGGER IF NOT EXISTS voice_samples_prevent_rerevoke
        BEFORE UPDATE OF revoked_at ON voice_samples
        WHEN NEW.revoked_at IS NULL OR OLD.revoked_at IS NOT NULL
        BEGIN
            SELECT RAISE(ABORT, 'voice sample revocation is immutable');
        END;",
    )?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if path.exists() {
            std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600)).ok();
        }
    }
    Ok(conn)
}

pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    if norm_a == 0.0 || norm_b == 0.0 {
        return 0.0;
    }
    dot / (norm_a * norm_b)
}

#[cfg(any(feature = "diarize", test))]
fn l2_normalize(embedding: &[f32]) -> Result<Vec<f32>, VoiceError> {
    if embedding.is_empty() || embedding.iter().any(|value| !value.is_finite()) {
        return Err(VoiceError::LowQuality {
            reason: "the embedding model returned an empty or non-finite vector".to_string(),
        });
    }
    let norm = embedding
        .iter()
        .map(|value| value * value)
        .sum::<f32>()
        .sqrt();
    if !norm.is_finite() || norm <= f32::EPSILON {
        return Err(VoiceError::LowQuality {
            reason: "the embedding model returned a zero-length vector".to_string(),
        });
    }
    Ok(embedding.iter().map(|value| value / norm).collect())
}

#[cfg(any(feature = "diarize", test))]
fn aggregate_solo_embeddings(
    window_embeddings: &[Vec<f32>],
    model_id: &str,
    speech_seconds: f64,
    snr: f32,
    clipping: f32,
) -> Result<SoloEmbedding, VoiceError> {
    if speech_seconds < SOLO_MIN_SPEECH_SECONDS {
        return Err(VoiceError::LowQuality {
            reason: format!(
                "only {speech_seconds:.1}s of speech was detected; speak for at least {SOLO_MIN_SPEECH_SECONDS:.0}s"
            ),
        });
    }
    if snr < SOLO_MIN_SNR_DB {
        return Err(VoiceError::LowQuality {
            reason: format!(
                "signal-to-noise ratio was {snr:.1} dB; move closer to the microphone or reduce background noise"
            ),
        });
    }
    if clipping > SOLO_MAX_CLIPPING_FRACTION {
        return Err(VoiceError::LowQuality {
            reason: format!(
                "{:.1}% of the recording was clipped; lower the microphone level or move farther away",
                clipping * 100.0
            ),
        });
    }
    if window_embeddings.len() < 2 {
        return Err(VoiceError::LowQuality {
            reason: "too few clean speech windows were available for a reliable voiceprint"
                .to_string(),
        });
    }

    let normalized = window_embeddings
        .iter()
        .map(|embedding| l2_normalize(embedding))
        .collect::<Result<Vec<_>, _>>()?;
    let dim = normalized[0].len();
    if normalized.iter().any(|embedding| embedding.len() != dim) {
        return Err(VoiceError::LowQuality {
            reason: "the embedding model returned inconsistent vector dimensions".to_string(),
        });
    }

    let mut window_consistency = 1.0f32;
    for left in 0..normalized.len() {
        for right in (left + 1)..normalized.len() {
            window_consistency =
                window_consistency.min(cosine_similarity(&normalized[left], &normalized[right]));
        }
    }
    if window_consistency < SOLO_MIN_WINDOW_CONSISTENCY {
        return Err(VoiceError::LowQuality {
            reason: format!(
                "the recording was inconsistent across speech windows (cosine {window_consistency:.2}); make sure only one person speaks"
            ),
        });
    }

    let mut embedding = vec![0.0f32; dim];
    for window in &normalized {
        for (sum, value) in embedding.iter_mut().zip(window) {
            *sum += value;
        }
    }
    for value in &mut embedding {
        *value /= normalized.len() as f32;
    }
    let embedding = l2_normalize(&embedding)?;
    let segment_count = u32::try_from(normalized.len())
        .map_err(|_| VoiceError::Other("voice window count exceeds u32".to_string()))?;

    Ok(SoloEmbedding {
        embedding,
        dim,
        model_id: model_id.to_string(),
        speech_seconds,
        segment_count,
        quality: VoiceQuality {
            snr,
            clipping,
            window_consistency,
        },
    })
}

#[cfg(feature = "diarize")]
fn decode_wav_mono_16khz(path: &Path) -> Result<Vec<f32>, VoiceError> {
    let mut reader = hound::WavReader::open(path)
        .map_err(|error| VoiceError::Other(format!("could not read WAV: {error}")))?;
    let spec = reader.spec();
    if spec.channels == 0 || spec.sample_rate == 0 {
        return Err(VoiceError::LowQuality {
            reason: "the WAV has an invalid channel count or sample rate".to_string(),
        });
    }

    let interleaved = match spec.sample_format {
        hound::SampleFormat::Float => reader
            .samples::<f32>()
            .map(|sample| {
                sample.map_err(|error| VoiceError::Other(format!("could not decode WAV: {error}")))
            })
            .collect::<Result<Vec<_>, _>>()?,
        hound::SampleFormat::Int => {
            let scale = 2_f32.powi(i32::from(spec.bits_per_sample).saturating_sub(1));
            reader
                .samples::<i32>()
                .map(|sample| {
                    sample.map(|value| value as f32 / scale).map_err(|error| {
                        VoiceError::Other(format!("could not decode WAV: {error}"))
                    })
                })
                .collect::<Result<Vec<_>, _>>()?
        }
    };

    let channels = usize::from(spec.channels);
    let mut mono = Vec::with_capacity(interleaved.len() / channels);
    for frame in interleaved.chunks_exact(channels) {
        let sample = frame.iter().copied().sum::<f32>() / channels as f32;
        if !sample.is_finite() {
            return Err(VoiceError::LowQuality {
                reason: "the WAV contains non-finite samples".to_string(),
            });
        }
        mono.push(sample.clamp(-1.0, 1.0));
    }

    if spec.sample_rate == SOLO_SAMPLE_RATE {
        return Ok(mono);
    }

    // Capture uses `crate::resample` to produce this same canonical 16 kHz
    // stream. Imported WAVs take the equivalent in-process linear path here so
    // enrollment never shells out to ffmpeg or feeds a model the wrong rate.
    let ratio = f64::from(spec.sample_rate) / f64::from(SOLO_SAMPLE_RATE);
    let output_len = (mono.len() as f64 / ratio).floor() as usize;
    let mut resampled = Vec::with_capacity(output_len);
    for output_index in 0..output_len {
        let source_position = output_index as f64 * ratio;
        let left = source_position.floor() as usize;
        let right = (left + 1).min(mono.len().saturating_sub(1));
        let fraction = (source_position - left as f64) as f32;
        if let Some(left_sample) = mono.get(left) {
            resampled.push(*left_sample + (mono[right] - *left_sample) * fraction);
        }
    }
    Ok(resampled)
}

#[cfg(feature = "diarize")]
fn solo_audio_quality(samples: &[f32]) -> (f64, f32, f32) {
    let clipping = samples.iter().filter(|sample| sample.abs() >= 0.99).count() as f32
        / samples.len().max(1) as f32;
    let frame_len = (SOLO_SAMPLE_RATE / 50) as usize;
    let mut frame_rms = samples
        .chunks(frame_len)
        .filter(|frame| frame.len() == frame_len)
        .map(|frame| {
            (frame.iter().map(|sample| sample * sample).sum::<f32>() / frame.len() as f32).sqrt()
        })
        .collect::<Vec<_>>();
    if frame_rms.is_empty() {
        return (0.0, 0.0, clipping);
    }
    frame_rms.sort_by(f32::total_cmp);
    let peak_rms = *frame_rms.last().unwrap_or(&0.0);
    let mut noise_rms = frame_rms[frame_rms.len() / 10].max(0.0001);
    if noise_rms >= peak_rms * 0.8 {
        noise_rms = 0.0001;
    }
    let speech_floor = (noise_rms * 3.0).max(0.003);
    let speech_frames = frame_rms
        .iter()
        .copied()
        .filter(|rms| *rms >= speech_floor)
        .collect::<Vec<_>>();
    let speech_seconds =
        speech_frames.len() as f64 * frame_len as f64 / f64::from(SOLO_SAMPLE_RATE);
    let signal_rms = if speech_frames.is_empty() {
        0.0
    } else {
        speech_frames.iter().sum::<f32>() / speech_frames.len() as f32
    };
    let snr = if signal_rms <= 0.0 {
        0.0
    } else {
        20.0 * (signal_rms / noise_rms).log10()
    };
    (speech_seconds, snr, clipping)
}

#[cfg(feature = "diarize")]
fn solo_embedding_windows(samples: &[f32]) -> Vec<Vec<i16>> {
    let window_len = (SOLO_WINDOW_SECONDS * f64::from(SOLO_SAMPLE_RATE)) as usize;
    let hop_len = (SOLO_WINDOW_HOP_SECONDS * f64::from(SOLO_SAMPLE_RATE)) as usize;
    if samples.len() < window_len {
        return Vec::new();
    }
    (0..=(samples.len() - window_len))
        .step_by(hop_len)
        .filter_map(|start| {
            let window = &samples[start..start + window_len];
            let rms = (window.iter().map(|sample| sample * sample).sum::<f32>()
                / window.len() as f32)
                .sqrt();
            if rms < 0.003 {
                return None;
            }
            let gain = (0.1 / rms).min(20.0);
            Some(
                window
                    .iter()
                    .map(|sample| (sample * gain).clamp(-1.0, 1.0))
                    .map(|sample| (sample * 32767.0) as i16)
                    .collect(),
            )
        })
        .collect()
}

/// Embed a known-single-speaker WAV using overlapping, independently
/// normalized windows and reject clips that cannot safely form a voiceprint.
#[cfg(feature = "diarize")]
pub fn embed_solo_clip(wav_path: &Path, config: &Config) -> Result<SoloEmbedding, VoiceError> {
    let samples = decode_wav_mono_16khz(wav_path)?;
    let (speech_seconds, snr, clipping) = solo_audio_quality(&samples);
    if speech_seconds < SOLO_MIN_SPEECH_SECONDS {
        return aggregate_solo_embeddings(
            &[],
            model_version(config),
            speech_seconds,
            snr,
            clipping,
        );
    }
    let windows = solo_embedding_windows(&samples);
    let embeddings = crate::diarize::extract_speaker_embeddings(&windows, config)
        .map_err(|error| VoiceError::Other(format!("voice embedding failed: {error}")))?;
    aggregate_solo_embeddings(
        &embeddings,
        model_version(config),
        speech_seconds,
        snr,
        clipping,
    )
}

/// Compile-safe enrollment stub for builds without the local diarization model.
#[cfg(not(feature = "diarize"))]
pub fn embed_solo_clip(_wav_path: &Path, _config: &Config) -> Result<SoloEmbedding, VoiceError> {
    Err(VoiceError::Other(
        "voice enrollment requires the `diarize` feature; reinstall Minutes with diarization enabled"
            .to_string(),
    ))
}

fn embedding_to_bytes(embedding: &[f32]) -> Vec<u8> {
    embedding.iter().flat_map(|f| f.to_le_bytes()).collect()
}

fn bytes_to_embedding(bytes: &[u8]) -> Vec<f32> {
    bytes
        .chunks_exact(4)
        .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
        .collect()
}

/// The provenance and confidence class assigned to an immutable voice sample.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustClass {
    /// A sample captured through explicit manual enrollment.
    Manual,
    /// A candidate sample explicitly confirmed by a person.
    ManuallyConfirmed,
    /// An unconfirmed candidate inferred from its capture source.
    SourceCandidate,
    /// An unconfirmed candidate proposed by voice matching.
    VoicematchCandidate,
}

impl TrustClass {
    /// Return the stable snake_case database representation.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Manual => "manual",
            Self::ManuallyConfirmed => "manually_confirmed",
            Self::SourceCandidate => "source_candidate",
            Self::VoicematchCandidate => "voicematch_candidate",
        }
    }

    /// Parse a stable snake_case database representation.
    #[allow(clippy::should_implement_trait)] // The WU1 storage API explicitly requires this helper.
    pub fn from_str(value: &str) -> Result<Self, VoiceError> {
        value.parse()
    }
}

impl std::str::FromStr for TrustClass {
    type Err = VoiceError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "manual" => Ok(Self::Manual),
            "manually_confirmed" => Ok(Self::ManuallyConfirmed),
            "source_candidate" => Ok(Self::SourceCandidate),
            "voicematch_candidate" => Ok(Self::VoicematchCandidate),
            _ => Err(VoiceError::Other(format!(
                "unknown voice sample trust class: {value}"
            ))),
        }
    }
}

/// An immutable, provenance-bearing voice embedding sample.
#[derive(Debug, Clone)]
pub struct VoiceSample {
    /// Database row identifier.
    pub id: i64,
    /// Stable person slug associated with the sample.
    pub person_slug: String,
    /// Display name associated with the sample when it was captured.
    pub name: String,
    /// Voice embedding decoded as little-endian `f32` values.
    pub embedding: Vec<f32>,
    /// Number of values in the embedding.
    pub embedding_dim: usize,
    /// Identifier of the model that produced the embedding.
    pub model_id: String,
    /// Embedding normalization convention.
    pub normalization: String,
    /// Provenance and confidence class of the sample.
    pub trust_class: TrustClass,
    /// Optional meeting artifact from which the sample was derived.
    pub meeting_path: Option<String>,
    /// Optional speaker label used in the meeting sidecar.
    pub sidecar_speaker: Option<String>,
    /// Optional capture source description.
    pub capture_source: Option<String>,
    /// Amount of speech represented by the sample, in seconds.
    pub speech_seconds: f64,
    /// Number of speech segments represented by the sample.
    pub segment_count: u32,
    /// Optional serialized quality metrics.
    pub quality_json: Option<String>,
    /// Optional similarity score that produced the sample.
    pub similarity: Option<f64>,
    /// Optional margin between the two strongest matches.
    pub top2_margin: Option<f64>,
    /// Optional version of the threshold policy used for the sample.
    pub threshold_version: Option<String>,
    /// Sensitivity policy assigned to the sample.
    pub sensitivity: String,
    /// Timestamp at which the immutable sample was created.
    pub created_at: String,
    /// Timestamp at which the sample was revoked, if any.
    pub revoked_at: Option<String>,
}

/// Caller-supplied fields used to insert an immutable voice sample.
#[derive(Debug, Clone)]
pub struct VoiceSampleInput {
    /// Stable person slug associated with the sample.
    pub person_slug: String,
    /// Display name associated with the sample.
    pub name: String,
    /// Voice embedding produced by `model_id`.
    pub embedding: Vec<f32>,
    /// Identifier of the model that produced the embedding.
    pub model_id: String,
    /// Provenance and confidence class of the sample.
    pub trust_class: TrustClass,
    /// Optional meeting artifact from which the sample was derived.
    pub meeting_path: Option<String>,
    /// Optional speaker label used in the meeting sidecar.
    pub sidecar_speaker: Option<String>,
    /// Optional capture source description.
    pub capture_source: Option<String>,
    /// Amount of speech represented by the sample, in seconds.
    pub speech_seconds: f64,
    /// Number of speech segments represented by the sample.
    pub segment_count: u32,
    /// Optional serialized quality metrics.
    pub quality_json: Option<String>,
    /// Optional similarity score that produced the sample.
    pub similarity: Option<f64>,
    /// Optional margin between the two strongest matches.
    pub top2_margin: Option<f64>,
    /// Optional version of the threshold policy used for the sample.
    pub threshold_version: Option<String>,
    /// Sensitivity policy assigned to the sample.
    pub sensitivity: String,
    /// Optional deterministic creation timestamp; current local time is used when absent.
    pub created_at: Option<String>,
}

/// A model-scoped voice profile derived from non-revoked immutable samples.
#[derive(Debug, Clone)]
pub struct ActiveProfile {
    /// Stable person slug represented by the profile.
    pub person_slug: String,
    /// Identifier of the embedding model used by every contributing sample.
    pub model_id: String,
    /// Display name associated with the derived profile.
    pub name: String,
    /// Robust mean of the contributing embeddings.
    pub embedding: Vec<f32>,
    /// Number of values in the embedding.
    pub embedding_dim: usize,
    /// Number of non-outlier samples included in the robust mean.
    pub sample_count: u32,
}

#[derive(Debug)]
struct StoredSample {
    id: i64,
    name: String,
    embedding: Vec<f32>,
    embedding_dim: usize,
}

const ACTIVE_PROFILE_COSINE_FLOOR: f32 = 0.5;

fn now_timestamp() -> String {
    chrono::Local::now().to_rfc3339()
}

fn mean_embedding(samples: &[&StoredSample]) -> Vec<f32> {
    let embedding_dim = samples[0].embedding_dim;
    let mut mean = vec![0.0; embedding_dim];
    for sample in samples {
        for (sum, value) in mean.iter_mut().zip(&sample.embedding) {
            *sum += value;
        }
    }
    let count = samples.len() as f32;
    for value in &mut mean {
        *value /= count;
    }
    mean
}

fn rebuild_active_profile_in_transaction(
    conn: &Connection,
    slug: &str,
    model_id: &str,
) -> Result<Option<ActiveProfile>, VoiceError> {
    if model_id == "unknown" {
        conn.execute(
            "DELETE FROM voice_active_profiles WHERE person_slug = ?1 AND model_id = ?2",
            params![slug, model_id],
        )?;
        return Ok(None);
    }

    let samples = {
        let mut stmt = conn.prepare(
            "SELECT id, name, embedding, embedding_dim
             FROM voice_samples
             WHERE person_slug = ?1 AND model_id = ?2 AND revoked_at IS NULL
             ORDER BY id ASC",
        )?;
        let rows = stmt.query_map(params![slug, model_id], |row| {
            let blob: Vec<u8> = row.get(2)?;
            Ok(StoredSample {
                id: row.get(0)?,
                name: row.get(1)?,
                embedding: bytes_to_embedding(&blob),
                embedding_dim: row.get(3)?,
            })
        })?;
        rows.collect::<Result<Vec<_>, _>>()?
    };

    if samples.is_empty() {
        conn.execute(
            "DELETE FROM voice_active_profiles WHERE person_slug = ?1 AND model_id = ?2",
            params![slug, model_id],
        )?;
        return Ok(None);
    }

    let mut dimension_counts = std::collections::BTreeMap::<usize, (usize, i64)>::new();
    for sample in &samples {
        let entry = dimension_counts
            .entry(sample.embedding_dim)
            .or_insert((0, sample.id));
        entry.0 += 1;
        entry.1 = entry.1.min(sample.id);
    }
    let embedding_dim = dimension_counts
        .into_iter()
        .max_by(|(dim_a, (count_a, first_a)), (dim_b, (count_b, first_b))| {
            count_a
                .cmp(count_b)
                .then_with(|| first_b.cmp(first_a))
                .then_with(|| dim_b.cmp(dim_a))
        })
        .map(|(dimension, _)| dimension)
        .expect("samples is non-empty");

    let matching_samples: Vec<&StoredSample> = samples
        .iter()
        .filter(|sample| {
            sample.embedding_dim == embedding_dim && sample.embedding.len() == embedding_dim
        })
        .collect();

    if matching_samples.is_empty() {
        conn.execute(
            "DELETE FROM voice_active_profiles WHERE person_slug = ?1 AND model_id = ?2",
            params![slug, model_id],
        )?;
        return Ok(None);
    }

    let provisional_centroid = mean_embedding(&matching_samples);
    let similarities: Vec<f32> = matching_samples
        .iter()
        .map(|sample| cosine_similarity(&sample.embedding, &provisional_centroid))
        .collect();
    let mut accepted: Vec<&StoredSample> = matching_samples
        .iter()
        .zip(&similarities)
        .filter_map(|(sample, similarity)| {
            (*similarity >= ACTIVE_PROFILE_COSINE_FLOOR).then_some(*sample)
        })
        .collect();
    if accepted.is_empty() {
        let best_index = similarities
            .iter()
            .enumerate()
            .max_by(|(index_a, similarity_a), (index_b, similarity_b)| {
                similarity_a
                    .total_cmp(similarity_b)
                    .then_with(|| index_b.cmp(index_a))
            })
            .map(|(index, _)| index)
            .expect("matching samples is non-empty");
        accepted.push(matching_samples[best_index]);
    }

    let embedding = mean_embedding(&accepted);
    let name = accepted
        .iter()
        .max_by_key(|sample| sample.id)
        .expect("accepted samples is non-empty")
        .name
        .clone();
    let sample_count = u32::try_from(accepted.len())
        .map_err(|_| VoiceError::Other("voice sample count exceeds u32".to_string()))?;
    let updated_at = now_timestamp();
    conn.execute(
        "INSERT INTO voice_active_profiles
            (person_slug, model_id, name, embedding, embedding_dim, sample_count, updated_at)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
         ON CONFLICT(person_slug, model_id) DO UPDATE SET
            name = excluded.name,
            embedding = excluded.embedding,
            embedding_dim = excluded.embedding_dim,
            sample_count = excluded.sample_count,
            updated_at = excluded.updated_at",
        params![
            slug,
            model_id,
            name,
            embedding_to_bytes(&embedding),
            embedding_dim,
            sample_count,
            updated_at,
        ],
    )?;

    Ok(Some(ActiveProfile {
        person_slug: slug.to_string(),
        model_id: model_id.to_string(),
        name,
        embedding,
        embedding_dim,
        sample_count,
    }))
}

/// Insert one immutable voice sample and rebuild its model-scoped active profile atomically.
pub fn insert_voice_sample(
    conn: &Connection,
    sample: &VoiceSampleInput,
) -> Result<i64, VoiceError> {
    let transaction = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)?;
    let created_at = sample.created_at.clone().unwrap_or_else(now_timestamp);
    transaction.execute(
        "INSERT INTO voice_samples (
            person_slug, name, embedding, embedding_dim, model_id, normalization,
            trust_class, meeting_path, sidecar_speaker, capture_source, speech_seconds,
            segment_count, quality_json, similarity, top2_margin, threshold_version,
            sensitivity, created_at
         ) VALUES (
            ?1, ?2, ?3, ?4, ?5, 'l2', ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13,
            ?14, ?15, ?16, ?17
         )",
        params![
            sample.person_slug,
            sample.name,
            embedding_to_bytes(&sample.embedding),
            sample.embedding.len(),
            sample.model_id,
            sample.trust_class.as_str(),
            sample.meeting_path,
            sample.sidecar_speaker,
            sample.capture_source,
            sample.speech_seconds,
            sample.segment_count,
            sample.quality_json,
            sample.similarity,
            sample.top2_margin,
            sample.threshold_version,
            sample.sensitivity,
            created_at,
        ],
    )?;
    let id = transaction.last_insert_rowid();
    rebuild_active_profile_in_transaction(&transaction, &sample.person_slug, &sample.model_id)?;
    transaction.commit()?;
    Ok(id)
}

/// Revoke an immutable voice sample and rebuild its model-scoped active profile atomically.
pub fn revoke_voice_sample(conn: &Connection, id: i64) -> Result<(), VoiceError> {
    let transaction = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)?;
    let sample_key: Option<(String, String, Option<String>)> = transaction
        .query_row(
            "SELECT person_slug, model_id, revoked_at FROM voice_samples WHERE id = ?1",
            params![id],
            |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
        )
        .optional()?;
    let (slug, model_id, revoked_at) =
        sample_key.ok_or_else(|| VoiceError::Other(format!("voice sample {id} does not exist")))?;
    if revoked_at.is_none() {
        transaction.execute(
            "UPDATE voice_samples SET revoked_at = ?1 WHERE id = ?2",
            params![now_timestamp(), id],
        )?;
    }
    rebuild_active_profile_in_transaction(&transaction, &slug, &model_id)?;
    transaction.commit()?;
    Ok(())
}

/// Rebuild one model-scoped active profile transactionally from non-revoked samples.
pub fn rebuild_active_profile(
    conn: &Connection,
    slug: &str,
    model_id: &str,
) -> Result<Option<ActiveProfile>, VoiceError> {
    let transaction = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)?;
    let profile = rebuild_active_profile_in_transaction(&transaction, slug, model_id)?;
    transaction.commit()?;
    Ok(profile)
}

fn active_profile_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<ActiveProfile> {
    let blob: Vec<u8> = row.get(3)?;
    Ok(ActiveProfile {
        person_slug: row.get(0)?,
        model_id: row.get(1)?,
        name: row.get(2)?,
        embedding: bytes_to_embedding(&blob),
        embedding_dim: row.get(4)?,
        sample_count: row.get(5)?,
    })
}

/// Read the cached active profile for one person and embedding model.
pub fn active_profile(
    conn: &Connection,
    slug: &str,
    model_id: &str,
) -> Result<Option<ActiveProfile>, VoiceError> {
    conn.query_row(
        "SELECT person_slug, model_id, name, embedding, embedding_dim, sample_count
         FROM voice_active_profiles
         WHERE person_slug = ?1 AND model_id = ?2",
        params![slug, model_id],
        active_profile_from_row,
    )
    .optional()
    .map_err(Into::into)
}

/// List every cached active profile produced by one embedding model.
pub fn list_active_profiles(
    conn: &Connection,
    model_id: &str,
) -> Result<Vec<ActiveProfile>, VoiceError> {
    let mut stmt = conn.prepare(
        "SELECT person_slug, model_id, name, embedding, embedding_dim, sample_count
         FROM voice_active_profiles
         WHERE model_id = ?1
         ORDER BY person_slug ASC",
    )?;
    let profiles = stmt
        .query_map(params![model_id], active_profile_from_row)?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(profiles)
}

/// List all active enrollments, including the most recent stored match evidence.
pub fn list_voice_enrollments(
    conn: &Connection,
) -> Result<Vec<VoiceEnrollmentSummary>, VoiceError> {
    let mut stmt = conn.prepare(
        "SELECT active.person_slug, active.name, active.model_id,
                (SELECT COUNT(*) FROM voice_samples samples
                 WHERE samples.person_slug = active.person_slug
                   AND samples.model_id = active.model_id
                   AND samples.revoked_at IS NULL),
                active.updated_at,
                (SELECT similarity FROM voice_samples matched
                 WHERE matched.person_slug = active.person_slug
                   AND matched.model_id = active.model_id
                   AND matched.similarity IS NOT NULL
                 ORDER BY matched.created_at DESC, matched.id DESC LIMIT 1),
                (SELECT top2_margin FROM voice_samples matched
                 WHERE matched.person_slug = active.person_slug
                   AND matched.model_id = active.model_id
                   AND matched.similarity IS NOT NULL
                 ORDER BY matched.created_at DESC, matched.id DESC LIMIT 1)
         FROM voice_active_profiles active
         ORDER BY lower(active.name), active.model_id",
    )?;
    let summaries = stmt
        .query_map([], |row| {
            Ok(VoiceEnrollmentSummary {
                person_slug: row.get(0)?,
                name: row.get(1)?,
                model_id: row.get(2)?,
                sample_count: row.get(3)?,
                updated_at: row.get(4)?,
                last_match_similarity: row.get::<_, Option<f64>>(5)?.map(|value| value as f32),
                last_match_margin: row.get::<_, Option<f64>>(6)?.map(|value| value as f32),
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(summaries)
}

/// Compare a probe only with profiles produced by the same embedding model.
pub fn match_active_profiles(
    embedding: &[f32],
    model_id: &str,
    profiles: &[ActiveProfile],
    threshold: f32,
) -> VoiceMatchEvidence {
    let mut ranked = profiles
        .iter()
        .filter(|profile| profile.model_id == model_id && profile.embedding_dim == embedding.len())
        .map(|profile| (profile, cosine_similarity(embedding, &profile.embedding)))
        .collect::<Vec<_>>();
    ranked.sort_by(|left, right| right.1.total_cmp(&left.1));
    let Some((winner, similarity)) = ranked.first().copied() else {
        return VoiceMatchEvidence {
            model_id: model_id.to_string(),
            winner_slug: None,
            winner_name: None,
            similarity: None,
            runner_up_similarity: None,
            margin: None,
            threshold,
            accepted: false,
            reason: "no compatible active voice profiles".to_string(),
        };
    };
    let runner_up_similarity = ranked.get(1).map(|(_, score)| *score);
    let margin = runner_up_similarity.map(|runner_up| similarity - runner_up);
    let accepted = similarity >= threshold;
    VoiceMatchEvidence {
        model_id: model_id.to_string(),
        winner_slug: Some(winner.person_slug.clone()),
        winner_name: Some(winner.name.clone()),
        similarity: Some(similarity),
        runner_up_similarity,
        margin,
        threshold,
        accepted,
        reason: if accepted {
            "matched above the configured threshold".to_string()
        } else {
            "best compatible profile was below the configured threshold".to_string()
        },
    }
}

/// Revoke every immutable sample for one person and remove derived caches.
pub fn revoke_voice_person(conn: &Connection, slug: &str) -> Result<usize, VoiceError> {
    let transaction = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)?;
    let revoked = transaction.execute(
        "UPDATE voice_samples SET revoked_at = ?1
         WHERE person_slug = ?2 AND revoked_at IS NULL",
        params![now_timestamp(), slug],
    )?;
    transaction.execute(
        "DELETE FROM voice_active_profiles WHERE person_slug = ?1",
        params![slug],
    )?;
    let legacy_deleted = transaction.execute(
        "DELETE FROM voice_profiles WHERE person_slug = ?1",
        params![slug],
    )?;
    transaction.commit()?;
    Ok(revoked + legacy_deleted)
}

fn sqlite_aux_path(db_path: &Path, suffix: &str) -> PathBuf {
    let mut path = db_path.as_os_str().to_os_string();
    path.push(suffix);
    PathBuf::from(path)
}

fn voice_sidecars(roots: &[PathBuf]) -> Vec<PathBuf> {
    let mut sidecars = Vec::new();
    for root in roots {
        if !root.exists() {
            continue;
        }
        for entry in walkdir::WalkDir::new(root)
            .follow_links(false)
            .into_iter()
            .filter_map(Result::ok)
        {
            if !entry.file_type().is_file() {
                continue;
            }
            let name = entry.file_name().to_string_lossy();
            if name.ends_with(".embeddings") {
                sidecars.push(entry.into_path());
            }
        }
    }
    sidecars.sort();
    sidecars.dedup();
    sidecars
}

fn restore_staged_sidecars(staged: &[(PathBuf, PathBuf)]) {
    for (original, quarantine) in staged.iter().rev() {
        if quarantine.exists() {
            let _ = std::fs::rename(quarantine, original);
        }
    }
}

fn delete_all_voice_data_at(
    db_path: &Path,
    sidecar_roots: &[PathBuf],
) -> Result<DeleteVoiceDataReport, VoiceError> {
    let nonce = format!(
        "{}-{}",
        std::process::id(),
        chrono::Local::now()
            .timestamp_nanos_opt()
            .unwrap_or_default()
    );
    let mut staged = Vec::<(PathBuf, PathBuf)>::new();
    for (index, original) in voice_sidecars(sidecar_roots).into_iter().enumerate() {
        let file_name = original
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("voice-sidecar");
        let quarantine = original.with_file_name(format!(
            ".{file_name}.minutes-delete-{nonce}-{index}.embeddings"
        ));
        if let Err(error) = std::fs::rename(&original, &quarantine) {
            restore_staged_sidecars(&staged);
            return Err(VoiceError::Io(error));
        }
        staged.push((original, quarantine));
    }

    let database_result = (|| -> Result<(usize, usize, usize), VoiceError> {
        if let Some(parent) = db_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let conn = open_db_at(db_path)?;
        conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        let transaction = Transaction::new_unchecked(&conn, TransactionBehavior::Immediate)?;
        transaction.execute_batch(
            "DROP TRIGGER IF EXISTS voice_samples_prevent_delete;
             DROP TRIGGER IF EXISTS voice_samples_prevent_content_update;
             DROP TRIGGER IF EXISTS voice_samples_prevent_rerevoke;",
        )?;
        let active_profiles_deleted =
            transaction.execute("DELETE FROM voice_active_profiles", [])?;
        let samples_deleted = transaction.execute("DELETE FROM voice_samples", [])?;
        let profiles_deleted = transaction.execute("DELETE FROM voice_profiles", [])?;
        transaction.execute_batch(
            "CREATE TRIGGER voice_samples_prevent_delete
             BEFORE DELETE ON voice_samples
             BEGIN SELECT RAISE(ABORT, 'voice samples are immutable'); END;

             CREATE TRIGGER voice_samples_prevent_content_update
             BEFORE UPDATE OF person_slug, name, embedding, embedding_dim, model_id,
                 normalization, trust_class, meeting_path, sidecar_speaker,
                 capture_source, speech_seconds, segment_count, quality_json,
                 similarity, top2_margin, threshold_version, sensitivity, created_at
             ON voice_samples
             BEGIN SELECT RAISE(ABORT, 'voice samples are immutable'); END;

             CREATE TRIGGER voice_samples_prevent_rerevoke
             BEFORE UPDATE OF revoked_at ON voice_samples
             WHEN NEW.revoked_at IS NULL OR OLD.revoked_at IS NOT NULL
             BEGIN SELECT RAISE(ABORT, 'voice sample revocation is immutable'); END;",
        )?;
        transaction.commit()?;
        conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")?;
        drop(conn);
        Ok((profiles_deleted, samples_deleted, active_profiles_deleted))
    })();

    let (profiles_deleted, samples_deleted, active_profiles_deleted) = match database_result {
        Ok(counts) => counts,
        Err(error) => {
            restore_staged_sidecars(&staged);
            return Err(error);
        }
    };

    let mut sidecars_deleted = 0;
    for (_, quarantine) in &staged {
        std::fs::remove_file(quarantine)?;
        sidecars_deleted += 1;
    }

    let mut sqlite_aux_files_deleted = 0;
    for suffix in ["-wal", "-shm"] {
        let path = sqlite_aux_path(db_path, suffix);
        if path.exists() {
            std::fs::remove_file(path)?;
            sqlite_aux_files_deleted += 1;
        }
    }

    Ok(DeleteVoiceDataReport {
        profiles_deleted,
        samples_deleted,
        active_profiles_deleted,
        sidecars_deleted,
        sqlite_aux_files_deleted,
    })
}

/// Remove all local voice profiles, immutable samples, derived caches,
/// meeting sidecars (including orphans), and SQLite WAL/SHM files.
pub fn delete_all_voice_data(config: &Config) -> Result<DeleteVoiceDataReport, VoiceError> {
    delete_all_voice_data_at(&db_path(), std::slice::from_ref(&config.output_dir))
}

/// Import legacy mutable profiles as manual immutable samples without creating duplicates.
pub fn migrate_legacy_profiles(conn: &Connection) -> Result<usize, VoiceError> {
    let transaction = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)?;
    let legacy_profiles = {
        let mut stmt = transaction.prepare(
            "SELECT person_slug, name, embedding, enrolled_at, source, model_version
             FROM voice_profiles
             ORDER BY id ASC",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, Vec<u8>>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, String>(4)?,
                row.get::<_, String>(5)?,
            ))
        })?;
        rows.collect::<Result<Vec<_>, _>>()?
    };

    let mut migrated = 0;
    for (slug, name, embedding, enrolled_at, source, legacy_model_id) in legacy_profiles {
        let model_id = if embedding.len() % std::mem::size_of::<f32>() == 0 {
            legacy_model_id
        } else {
            "unknown".to_string()
        };
        let exists: bool = transaction.query_row(
            "SELECT EXISTS(
                SELECT 1 FROM voice_samples
                WHERE person_slug = ?1 AND model_id = ?2 AND created_at = ?3
                    AND trust_class = 'manual'
             )",
            params![slug, model_id, enrolled_at],
            |row| row.get(0),
        )?;
        if !exists {
            transaction.execute(
                "INSERT INTO voice_samples (
                    person_slug, name, embedding, embedding_dim, model_id, normalization,
                    trust_class, capture_source, sensitivity, created_at
                 ) VALUES (?1, ?2, ?3, ?4, ?5, 'l2', 'manual', ?6, 'normal', ?7)",
                params![
                    slug,
                    name,
                    embedding,
                    embedding.len() / std::mem::size_of::<f32>(),
                    model_id,
                    source,
                    enrolled_at,
                ],
            )?;
            migrated += 1;
        }
        rebuild_active_profile_in_transaction(&transaction, &slug, &model_id)?;
    }
    transaction.commit()?;
    Ok(migrated)
}

pub fn save_profile(
    conn: &Connection,
    slug: &str,
    name: &str,
    embedding: &[f32],
    source: &str,
    model_version: &str,
) -> Result<(), VoiceError> {
    let now = chrono::Local::now().to_rfc3339();
    let blob = embedding_to_bytes(embedding);
    conn.execute(
        "INSERT INTO voice_profiles (person_slug, name, embedding, enrolled_at, updated_at, sample_count, source, model_version)
         VALUES (?1, ?2, ?3, ?4, ?5, 1, ?6, ?7)
         ON CONFLICT(person_slug) DO UPDATE SET
            name = excluded.name, embedding = excluded.embedding, updated_at = excluded.updated_at,
            sample_count = sample_count + 1, source = excluded.source, model_version = excluded.model_version",
        params![slug, name, blob, now, now, source, model_version],
    )?;
    Ok(())
}

pub fn save_profile_blended(
    conn: &Connection,
    slug: &str,
    name: &str,
    new_embedding: &[f32],
    source: &str,
    model_version: &str,
) -> Result<(), VoiceError> {
    if let Some(existing) = load_profile_with_embedding(conn, slug)? {
        let total = existing.sample_count as f32 + 1.0;
        let old_weight = existing.sample_count as f32;
        let blended: Vec<f32> = existing
            .embedding
            .iter()
            .zip(new_embedding.iter())
            .map(|(old, new)| (old * old_weight + new) / total)
            .collect();
        save_profile(conn, slug, name, &blended, source, model_version)
    } else {
        save_profile(conn, slug, name, new_embedding, source, model_version)
    }
}

fn load_profile_with_embedding(
    conn: &Connection,
    slug: &str,
) -> Result<Option<VoiceProfileWithEmbedding>, VoiceError> {
    let mut stmt = conn.prepare("SELECT person_slug, name, embedding, sample_count FROM voice_profiles WHERE person_slug = ?1")?;
    match stmt.query_row(params![slug], |row| {
        let blob: Vec<u8> = row.get(2)?;
        Ok(VoiceProfileWithEmbedding {
            person_slug: row.get(0)?,
            name: row.get(1)?,
            embedding: bytes_to_embedding(&blob),
            sample_count: row.get(3)?,
        })
    }) {
        Ok(p) => Ok(Some(p)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(e.into()),
    }
}

pub fn list_profiles(conn: &Connection) -> Result<Vec<VoiceProfile>, VoiceError> {
    let mut stmt = conn.prepare("SELECT person_slug, name, enrolled_at, updated_at, sample_count, source, model_version FROM voice_profiles ORDER BY updated_at DESC")?;
    let profiles = stmt
        .query_map([], |row| {
            Ok(VoiceProfile {
                person_slug: row.get(0)?,
                name: row.get(1)?,
                enrolled_at: row.get(2)?,
                updated_at: row.get(3)?,
                sample_count: row.get(4)?,
                source: row.get(5)?,
                model_version: row.get(6)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(profiles)
}

pub fn load_all_with_embeddings(
    conn: &Connection,
) -> Result<Vec<VoiceProfileWithEmbedding>, VoiceError> {
    let mut stmt =
        conn.prepare("SELECT person_slug, name, embedding, sample_count FROM voice_profiles")?;
    let profiles = stmt
        .query_map([], |row| {
            let blob: Vec<u8> = row.get(2)?;
            Ok(VoiceProfileWithEmbedding {
                person_slug: row.get(0)?,
                name: row.get(1)?,
                embedding: bytes_to_embedding(&blob),
                sample_count: row.get(3)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(profiles)
}

pub fn delete_profile(conn: &Connection, slug: &str) -> Result<bool, VoiceError> {
    Ok(conn.execute(
        "DELETE FROM voice_profiles WHERE person_slug = ?1",
        params![slug],
    )? > 0)
}

pub fn match_embedding(
    embedding: &[f32],
    profiles: &[VoiceProfileWithEmbedding],
    threshold: f32,
) -> Option<String> {
    let mut best_name = None;
    let mut best_sim = f32::MIN;

    for p in profiles {
        let sim = cosine_similarity(embedding, &p.embedding);
        tracing::debug!(
            profile = %p.name,
            similarity = format!("{:.4}", sim),
            "voice embedding comparison"
        );
        if sim > best_sim {
            best_sim = sim;
            if sim > threshold {
                best_name = Some(p.name.clone());
            }
        }
    }

    if let Some(ref name) = best_name {
        tracing::info!(matched = %name, similarity = format!("{:.4}", best_sim), "voice profile matched");
    } else if !profiles.is_empty() {
        tracing::info!(
            best_similarity = format!("{:.4}", best_sim),
            threshold = format!("{:.4}", threshold),
            "no voice profile matched"
        );
    }

    best_name
}

/// Save per-speaker embeddings as a sidecar file next to the meeting markdown.
/// Path: ~/meetings/.2026-03-25-standup.embeddings (hidden file, same dir)
pub fn save_meeting_embeddings(
    meeting_path: &std::path::Path,
    embeddings: &std::collections::HashMap<String, Vec<f32>>,
) {
    if embeddings.is_empty() {
        return;
    }
    let sidecar = meeting_embeddings_sidecar_path(meeting_path);
    let data = serde_json::to_vec(embeddings).unwrap_or_default();
    if let Err(e) = std::fs::write(&sidecar, &data) {
        tracing::warn!(path = %sidecar.display(), error = %e, "failed to write meeting embeddings");
    } else {
        // Set 0600 permissions (embeddings are biometric-adjacent data)
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&sidecar, std::fs::Permissions::from_mode(0o600)).ok();
        }
        tracing::debug!(path = %sidecar.display(), speakers = embeddings.len(), "meeting embeddings saved");
    }
}

/// Load per-speaker embeddings from a meeting's sidecar file.
pub fn load_meeting_embeddings(
    meeting_path: &std::path::Path,
) -> Option<std::collections::HashMap<String, Vec<f32>>> {
    let sidecar = meeting_embeddings_sidecar_path(meeting_path);
    let data = std::fs::read(&sidecar).ok()?;
    serde_json::from_slice(&data).ok()
}

pub fn meeting_embeddings_sidecar_path(meeting_path: &std::path::Path) -> std::path::PathBuf {
    let dir = meeting_path.parent().unwrap_or(std::path::Path::new("."));
    let stem = meeting_path
        .file_name()
        .unwrap_or_default()
        .to_string_lossy();
    dir.join(format!(".{}.embeddings", stem.trim_end_matches(".md")))
}

pub fn load_self_profile(config: &Config) -> Option<VoiceProfileWithEmbedding> {
    if !config.voice.enabled {
        return None;
    }
    let name = config.identity.name.as_ref()?;
    let slug = person_slug(name);
    let conn = open_db().ok()?;
    load_profile_with_embedding(&conn, &slug).ok().flatten()
}

/// Produce the stable slug used by voice profiles for a display name.
pub fn person_slug(text: &str) -> String {
    let slug: String = text
        .to_lowercase()
        .chars()
        .map(|c| if c.is_alphanumeric() { c } else { '-' })
        .collect();
    let mut result = String::new();
    let mut prev_hyphen = false;
    for c in slug.chars() {
        if c == '-' {
            if !prev_hyphen && !result.is_empty() {
                result.push(c);
            }
            prev_hyphen = true;
        } else {
            result.push(c);
            prev_hyphen = false;
        }
    }
    result.trim_end_matches('-').to_string()
}

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

    fn test_db() -> (Connection, NamedTempFile) {
        let tmp = NamedTempFile::new().unwrap();
        let conn = open_db_at(tmp.path()).unwrap();
        (conn, tmp)
    }

    fn voice_sample_input(
        slug: &str,
        name: &str,
        embedding: &[f32],
        model_id: &str,
        created_at: &str,
    ) -> VoiceSampleInput {
        VoiceSampleInput {
            person_slug: slug.to_string(),
            name: name.to_string(),
            embedding: embedding.to_vec(),
            model_id: model_id.to_string(),
            trust_class: TrustClass::Manual,
            meeting_path: None,
            sidecar_speaker: None,
            capture_source: Some("test".to_string()),
            speech_seconds: 3.0,
            segment_count: 1,
            quality_json: None,
            similarity: None,
            top2_margin: None,
            threshold_version: None,
            sensitivity: "normal".to_string(),
            created_at: Some(created_at.to_string()),
        }
    }

    fn assert_embedding_close(actual: &[f32], expected: &[f32]) {
        assert_eq!(actual.len(), expected.len());
        for (actual, expected) in actual.iter().zip(expected) {
            assert!(
                (actual - expected).abs() < 1e-6,
                "expected {expected}, got {actual}"
            );
        }
    }

    #[test]
    fn voice_samples_insert_same_model_derives_mean() {
        let (conn, _tmp) = test_db();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[1.0, 0.0], "model-a", "2026-01-01T00:00:00Z"),
        )
        .unwrap();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[0.8, 0.2], "model-a", "2026-01-02T00:00:00Z"),
        )
        .unwrap();

        let profile = active_profile(&conn, "mat", "model-a").unwrap().unwrap();
        assert_eq!(profile.sample_count, 2);
        assert_eq!(profile.embedding_dim, 2);
        assert_embedding_close(&profile.embedding, &[0.9, 0.1]);
        assert_eq!(list_active_profiles(&conn, "model-a").unwrap().len(), 1);
    }

    #[test]
    fn voice_samples_different_models_are_isolated() {
        let (conn, _tmp) = test_db();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[1.0, 0.0], "model-a", "2026-01-01T00:00:00Z"),
        )
        .unwrap();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[0.0, 1.0], "model-b", "2026-01-02T00:00:00Z"),
        )
        .unwrap();

        let model_a = active_profile(&conn, "mat", "model-a").unwrap().unwrap();
        let model_b = active_profile(&conn, "mat", "model-b").unwrap().unwrap();
        assert_embedding_close(&model_a.embedding, &[1.0, 0.0]);
        assert_embedding_close(&model_b.embedding, &[0.0, 1.0]);
        assert_eq!(model_a.sample_count, 1);
        assert_eq!(model_b.sample_count, 1);
    }

    #[test]
    fn voice_samples_revocation_rebuilds_and_removes_profile() {
        let (conn, _tmp) = test_db();
        let first = insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[1.0, 0.0], "model-a", "2026-01-01T00:00:00Z"),
        )
        .unwrap();
        let second = insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[0.8, 0.2], "model-a", "2026-01-02T00:00:00Z"),
        )
        .unwrap();

        revoke_voice_sample(&conn, first).unwrap();
        let profile = active_profile(&conn, "mat", "model-a").unwrap().unwrap();
        assert_eq!(profile.sample_count, 1);
        assert_embedding_close(&profile.embedding, &[0.8, 0.2]);

        revoke_voice_sample(&conn, second).unwrap();
        assert!(active_profile(&conn, "mat", "model-a").unwrap().is_none());
    }

    #[test]
    fn voice_samples_rebuild_rejects_outlier() {
        let (conn, _tmp) = test_db();
        for (index, embedding) in [
            vec![1.0, 0.0, 0.0],
            vec![0.98, 0.02, 0.0],
            vec![0.97, -0.03, 0.0],
            vec![0.0, 1.0, 0.0],
        ]
        .iter()
        .enumerate()
        {
            insert_voice_sample(
                &conn,
                &voice_sample_input(
                    "mat",
                    "Mat",
                    embedding,
                    "model-a",
                    &format!("2026-01-0{}T00:00:00Z", index + 1),
                ),
            )
            .unwrap();
        }

        let profile = rebuild_active_profile(&conn, "mat", "model-a")
            .unwrap()
            .unwrap();
        assert_eq!(profile.sample_count, 3);
        assert_embedding_close(&profile.embedding, &[0.98333335, -0.003333333, 0.0]);
    }

    #[test]
    fn voice_samples_migrate_legacy_profiles_is_idempotent() {
        let (conn, _tmp) = test_db();
        for (slug, name, embedding, model_id, timestamp) in [
            (
                "mat",
                "Mat",
                vec![1.0, 0.0],
                "model-a",
                "2026-01-01T00:00:00Z",
            ),
            (
                "alex",
                "Alex",
                vec![0.0, 1.0],
                "model-b",
                "2026-01-02T00:00:00Z",
            ),
        ] {
            conn.execute(
                "INSERT INTO voice_profiles (
                    person_slug, name, embedding, enrolled_at, updated_at,
                    sample_count, source, model_version
                 ) VALUES (?1, ?2, ?3, ?4, ?4, 1, 'legacy', ?5)",
                params![
                    slug,
                    name,
                    embedding_to_bytes(&embedding),
                    timestamp,
                    model_id
                ],
            )
            .unwrap();
        }

        assert_eq!(migrate_legacy_profiles(&conn).unwrap(), 2);
        assert_eq!(migrate_legacy_profiles(&conn).unwrap(), 0);
        let sample_count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM voice_samples WHERE trust_class = 'manual'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(sample_count, 2);
        assert!(active_profile(&conn, "mat", "model-a").unwrap().is_some());
        assert!(active_profile(&conn, "alex", "model-b").unwrap().is_some());
    }

    #[test]
    fn voice_samples_migrate_malformed_legacy_blob_as_unknown() {
        let (conn, _tmp) = test_db();
        conn.execute(
            "INSERT INTO voice_profiles (
                person_slug, name, embedding, enrolled_at, updated_at,
                sample_count, source, model_version
             ) VALUES ('mat', 'Mat', ?1, ?2, ?2, 1, 'legacy', 'model-a')",
            params![vec![1_u8, 2, 3], "2026-01-01T00:00:00Z"],
        )
        .unwrap();

        assert_eq!(migrate_legacy_profiles(&conn).unwrap(), 1);
        let model_id: String = conn
            .query_row(
                "SELECT model_id FROM voice_samples WHERE person_slug = 'mat'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(model_id, "unknown");
        assert!(active_profile(&conn, "mat", "unknown").unwrap().is_none());
        assert!(list_active_profiles(&conn, "unknown").unwrap().is_empty());
    }

    #[test]
    fn voice_samples_trust_class_round_trips() {
        for trust_class in [
            TrustClass::Manual,
            TrustClass::ManuallyConfirmed,
            TrustClass::SourceCandidate,
            TrustClass::VoicematchCandidate,
        ] {
            assert_eq!(
                TrustClass::from_str(trust_class.as_str()).unwrap(),
                trust_class
            );
        }
        assert!(TrustClass::from_str("untrusted").is_err());
    }

    #[test]
    fn cosine_identical() {
        assert!((cosine_similarity(&[1.0, 0.0], &[1.0, 0.0]) - 1.0).abs() < 1e-6);
    }
    #[test]
    fn cosine_orthogonal() {
        assert!(cosine_similarity(&[1.0, 0.0], &[0.0, 1.0]).abs() < 1e-6);
    }
    #[test]
    fn cosine_empty() {
        assert_eq!(cosine_similarity(&[], &[]), 0.0);
    }

    #[test]
    fn embedding_roundtrip() {
        let orig = vec![0.1, 0.2, -0.3, 1.0];
        assert_eq!(bytes_to_embedding(&embedding_to_bytes(&orig)), orig);
    }

    const TEST_MODEL_VERSION: &str = "test_model_v1";

    #[test]
    fn save_and_list() {
        let (conn, _tmp) = test_db();
        save_profile(
            &conn,
            "mat",
            "Mat",
            &vec![0.1f32; 512],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        let profiles = list_profiles(&conn).unwrap();
        assert_eq!(profiles.len(), 1);
        assert_eq!(profiles[0].person_slug, "mat");
        assert_eq!(profiles[0].sample_count, 1);
    }

    #[test]
    fn upsert_increments_count() {
        let (conn, _tmp) = test_db();
        save_profile(
            &conn,
            "mat",
            "Mat",
            &[0.1f32; 4],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        save_profile(
            &conn,
            "mat",
            "Mat",
            &[0.2f32; 4],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        assert_eq!(list_profiles(&conn).unwrap()[0].sample_count, 2);
    }

    #[test]
    fn blended_averages() {
        let (conn, _tmp) = test_db();
        save_profile(
            &conn,
            "mat",
            "Mat",
            &[1.0f32; 4],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        save_profile_blended(
            &conn,
            "mat",
            "Mat",
            &[3.0f32; 4],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        let p = load_profile_with_embedding(&conn, "mat").unwrap().unwrap();
        assert!((p.embedding[0] - 2.0).abs() < 1e-6);
    }

    #[test]
    fn delete_works() {
        let (conn, _tmp) = test_db();
        save_profile(
            &conn,
            "mat",
            "Mat",
            &[0.1f32; 4],
            "self-enrollment",
            TEST_MODEL_VERSION,
        )
        .unwrap();
        assert!(delete_profile(&conn, "mat").unwrap());
        assert!(list_profiles(&conn).unwrap().is_empty());
    }

    #[test]
    fn match_finds_best() {
        let profiles = vec![
            VoiceProfileWithEmbedding {
                person_slug: "mat".into(),
                name: "Mat".into(),
                embedding: vec![1.0, 0.0, 0.0],
                sample_count: 1,
            },
            VoiceProfileWithEmbedding {
                person_slug: "alex".into(),
                name: "Alex".into(),
                embedding: vec![0.0, 1.0, 0.0],
                sample_count: 1,
            },
        ];
        assert_eq!(
            match_embedding(&[0.9, 0.1, 0.0], &profiles, 0.5),
            Some("Mat".into())
        );
        assert_eq!(
            match_embedding(&[0.0, 1.0, 0.0], &profiles, 0.5),
            Some("Alex".into())
        );
    }

    #[test]
    fn match_none_below_threshold() {
        let profiles = vec![VoiceProfileWithEmbedding {
            person_slug: "mat".into(),
            name: "Mat".into(),
            embedding: vec![1.0, 0.0],
            sample_count: 1,
        }];
        assert_eq!(match_embedding(&[0.0, 1.0], &profiles, 0.5), None);
    }

    #[test]
    fn slugify_basic() {
        assert_eq!(person_slug("Mat Silverstein"), "mat-silverstein");
    }

    #[test]
    fn voice_embed_solo_consistent_injected_windows_pass_quality_gate() {
        let result = aggregate_solo_embeddings(
            &[
                vec![1.0, 0.02, 0.0],
                vec![0.99, 0.04, 0.01],
                vec![0.98, 0.03, 0.02],
            ],
            "model-a",
            8.0,
            24.0,
            0.0,
        )
        .unwrap();
        assert_eq!(result.model_id, "model-a");
        assert_eq!(result.dim, 3);
        assert_eq!(result.segment_count, 3);
        assert!(result.quality.window_consistency > 0.99);
        assert!((cosine_similarity(&result.embedding, &result.embedding) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn voice_embed_solo_multimodal_injected_windows_are_rejected() {
        let error = aggregate_solo_embeddings(
            &[vec![1.0, 0.0], vec![0.0, 1.0], vec![0.7, 0.7]],
            "model-a",
            8.0,
            24.0,
            0.0,
        )
        .unwrap_err();
        assert!(matches!(error, VoiceError::LowQuality { .. }));
        assert!(error.to_string().contains("inconsistent"));
    }

    #[test]
    fn voice_embed_solo_short_speech_is_rejected_before_model_use() {
        let error =
            aggregate_solo_embeddings(&[vec![1.0, 0.0], vec![1.0, 0.0]], "model-a", 2.0, 24.0, 0.0)
                .unwrap_err();
        assert!(matches!(error, VoiceError::LowQuality { .. }));
        assert!(error.to_string().contains("at least 5s"));
    }

    #[test]
    fn voice_match_evidence_uses_only_compatible_models() {
        let profiles = vec![
            ActiveProfile {
                person_slug: "mat".into(),
                model_id: "model-a".into(),
                name: "Mat".into(),
                embedding: vec![1.0, 0.0],
                embedding_dim: 2,
                sample_count: 1,
            },
            ActiveProfile {
                person_slug: "alex".into(),
                model_id: "model-b".into(),
                name: "Alex".into(),
                embedding: vec![1.0, 0.0],
                embedding_dim: 2,
                sample_count: 1,
            },
        ];
        let evidence = match_active_profiles(&[0.99, 0.01], "model-a", &profiles, 0.65);
        assert!(evidence.accepted);
        assert_eq!(evidence.winner_slug.as_deref(), Some("mat"));
        assert!(evidence.runner_up_similarity.is_none());
    }

    #[test]
    fn voice_remove_revokes_samples_and_clears_active_profile() {
        let (conn, _tmp) = test_db();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[1.0, 0.0], "model-a", "2026-01-01T00:00:00Z"),
        )
        .unwrap();
        assert_eq!(revoke_voice_person(&conn, "mat").unwrap(), 1);
        assert!(active_profile(&conn, "mat", "model-a").unwrap().is_none());
        let revoked: usize = conn
            .query_row(
                "SELECT COUNT(*) FROM voice_samples WHERE revoked_at IS NOT NULL",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(revoked, 1);
    }

    #[test]
    fn voice_privacy_sweep_removes_profiles_samples_cache_sidecars_and_wal() {
        let root = tempfile::TempDir::new().unwrap();
        let db = root.path().join("state/voices.db");
        let meetings = root.path().join("meetings");
        let nested = meetings.join("archive");
        std::fs::create_dir_all(db.parent().unwrap()).unwrap();
        std::fs::create_dir_all(&nested).unwrap();
        let meeting = meetings.join("present.md");
        std::fs::write(&meeting, "# present").unwrap();
        let sidecar = meeting_embeddings_sidecar_path(&meeting);
        std::fs::write(&sidecar, b"{}").unwrap();
        let orphan = nested.join(".missing.embeddings");
        std::fs::write(&orphan, b"{}").unwrap();

        let conn = open_db_at(&db).unwrap();
        insert_voice_sample(
            &conn,
            &voice_sample_input("mat", "Mat", &[1.0, 0.0], "model-a", "2026-01-01T00:00:00Z"),
        )
        .unwrap();
        save_profile(&conn, "legacy", "Legacy", &[1.0, 0.0], "test", "model-a").unwrap();
        drop(conn);
        std::fs::write(sqlite_aux_path(&db, "-wal"), b"stale wal").unwrap();
        std::fs::write(sqlite_aux_path(&db, "-shm"), b"stale shm").unwrap();

        let report = delete_all_voice_data_at(&db, std::slice::from_ref(&meetings)).unwrap();
        assert_eq!(report.profiles_deleted, 1);
        assert_eq!(report.samples_deleted, 1);
        assert_eq!(report.active_profiles_deleted, 1);
        assert_eq!(report.sidecars_deleted, 2);
        assert!(!sidecar.exists());
        assert!(!orphan.exists());
        assert!(!sqlite_aux_path(&db, "-wal").exists());
        assert!(!sqlite_aux_path(&db, "-shm").exists());

        let conn = open_db_at(&db).unwrap();
        assert!(list_profiles(&conn).unwrap().is_empty());
        assert!(list_voice_enrollments(&conn).unwrap().is_empty());
    }

    #[cfg(feature = "diarize")]
    #[test]
    #[ignore = "requires the local CAM++ ONNX model; exercised manually on macOS"]
    fn voice_embed_solo_real_wav_model_integration() {
        let root = tempfile::TempDir::new().unwrap();
        let wav = root.path().join("speaker.wav");
        let spec = hound::WavSpec {
            channels: 1,
            sample_rate: SOLO_SAMPLE_RATE,
            bits_per_sample: 16,
            sample_format: hound::SampleFormat::Int,
        };
        let mut writer = hound::WavWriter::create(&wav, spec).unwrap();
        for index in 0..SOLO_SAMPLE_RATE * 8 {
            let sample = ((index as f32 * 0.11).sin() * 8_000.0) as i16;
            writer.write_sample(sample).unwrap();
        }
        writer.finalize().unwrap();
        embed_solo_clip(&wav, &Config::default()).unwrap();
    }

    #[test]
    fn meeting_embeddings_roundtrip() {
        let dir = tempfile::TempDir::new().unwrap();
        let meeting = dir.path().join("2026-03-25-standup.md");
        std::fs::write(&meeting, "---\ntitle: test\n---\ntranscript").unwrap();

        let mut embeddings = std::collections::HashMap::new();
        embeddings.insert("SPEAKER_1".to_string(), vec![0.1f32, 0.2, 0.3]);
        embeddings.insert("SPEAKER_2".to_string(), vec![0.4f32, 0.5, 0.6]);

        save_meeting_embeddings(&meeting, &embeddings);

        let loaded = load_meeting_embeddings(&meeting).unwrap();
        assert_eq!(loaded.len(), 2);
        assert_eq!(loaded["SPEAKER_1"], vec![0.1f32, 0.2, 0.3]);
        assert_eq!(loaded["SPEAKER_2"], vec![0.4f32, 0.5, 0.6]);
    }

    #[test]
    fn meeting_embeddings_missing_returns_none() {
        let dir = tempfile::TempDir::new().unwrap();
        let meeting = dir.path().join("nonexistent.md");
        assert!(load_meeting_embeddings(&meeting).is_none());
    }

    #[test]
    fn sidecar_path_is_hidden_file() {
        let p = meeting_embeddings_sidecar_path(std::path::Path::new(
            "/tmp/meetings/2026-03-25-standup.md",
        ));
        assert_eq!(
            p.file_name().unwrap().to_str().unwrap(),
            ".2026-03-25-standup.embeddings"
        );
    }
}