koan-core 0.60.9

Core library for koan — bit-perfect music player. Audio engine, player, database, format strings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
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
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Once};
use std::time::SystemTime;

use figment::Figment;
use figment::providers::{Env, Format, Serialized, Toml};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    #[error("parse error: {0}")]
    Parse(#[from] toml::de::Error),
    #[error("serialize error: {0}")]
    Serialize(#[from] toml::ser::Error),
    #[error("config error: {0}")]
    Figment(#[from] Box<figment::Error>),
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
    pub library: LibraryConfig,
    pub playback: PlaybackConfig,
    pub remote: RemoteConfig,
    pub organize: OrganizeConfig,
    #[serde(alias = "visualiser")]
    pub visualizer: VisualizerConfig,
    pub graphql: GraphqlConfig,
    pub subsonic: SubsonicConfig,
    pub auth: AuthConfig,
    pub sharing: SharingConfig,
    pub mcp: McpConfig,
    pub push: PushConfig,
    pub devices: DevicesConfig,
    pub dsp: DspConfig,
    pub appearance: AppearanceConfig,
}

/// How the apps are drawn: Settings → Appearance.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AppearanceConfig {
    /// `"koan"`, the site's look throughout (see `docs/design/koan-theme.md`),
    /// or `"system"`, the platform's own look in koan's colours. Read at launch.
    pub theme: String,
    /// In the kōan theme, draw the app's icons beside navigation, tabs and
    /// controls; off, labels alone.
    pub theme_icons: bool,
    /// Take colour from the record playing: the wash behind the window and
    /// the accent. Off, there is no wash and the accent is koan's mint, in
    /// either theme.
    pub record_colours: bool,
    /// On the Mac in the kōan theme, the wash runs under the whole window:
    /// sidebar, toolbar, transport and lyrics drawn clear over it. Off, those
    /// keep grounds of their own. Provisional: a way to live with both while
    /// the look is decided, and expected to go once it is.
    pub wash_window: bool,
}

impl Default for AppearanceConfig {
    fn default() -> Self {
        Self {
            theme: "koan".into(),
            theme_icons: true,
            record_colours: true,
            wash_window: true,
        }
    }
}

/// Share links this koan serves itself.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SharingConfig {
    /// Where this server is reached from outside, e.g. `https://koan.example.com`.
    /// Share links are built on it; without it a server makes no links, since
    /// it cannot know which of its addresses a stranger can reach.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub public_url: Option<String>,
}

/// MCP clients signing in through this server's OAuth.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct McpConfig {
    /// The only hosts an MCP client may register a redirect to, besides this
    /// machine. Empty allows any HTTPS host: each approval then rests on the
    /// user recognising the host the consent page names.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub redirect_hosts: Vec<String>,
}

/// Push notifications to koan's iOS app, which reach a phone iOS has
/// suspended. Apple accepts them only signed with the key of the team that
/// ships the app, so only a server holding that key can send them; without one
/// a server reaches phones only while they are linked.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PushConfig {
    /// The APNs auth key (`AuthKey_XXXXXXXXXX.p8`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key_path: Option<PathBuf>,
    /// The key itself, PEM, for a deployment that hands secrets over as
    /// environment (`KOAN_PUSH__KEY`) rather than files.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,
    /// The key's ten-character id.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub key_id: String,
    /// The Apple developer team the key belongs to.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub team_id: String,
    /// The app's bundle id, which every push names.
    pub topic: String,
}

impl Default for PushConfig {
    fn default() -> Self {
        Self {
            key_path: None,
            key: None,
            key_id: String::new(),
            team_id: String::new(),
            topic: "cc.blit.koan".into(),
        }
    }
}

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

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct PlaybackConfig {
    pub replaygain: ReplayGainMode,
    /// UI render rate in frames-per-second (default: 60).
    /// Controls how often the TUI redraws. 30, 60, or 120 are typical values.
    pub target_fps: u8,
    /// Show an FPS counter overlay in the top-right corner.
    pub show_fps: bool,
    /// ReplayGain pre-amplification in dB. Applied on top of track/album gain.
    /// Positive values boost, negative values attenuate. Default: 0.0.
    pub pre_amp_db: f64,
    /// Fade out on pause and back in on resume, rather than cutting.
    pub fade_on_pause: bool,
    /// Output audio device name. None = system default.
    /// Persisted by name (not ID) since IDs can change across reboots.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_device: Option<String>,
    /// The UPnP renderer last picked as the output, by UDN, and its name for
    /// showing. Gone back to at launch if it is on the network; otherwise the
    /// music plays on `output_device`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub renderer: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub renderer_name: Option<String>,
    /// Look for UPnP renderers on the network. Off, none is found, so none
    /// can be played to.
    pub renderers: bool,
    /// Play silence: the output runs as usual, with every sample zeroed. For
    /// automated runs on a machine someone is using.
    pub muted: bool,
    /// Album art width in terminal columns (default: 24).
    /// Height is always width/2 (square via halfblock rendering).
    pub art_size: u16,
    /// Silence played after the output device changes sample rate, so the
    /// start of the track is not lost while the device relocks its clock.
    /// How long that takes is a property of the device.
    pub rate_switch_lead_in_ms: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReplayGainMode {
    Off,
    Track,
    Album,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct RemoteConfig {
    pub enabled: bool,
    pub url: String,
    pub username: String,
    /// Password — stored in config.local.toml (gitignored), not config.toml.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub password: String,
    /// An OpenSubsonic API key, signing in instead of the password. What
    /// joining with a koan invite stores. config.local.toml, like the password.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub api_key: String,
    /// The keypair this device proves itself with to the account's other
    /// devices on the local network: base64 of its Ed25519 PKCS#8. Made at
    /// each sign-in with an API key and dropped at sign-out, so the server's
    /// copy of the public key goes with the API key it is kept on. A secret,
    /// kept with the credentials. See `remote::proof`.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub device_key: String,
    /// Defaults to config_dir()/cache if empty.
    pub cache_dir: Option<PathBuf>,
    /// Parallel download workers for remote tracks (default: 5).
    pub download_workers: usize,
    /// Maximum cache size on disk. Human-readable: "50GB", "500MB", etc.
    /// None or empty = unlimited. Past it, whole albums are evicted, least
    /// recently used first, at startup and as downloads land.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cache_limit: Option<String>,
    /// Sync the library from the server on startup, and on a timer for a server
    /// that cannot say when it changes; a koan server tells its apps itself.
    ///
    /// Each run walks the library only if the server says it changed since the
    /// last walk, so it is cheap enough to run unattended.
    pub auto_sync: bool,
    /// Minutes between automatic syncs. 0 runs one at startup and no more.
    pub auto_sync_interval_mins: u64,
    /// Keep this device's queue in the account's play queue on the server,
    /// where Subsonic clients save theirs, so a queue can be picked up in
    /// another client or on another kōan device after a restart. Off by
    /// default: between kōan devices the queue already moves live over the
    /// link. Per device.
    pub play_queue: bool,
}

impl Default for LibraryConfig {
    fn default() -> Self {
        let music_dir = dirs::audio_dir().unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join("Music"))
                .unwrap_or_else(|| PathBuf::from("/Music"))
        });
        Self {
            folders: vec![music_dir],
        }
    }
}

impl Default for PlaybackConfig {
    fn default() -> Self {
        Self {
            replaygain: ReplayGainMode::Off,
            target_fps: 60,
            show_fps: false,
            pre_amp_db: 0.0,
            fade_on_pause: true,
            output_device: None,
            renderer: None,
            renderer_name: None,
            renderers: true,
            muted: false,
            art_size: 24,
            rate_switch_lead_in_ms: 1000,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct VisualizerConfig {
    pub enabled: bool,
    pub fps: u8,
    /// Visualizer mode by name: "bars" (default), or any name
    /// `VisualizerMode::parse` in koan-tui accepts.
    pub mode: String,
    /// Frequency scale: "bark" (default), "mel", "log", "linear".
    pub scale: String,
    /// Amplitude scale: "aweight" (default, A-weighted), "perceptual" (A-weighted + gamma), "sqrt", "linear".
    pub amplitude_scale: String,
    /// Bar decay half-life in milliseconds (how fast bars drop).
    pub bar_decay_ms: u32,
    /// Peak decay half-life in milliseconds (how long peaks linger).
    pub peak_decay_ms: u32,
    /// Color palette: "spectrum" (default), "mono", "fire", "neon".
    /// Controls the frequency-mapped color gradient on spectrum bars.
    pub palette: String,
    /// Reactivity multiplier (0.0..2.0, default 1.0).
    /// Scales all beat/spectrum-driven animation coefficients.
    /// 0.0 = static, 1.0 = normal, 2.0 = hypersensitive.
    pub reactivity: f32,
    /// Bass shake: camera jitter + scale pulse on bass hits.
    /// Applies to braille-rendered modes (oscilloscope, radial, wireframe, starfield, etc.).
    pub bass_shake: bool,
    /// Matrix overlay: replace all rendered characters with random matrix glyphs in green.
    /// Applies to any visualizer mode as a post-processing pass.
    pub matrix_overlay: bool,
    /// Beat-reactive background color on braille modes (starfield, wormhole, etc.).
    pub reactive_bg: bool,
}

impl Default for VisualizerConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            fps: 60,
            mode: "bars".into(),
            scale: "bark".into(),
            amplitude_scale: "aweight".into(),
            bar_decay_ms: 50,
            peak_decay_ms: 180,
            palette: "spectrum".into(),
            reactivity: 1.0,
            bass_shake: true,
            matrix_overlay: false,
            reactive_bg: false,
        }
    }
}

impl Default for RemoteConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            url: String::new(),
            username: String::new(),
            password: String::new(),
            api_key: String::new(),
            device_key: String::new(),
            cache_dir: None,
            download_workers: 5,
            cache_limit: None,
            auto_sync: true,
            auto_sync_interval_mins: 60,
            play_queue: false,
        }
    }
}

/// Parse a human-readable size string like "50GB", "500 MB", "1.5TB" into bytes.
/// Supports B, KB, MB, GB, TB (case-insensitive). Returns None for invalid input.
pub fn parse_size_bytes(s: &str) -> Option<u64> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }

    // Split into numeric part and suffix.
    let mut num_end = 0;
    for (i, c) in s.char_indices() {
        if c.is_ascii_digit() || c == '.' {
            num_end = i + c.len_utf8();
        } else if !c.is_whitespace() {
            break;
        }
    }

    let num_str = s[..num_end].trim();
    let suffix = s[num_end..].trim().to_ascii_uppercase();

    let value: f64 = num_str.parse().ok()?;
    let multiplier: u64 = match suffix.as_str() {
        "" | "B" => 1,
        "KB" | "K" => 1024,
        "MB" | "M" => 1024 * 1024,
        "GB" | "G" => 1024 * 1024 * 1024,
        "TB" | "T" => 1024 * 1024 * 1024 * 1024,
        _ => return None,
    };

    Some((value * multiplier as f64) as u64)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct OrganizeConfig {
    /// Named pattern preselected when an organize sheet or modal opens.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,
    /// Named patterns — keys are names, values are format strings.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub patterns: HashMap<String, String>,
    /// Move cover art, cue sheets and logs alongside the music they belong to.
    /// On by default: a folder's artwork is part of the release, and leaving it
    /// behind turns one album into two half-albums.
    #[serde(default = "default_true")]
    pub move_ancillary: bool,
}

impl Default for OrganizeConfig {
    fn default() -> Self {
        Self {
            default: None,
            patterns: HashMap::new(),
            move_ancillary: true,
        }
    }
}

/// GraphQL API server configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct GraphqlConfig {
    /// Enable the GraphQL API server alongside the TUI (default: true).
    /// Set to false for TUI-only mode (equivalent to --no-api).
    pub enabled: bool,
    /// GraphQL API port (default: 4000).
    pub port: u16,
    /// Bind address for the API server (default: 127.0.0.1).
    /// Use "0.0.0.0" to listen on all interfaces (NOT RECOMMENDED without auth).
    #[serde(default = "default_bind")]
    pub bind: std::net::IpAddr,
    /// Enable GraphiQL web IDE at GET /graphql.
    pub playground: bool,
    /// Require authentication for API access (default: true).
    /// When false, all requests are treated as admin. When true, JWT auth is enforced.
    pub auth_enabled: bool,
    /// Access token TTL (default: "15m"). Supports: "15m", "1h", "3600s".
    pub access_token_ttl: String,
    /// Refresh token TTL (default: "30d"). Supports: "30d", "7d", "720h".
    pub refresh_token_ttl: String,
    /// Allowed CORS origins. Empty = no cross-origin browser access at all.
    /// Example: ["https://music.example.com"]
    pub cors_origins: Vec<String>,
    /// Extra `Host:` values the server will answer to, beyond `localhost` and
    /// bare IP literals. Requests carrying any other Host are refused, which is
    /// what stops a DNS-rebinding page from reaching the API as same-origin.
    pub allowed_hosts: Vec<String>,
    /// Mark the session cookie `Secure`. Only set this when clients reach koan
    /// over HTTPS — browsers silently discard `Secure` cookies sent over plain
    /// `http://` to anything but localhost.
    pub cookie_secure: bool,
    /// A request header an authenticating reverse proxy (Authelia, Authentik,
    /// oauth2-proxy) sets to the signed-in username, e.g. `Remote-User`. The
    /// web UI signs that account in without asking for its password. Honoured
    /// only on connections from `proxy_auth_from`; empty turns it off.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub proxy_auth_header: String,
    /// The addresses or CIDR ranges the authenticating proxy connects from.
    /// The header is ignored on a connection from anywhere else.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub proxy_auth_from: Vec<String>,
    /// Expose the `organize*` mutations, which physically move files on disk.
    pub allow_organize: bool,
}

fn default_true() -> bool {
    true
}

fn default_bind() -> std::net::IpAddr {
    std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
}

impl Default for GraphqlConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            port: 4000,
            bind: default_bind(),
            playground: false,
            auth_enabled: true,
            access_token_ttl: "15m".into(),
            refresh_token_ttl: "30d".into(),
            cors_origins: Vec::new(),
            allowed_hosts: Vec::new(),
            cookie_secure: false,
            proxy_auth_header: String::new(),
            proxy_auth_from: Vec::new(),
            allow_organize: false,
        }
    }
}

/// Subsonic-compatible REST API.
///
/// Credentials are deliberately separate from `[remote]`: the Subsonic protocol
/// authenticates with `md5(password + salt)` over whatever transport the client
/// picked, so the secret has to be recoverable and is exposed to anyone who can
/// capture a request. Reusing the upstream Navidrome password would hand out
/// that account too.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct SubsonicConfig {
    /// Serve `/rest/*`. Off unless explicitly enabled.
    ///
    /// The routes are mounted on the GraphQL port. `port` adds a second
    /// listener for clients that expect Subsonic on one of its own.
    pub enabled: bool,
    /// Serve `/rest/*` on a dedicated port as well as the GraphQL one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub port: Option<u16>,
    /// Username Subsonic clients authenticate as.
    pub username: String,
    /// Shared secret, written by `koan subsonic setup`. Lives in
    /// config.local.toml, which is gitignored and `0600`.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub password: String,
    /// Transcode `stream` for clients that ask for a lower bitrate or another
    /// format. Off, every client gets the original file.
    pub transcode: bool,
    /// The `ffmpeg` transcoding runs, by name on `PATH` or by path. Without
    /// one, originals are served.
    pub ffmpeg: String,
}

impl Default for SubsonicConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            port: None,
            username: "koan".into(),
            password: String::new(),
            transcode: true,
            ffmpeg: "ffmpeg".into(),
        }
    }
}

/// Credentials for a remote koan server this machine signs in to.
///
/// The other direction from `GraphqlConfig`, which configures the server koan
/// *is*. Written by `koan auth login` and cleared by `koan auth logout`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct AuthConfig {
    /// Server the token below belongs to. One at a time.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub server: String,
    /// Refresh token, exchanged for short-lived access tokens. Revocable at the
    /// server, which is what separates it from a password.
    #[serde(skip_serializing_if = "String::is_empty")]
    pub refresh_token: String,
}

/// Controlling this device from others on the local network, and finding
/// them. Devices on the same account reach each other through the server
/// whatever this says.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DevicesConfig {
    /// Take part in the local network at all: listen, announce, look for other
    /// devices and dial them. Off, this device reaches others only through the
    /// server. For a shared network, and for test runs that relaunch an app
    /// over and over, which would otherwise announce it to every device in
    /// the house each time.
    pub nearby: bool,
    /// Listen on the local network and announce this device there, so any
    /// koan app on it can see what is playing and control it.
    pub discoverable: bool,
    /// The port listened on. Fixed so that an address typed into another
    /// device (over Tailscale, where nothing is announced) keeps working.
    pub port: u16,
    /// Devices to connect to by address, `host:port`: for networks that do
    /// not carry Bonjour, such as a tailnet.
    pub addresses: Vec<String>,
    /// What a kōan on the local network may have this device do, whoever is
    /// signed in there.
    pub nearby_control: NearbyControl,
    /// Keep the Mac app running in the menu bar once its window is closed, so
    /// other devices can still see and control this one. Quitting it then
    /// means this Mac is out of reach until it is opened again.
    pub keep_running: bool,
}

/// What devices on the local network may do with this one.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NearbyControl {
    /// Play, the queue, the output, the preset and the volume, and moving the
    /// music here or away: what a household network wants.
    #[default]
    Full,
    /// Play and the queue only, and a hand-off that stays on the network: for
    /// a network shared with strangers.
    Playback,
}

impl Default for DevicesConfig {
    fn default() -> Self {
        Self {
            nearby: true,
            discoverable: true,
            port: DEVICES_PORT,
            addresses: Vec::new(),
            nearby_control: NearbyControl::Full,
            keep_running: false,
        }
    }
}

/// "koan" on a phone keypad.
pub const DEVICES_PORT: u16 = 5626;

/// Equalisation and convolution for the listening setup: headphones, speakers,
/// a room. Each profile names the output devices it is for, so plugging in the
/// headphones selects their correction. A device no profile names plays
/// bit-perfect, untouched.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct DspConfig {
    /// Off bypasses every profile without forgetting any of them.
    pub enabled: bool,
    pub profiles: Vec<DspProfile>,
    /// Output devices whose AutoEQ suggestion was turned down. See
    /// `audio::dsp::autoeq::suggest`.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub autoeq_dismissed: Vec<String>,
    /// The tuning each output plays on top of its correction, the profile
    /// that lists it: its EQs, in the order listed, each switched on or off.
    /// This device's, as which profile an output plays is.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub tunings: Vec<DspOutputTuning>,
    /// The preset each output's correction and tuning were last set from,
    /// which they are compared with to say whether it was changed since.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub presets: Vec<DspOutputPreset>,
}

/// One EQ of an output's tuning.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DspOutputTuning {
    pub device: String,
    pub tuning: String,
    /// Switched off, it stays in the tuning and plays nothing.
    #[serde(default = "on", skip_serializing_if = "is_on")]
    pub on: bool,
}

fn on() -> bool {
    true
}

fn is_on(on: &bool) -> bool {
    *on
}

/// The preset an output was set from.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DspOutputPreset {
    pub device: String,
    pub preset: String,
}

impl Default for DspConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            profiles: Vec::new(),
            autoeq_dismissed: Vec::new(),
            tunings: Vec::new(),
            presets: Vec::new(),
        }
    }
}

impl DspProfile {
    /// The measurement and target a correction is built from: fitted, or
    /// saved before kōan fitted bands.
    pub fn measured(&self) -> Option<&DspMeasurement> {
        self.fitted.as_ref().or(self.measurement.as_ref())
    }
}

impl DspConfig {
    /// The profile for the output device called `device`, if DSP is on and
    /// one names it.
    pub fn profile_for(&self, device: &str) -> Option<&DspProfile> {
        if !self.enabled {
            return None;
        }
        self.profiles
            .iter()
            .find(|p| p.devices.iter().any(|d| d == device))
    }
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct DspProfile {
    pub name: String,
    /// Output devices, by name, that play through this profile.
    pub devices: Vec<String>,
    /// Gain before the filters. Unset, it is derived from the filters' peak
    /// gain, so that no boost can push a sample past full scale.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub preamp_db: Option<f64>,
    /// Run in order, at the output rate, ahead of the impulse responses.
    pub filters: Vec<DspFilter>,
    /// Impulse responses as WAV files, one per sample rate the correction was
    /// exported at; a relative path is read from beside the config. Each
    /// file's own rate is the rate it applies to.
    pub impulses: Vec<PathBuf>,
    /// The files it was imported from, by name, for showing where it came
    /// from. Nothing reads them again.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub source: Vec<String>,
    /// For a correction installed from AutoEQ: the target it was made for,
    /// and another to move it to. See `audio::dsp::targets`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target: Option<DspTarget>,
    /// Other profiles played first, in order: a headphone's correction and
    /// then taste on top of it, each switched on or off. A profile with
    /// layers is a stack. See `audio::dsp::Setup::load`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub layers: Vec<DspLayer>,
    /// A group rather than a stack of layers: exactly one of its layers
    /// plays, the one switched on, chosen as one picks a radio button.
    /// Whole presets imported together are a group: alternatives, not taste
    /// to play together.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub group: bool,
    /// A preset: a correction and its tuning saved together, its layers the
    /// correction first and then the tuning's EQs, each on or off. What an
    /// output is set from, and what the quick EQ menu lists.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub preset: bool,
    /// Whether it is the account's, kept on every device signed in to its
    /// kōan server, or this device's alone. Unset, it follows from what the
    /// profile is: see `audio::dsp::profiles::scope`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scope: Option<DspScope>,
    /// What every device calls it, made when it is first shared. Its name may
    /// change; this does not.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uid: Option<String>,
    /// The device that first shared it, by name: what tells two different
    /// profiles of one name apart when they meet.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    /// What it is for: correcting a headphone, or tuning on top of one.
    /// Unset, it follows from what the profile is: see
    /// `audio::dsp::profiles::role`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<DspRole>,
    /// A correction built from a headphone's measurement, kept as
    /// `measurement.csv` in the profile's folder, to `target`, as kōan
    /// saved one before it fitted bands: played as the bands squig.link's
    /// auto-EQ fits to it, which are not written back. See `fitted`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub measurement: Option<DspMeasurement>,
    /// A correction fitted to a headphone's measurement, kept as
    /// `measurement.csv` in the profile's folder, and `target`: its filters
    /// and preamp are the bands squig.link's auto-EQ fits, refitted when the
    /// target changes. A key of its own rather than `measurement`, so a
    /// build that knows only `measurement` plays the bands alone and not a
    /// correction of its own after them.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fitted: Option<DspMeasurement>,
    /// For a tuning: the target it was made against, by id. On a correction
    /// to another target, the difference between the two plays first, so
    /// the tuning sounds as it was made to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tuned_for: Option<String>,
    /// What an import made it, kept so an edit can be undone.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original: Option<DspOriginal>,
}

/// A profile as imported: its filters and preamp.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DspOriginal {
    pub filters: Vec<DspFilter>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub preamp_db: Option<f64>,
}

/// What a profile is for. A chain, a profile with its layers, corrects a
/// headphone once: two corrections would each undo the same headphone.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DspRole {
    /// Brings a headphone to a target: an AutoEQ install, a correction built
    /// from a measurement, or a finished EQ made for the headphone.
    Correction,
    /// Taste on top: a bass shelf, a tilt, bands made by hand.
    Tuning,
    /// A correction with a tuning already in it, as most finished presets
    /// are: Qudelix's "Lush", or a squig.link export to a target with a
    /// preference. Counts as the chain's correction; a tuning on top would
    /// add taste twice.
    Baked,
}

impl DspRole {
    /// Whether it brings a headphone to a target, so a chain has one.
    pub fn corrects(self) -> bool {
        matches!(self, Self::Correction | Self::Baked)
    }
}

/// A headphone's measurement and the target it is corrected to.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DspMeasurement {
    pub ear: DspEar,
    /// A target id: one that ships, or `added:<name>`.
    pub target: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DspEar {
    In,
    Over,
    Speaker,
}

/// The bounds a profile must keep to be played: what any real correction
/// asks, and nothing that would deafen a listener, starve the decode thread
/// or exhaust memory. `DspProfile::sanitize` brings every profile within
/// them, wherever it came from. The one place they are set.
pub mod dsp_bounds {
    use std::ops::RangeInclusive;
    pub const FREQ_HZ: RangeInclusive<f64> = 1.0..=48_000.0;
    pub const GAIN_DB: RangeInclusive<f64> = -30.0..=30.0;
    pub const Q: RangeInclusive<f64> = 0.01..=100.0;
    pub const DELAY_MS: RangeInclusive<f64> = 0.0..=2_000.0;
    /// Two seconds at 384 kHz.
    pub const DELAY_SAMPLES: RangeInclusive<f64> = 0.0..=768_000.0;
    /// A mix's linear gain: ±30 dB.
    pub const MIX_GAIN: f64 = 31.63;
    pub const CHANNELS: u16 = 64;
    /// Parametric bands applying to any one channel.
    pub const BANDS_PER_CHANNEL: usize = 64;
    pub const FILTERS: usize = 256;
    pub const GRAPHIC_POINTS: usize = 2_048;
    pub const LAYERS: usize = 32;
    pub const NAME: usize = 128;

    // What a whole chain, layers and target step included, may add up to:
    // each value within bounds can still sum past what a device can hold
    // or keep up with.

    /// Delay on any one channel, every delay summed, in ms. A delay in
    /// samples counts at 44.1 kHz, the lowest rate it could mean the most
    /// time at.
    pub const CHAIN_DELAY_MS: f64 = 2_000.0;
    /// Graphic curves on any one channel: each is a minimum-phase FIR of up
    /// to a second of taps at the output rate. Two is a target step and a
    /// curve of the user's own.
    pub const CHAIN_GRAPHICS: usize = 2;
    /// Mixes in a chain.
    pub const CHAIN_MIXES: usize = 8;
    /// Taps of an impulse response, per route: the longest a shipped room
    /// correction is known to need, Harman's 780 pack at 192 kHz, which is
    /// over five seconds at 48 kHz, longer than any room's decay.
    pub const IMPULSE_TAPS: usize = 262_145;
}

/// Bring a whole chain within the `dsp_bounds` budgets, saying what was
/// changed: delays past the total on a channel shortened or dropped,
/// graphic curves and mixes past their counts dropped.
pub fn budget(filters: &mut Vec<DspFilter>) -> Vec<String> {
    use dsp_bounds as b;
    let mut notes = Vec::new();
    let mut note = |n: String| {
        if !notes.contains(&n) {
            notes.push(n);
        }
    };
    let channels = b::CHANNELS as usize;
    let mut delay = vec![0.0f64; channels];
    let mut graphics = vec![0usize; channels];
    let mut mixes = 0;
    let on = |cs: &[u16], c: usize| cs.is_empty() || cs.contains(&(c as u16));
    filters.retain_mut(|f| match f {
        DspFilter::Delay(d) => {
            let ms = d.ms + d.samples / 44.1;
            let room = (0..channels)
                .filter(|c| on(&d.channels, *c))
                .map(|c| b::CHAIN_DELAY_MS - delay[c])
                .fold(b::CHAIN_DELAY_MS, f64::min)
                .max(0.0);
            if ms > room {
                note(format!(
                    "delays shortened to {} ms in all",
                    b::CHAIN_DELAY_MS
                ));
                if room <= 0.0 {
                    return false;
                }
                // Shortened in proportion, keeping how it was given.
                let scale = room / ms;
                d.ms *= scale;
                d.samples = (d.samples * scale).floor();
            }
            let ms = d.ms + d.samples / 44.1;
            for (c, total) in delay.iter_mut().enumerate() {
                if on(&d.channels, c) {
                    *total += ms;
                }
            }
            true
        }
        DspFilter::Graphic(g) => {
            let full =
                (0..channels).any(|c| on(&g.channels, c) && graphics[c] == b::CHAIN_GRAPHICS);
            if full {
                note(format!(
                    "graphic EQs past {} a channel dropped",
                    b::CHAIN_GRAPHICS
                ));
                return false;
            }
            for (c, n) in graphics.iter_mut().enumerate() {
                if on(&g.channels, c) {
                    *n += 1;
                }
            }
            true
        }
        DspFilter::Mix(_) => {
            mixes += 1;
            if mixes > b::CHAIN_MIXES {
                note(format!("mixes past {} dropped", b::CHAIN_MIXES));
                return false;
            }
            true
        }
        DspFilter::Band(_) => true,
    });
    notes
}

impl DspProfile {
    /// Bring the profile within `dsp_bounds`, saying what was changed: values
    /// past a bound are clamped to it, ones that are not finite are dropped
    /// with what holds them, and filters, bands, layers and points past a
    /// cap are cut off at it. Every profile is played as this leaves it,
    /// wherever it came from, so one that would deafen a listener, starve
    /// the decode thread or exhaust memory still plays, adjusted, and its
    /// page says how.
    pub fn sanitize(&mut self) -> Vec<String> {
        use dsp_bounds as b;
        let mut notes: Vec<String> = Vec::new();
        let mut note = |n: String| {
            if !notes.contains(&n) {
                notes.push(n);
            }
        };
        let mut clamp = |what: &str, v: &mut f64, r: &std::ops::RangeInclusive<f64>, unit: &str| {
            let c = v.clamp(*r.start(), *r.end());
            if c != *v {
                note(format!(
                    "{what} clamped to {}{unit}",
                    if *v > *r.end() { r.end() } else { r.start() }
                ));
                *v = c;
            }
        };
        let finite = |v: f64| v.is_finite();
        let mut dropped = Vec::new();

        let cleaned: String = self
            .name
            .chars()
            .filter(|c| !c.is_control())
            .take(b::NAME)
            .collect();
        let cleaned = if cleaned.trim().is_empty() {
            "Profile".to_owned()
        } else {
            cleaned
        };
        if cleaned != self.name {
            dropped.push("name shortened".to_owned());
            self.name = cleaned;
        }
        if let Some(db) = self.preamp_db.as_mut() {
            if finite(*db) {
                clamp("preamp", db, &b::GAIN_DB, " dB");
            } else {
                self.preamp_db = None;
                dropped.push("preamp dropped".to_owned());
            }
        }
        let bad_channel = |cs: &[u16]| cs.iter().any(|c| *c >= b::CHANNELS);
        let mut per_channel = [0usize; b::CHANNELS as usize];
        let before = self.filters.len();
        let mut kept = Vec::with_capacity(before.min(b::FILTERS));
        for mut filter in std::mem::take(&mut self.filters) {
            if kept.len() == b::FILTERS {
                dropped.push(format!("filters past {} dropped", b::FILTERS));
                break;
            }
            let keep = match &mut filter {
                DspFilter::Band(f) => {
                    if ![f.freq, f.gain_db, f.q].into_iter().all(finite) || bad_channel(&f.channels)
                    {
                        false
                    } else {
                        let on =
                            |c: usize| f.channels.is_empty() || f.channels.contains(&(c as u16));
                        if (0..b::CHANNELS as usize)
                            .any(|c| on(c) && per_channel[c] == b::BANDS_PER_CHANNEL)
                        {
                            dropped.push(format!(
                                "bands past {} a channel dropped",
                                b::BANDS_PER_CHANNEL
                            ));
                            continue;
                        } else {
                            for (c, n) in per_channel.iter_mut().enumerate() {
                                if on(c) {
                                    *n += 1;
                                }
                            }
                            clamp("a band's frequency", &mut f.freq, &b::FREQ_HZ, " Hz");
                            clamp("a band's gain", &mut f.gain_db, &b::GAIN_DB, " dB");
                            clamp("a band's Q", &mut f.q, &b::Q, "");
                            true
                        }
                    }
                }
                DspFilter::Delay(d) => {
                    if !finite(d.ms) || !finite(d.samples) || bad_channel(&d.channels) {
                        false
                    } else {
                        clamp("delay", &mut d.ms, &b::DELAY_MS, " ms");
                        clamp("delay", &mut d.samples, &b::DELAY_SAMPLES, " samples");
                        true
                    }
                }
                DspFilter::Mix(m) => {
                    m.outputs.truncate(b::CHANNELS as usize);
                    for o in &mut m.outputs {
                        o.retain(|(c, g)| *c < b::CHANNELS && finite(*g));
                        o.truncate(b::CHANNELS as usize);
                        for (_, g) in o.iter_mut() {
                            clamp("a mix's gain", g, &(-b::MIX_GAIN..=b::MIX_GAIN), "");
                        }
                    }
                    true
                }
                DspFilter::Graphic(g) => {
                    if bad_channel(&g.channels) {
                        false
                    } else {
                        g.points.retain(|(hz, db)| finite(*hz) && finite(*db));
                        if g.points.len() > b::GRAPHIC_POINTS {
                            g.points.truncate(b::GRAPHIC_POINTS);
                            dropped.push(format!(
                                "graphic EQ points past {} dropped",
                                b::GRAPHIC_POINTS
                            ));
                        }
                        for (hz, db) in &mut g.points {
                            clamp(
                                "a graphic EQ's frequency",
                                hz,
                                &(0.0..=*b::FREQ_HZ.end()),
                                " Hz",
                            );
                            clamp("a graphic EQ's gain", db, &b::GAIN_DB, " dB");
                        }
                        true
                    }
                }
            };
            if keep {
                kept.push(filter);
            } else {
                dropped.push("a filter that could not play dropped".to_owned());
            }
        }
        self.filters = kept;
        for n in budget(&mut self.filters) {
            dropped.push(n);
        }
        if self.layers.len() > b::LAYERS {
            self.layers.truncate(b::LAYERS);
            dropped.push(format!("layers past {} dropped", b::LAYERS));
        }
        self.layers.retain(|l| l.profile.chars().count() <= b::NAME);
        if self
            .origin
            .as_ref()
            .is_some_and(|o| o.chars().count() > b::NAME)
        {
            self.origin = None;
        }
        if self
            .tuned_for
            .as_ref()
            .is_some_and(|t| t.chars().count() > b::NAME)
        {
            self.tuned_for = None;
            dropped.push("tuned-for target dropped".to_owned());
        }
        if self
            .measurement
            .as_ref()
            .is_some_and(|m| m.target.chars().count() > b::NAME)
        {
            self.measurement = None;
            dropped.push("measurement's target dropped".to_owned());
        }
        if self
            .fitted
            .as_ref()
            .is_some_and(|m| m.target.chars().count() > b::NAME)
        {
            self.fitted = None;
            dropped.push("measurement's target dropped".to_owned());
        }
        if let Some(t) = &mut self.target {
            let long = |s: &str| s.chars().count() > b::NAME;
            if long(&t.made_for) {
                self.target = None;
                dropped.push("target dropped".to_owned());
            } else if t.chosen.as_deref().is_some_and(long) {
                t.chosen = None;
                dropped.push("chosen target dropped".to_owned());
            }
        }
        // The original plays when reverted to: held to the same bounds.
        if let Some(o) = self.original.take() {
            let mut kept = DspProfile {
                filters: o.filters,
                preamp_db: o.preamp_db,
                ..Default::default()
            };
            kept.sanitize();
            self.original = Some(DspOriginal {
                filters: kept.filters,
                preamp_db: kept.preamp_db,
            });
        }
        for d in dropped {
            note(d);
        }
        notes
    }

    /// As played: `sanitize`d, leaving this one as it is.
    pub fn sanitized(&self) -> Self {
        let mut p = self.clone();
        p.sanitize();
        p
    }
}

/// Where a profile is kept.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DspScope {
    /// The account's: synced through its kōan server to its other devices.
    Everywhere,
    /// This device's: never uploaded.
    Device,
}

/// One profile played as part of another.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DspLayer {
    pub profile: String,
    #[serde(default = "layer_on")]
    pub on: bool,
}

fn layer_on() -> bool {
    true
}

/// The target a correction was made for, and the one chosen in its place.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DspTarget {
    /// One of the targets koan ships, by id.
    pub made_for: String,
    /// A target koan ships, or one added (`added:<name>`). Unset, or the same
    /// as `made_for`, the correction plays as it was made.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chosen: Option<String>,
}

/// One step of a profile's processing. Bands on different channels commute;
/// a mix does not, which is why the steps are an ordered list.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum DspFilter {
    Delay(Delay),
    Mix(Mix),
    Graphic(GraphicEq),
    #[serde(untagged)]
    Band(EqFilter),
}

impl DspFilter {
    /// The channels it applies to, from 0. Empty is every channel; a mix
    /// names its own.
    pub fn channels(&self) -> &[u16] {
        match self {
            Self::Band(f) => &f.channels,
            Self::Delay(d) => &d.channels,
            Self::Graphic(g) => &g.channels,
            Self::Mix(_) => &[],
        }
    }

    pub fn channels_mut(&mut self) -> Option<&mut Vec<u16>> {
        match self {
            Self::Band(f) => Some(&mut f.channels),
            Self::Delay(d) => Some(&mut d.channels),
            Self::Graphic(g) => Some(&mut g.channels),
            Self::Mix(_) => None,
        }
    }
}

impl From<EqFilter> for DspFilter {
    fn from(f: EqFilter) -> Self {
        Self::Band(f)
    }
}

/// A fixed delay: `ms` and `samples` added together, at the output rate.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Delay {
    #[serde(skip_serializing_if = "is_zero")]
    pub ms: f64,
    #[serde(skip_serializing_if = "is_zero")]
    pub samples: f64,
    /// Keep the fraction of a sample, through an allpass, rather than round
    /// to the nearest whole one.
    #[serde(skip_serializing_if = "std::ops::Not::not")]
    pub subsample: bool,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub channels: Vec<u16>,
}

fn is_zero(v: &f64) -> bool {
    *v == 0.0
}

/// Channels made from others. `outputs[o]` is what channel `o` becomes, as
/// `(input channel, linear gain)` pairs read before any is written; an empty
/// list is silence. Channels past the end of `outputs` pass through.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Mix {
    pub outputs: Vec<Vec<(u16, f64)>>,
}

/// A curve of `(Hz, dB)` points, as Equalizer APO's `GraphicEQ:` gives one,
/// run as a minimum-phase FIR designed at the output rate. Between points the
/// gain is interpolated against log frequency; past the ends it holds.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct GraphicEq {
    pub points: Vec<(f64, f64)>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub channels: Vec<u16>,
}

/// One parametric band, as AutoEQ and Equalizer APO describe it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EqFilter {
    #[serde(rename = "type")]
    pub kind: EqFilterKind,
    /// Centre or corner frequency in Hz.
    pub freq: f64,
    #[serde(default)]
    pub gain_db: f64,
    #[serde(default = "default_q")]
    pub q: f64,
    /// The channels it applies to, from 0. Empty is every channel.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub channels: Vec<u16>,
}

fn default_q() -> f64 {
    std::f64::consts::FRAC_1_SQRT_2
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EqFilterKind {
    Peaking,
    LowShelf,
    HighShelf,
    LowPass,
    HighPass,
    Notch,
    BandPass,
    AllPass,
    /// First-order sections, 6 dB per octave. The shelves are at half their
    /// gain at `freq`; `q` is not used.
    LowShelfFirstOrder,
    HighShelfFirstOrder,
    LowPassFirstOrder,
    HighPassFirstOrder,
    AllPassFirstOrder,
    /// Flat gain, `gain_db`: a channel's own preamp.
    Gain,
}

impl EqFilterKind {
    /// As written in the config.
    pub fn name(self) -> &'static str {
        match self {
            Self::Peaking => "peaking",
            Self::LowShelf => "low_shelf",
            Self::HighShelf => "high_shelf",
            Self::LowPass => "low_pass",
            Self::HighPass => "high_pass",
            Self::Notch => "notch",
            Self::BandPass => "band_pass",
            Self::AllPass => "all_pass",
            Self::LowShelfFirstOrder => "low_shelf_first_order",
            Self::HighShelfFirstOrder => "high_shelf_first_order",
            Self::LowPassFirstOrder => "low_pass_first_order",
            Self::HighPassFirstOrder => "high_pass_first_order",
            Self::AllPassFirstOrder => "all_pass_first_order",
            Self::Gain => "gain",
        }
    }
}

/// Which of the two files a setting is written to when koan changes it itself.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Layer {
    /// `config.toml` — taste, meaningful on any machine, safe in a dotfiles repo.
    Shared,
    /// `config.local.toml` — belongs to this machine and nowhere else.
    Machine,
}

/// Where the setting at a dotted path belongs.
///
/// `config.toml` is meant to be committed to a dotfiles repo, so three kinds of
/// setting have no business in it: secrets, anything naming this machine's
/// hardware, paths or account, and UI state that a keypress flips — the last
/// kind would rewrite the shared file every session and land on the next
/// machine as someone else's window size.
///
/// Anything not named here is taste, and taste travels.
pub fn layer_of(path: &str) -> Layer {
    match path {
        // Secrets.
        "remote.password"
        | "remote.api_key"
        | "remote.device_key"
        | "subsonic.password"
        | "auth.refresh_token"
        | "push.key"
        | "push.key_path"
        | "push.key_id"
        | "push.team_id"
        // This machine's paths, disk and account.
        | "library.folders"
        | "remote.enabled"
        | "remote.url"
        | "remote.username"
        | "remote.cache_dir"
        | "remote.cache_limit"
        // Whether this device follows the account's saved play queue.
        | "remote.play_queue"
        // This machine's hardware.
        | "playback.output_device"
        | "playback.renderer"
        | "playback.renderer_name"
        | "playback.renderers"
        | "playback.muted"
        | "playback.rate_switch_lead_in_ms"
        // Which machine serves Subsonic, and as whom. Enabling a REST API is a
        // decision about one host, and the secret guarding it is per-machine.
        | "subsonic.enabled"
        | "subsonic.port"
        | "subsonic.username"
        | "subsonic.transcode"
        | "subsonic.ffmpeg"
        // Whether this machine is open to its network, and where others are.
        | "devices.nearby"
        | "devices.discoverable"
        | "devices.port"
        | "devices.addresses"
        | "devices.nearby_control"
        | "devices.keep_running"
        // Which koan server this machine signs in to.
        | "auth.server"
        // Volatile: UI state behind a keybind or a mouse drag.
        | "playback.art_size"
        | "visualizer.enabled"
        | "visualizer.mode"
        | "visualizer.matrix_overlay"
        | "visualizer.bass_shake" => Layer::Machine,
        // The listening setup: these headphones, this room.
        p if p == "dsp" || p.starts_with("dsp.") => Layer::Machine,
        _ => Layer::Shared,
    }
}

/// Mtimes of the two files `figment()` layers. Keyed on these so a config
/// edited by hand is picked up without koan being told about it; `KOAN_*` env
/// vars are not tracked, since they are fixed for the life of the process.
/// Which directory, and its two files' mtimes. The directory is part of it:
/// two directories' files can share an mtime, and a read in one must never
/// be served what was read in the other.
type ConfigStamp = (PathBuf, Option<SystemTime>, Option<SystemTime>);

type CachedConfig = Option<(ConfigStamp, Arc<Config>)>;

static CONFIG_CACHE: LazyLock<parking_lot::RwLock<CachedConfig>> =
    LazyLock::new(|| parking_lot::RwLock::new(None));

/// Moved by every invalidation. A read keeps what it loaded only if this has
/// not moved since it began: the stamp alone cannot tell a directory switched
/// away and back again mid-read, which leaves the stamp as it was and the
/// config loaded from the other directory.
static CONFIG_GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

fn config_stamp() -> ConfigStamp {
    let (base, local) = stamp_of(&config_file_path(), &config_local_file_path());
    (config_dir(), base, local)
}

/// A file that does not exist stamps as `None`, so creating one is a change.
fn stamp_of(base: &Path, local: &Path) -> (Option<SystemTime>, Option<SystemTime>) {
    let mtime = |p: &Path| fs::metadata(p).and_then(|m| m.modified()).ok();
    (mtime(base), mtime(local))
}

impl Config {
    /// Build the figment provider chain:
    /// defaults → config.toml → config.local.toml → KOAN_* env vars.
    ///
    /// Env vars use `KOAN_` prefix with `__` as section separator:
    ///   KOAN_REMOTE__PASSWORD, KOAN_GRAPHQL__PORT, KOAN_PLAYBACK__TARGET_FPS, etc.
    fn figment() -> Figment {
        let base_path = config_file_path();
        let local_path = config_local_file_path();

        Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(&base_path))
            .merge(Toml::file(&local_path))
            .merge(Env::prefixed("KOAN_").split("__"))
    }

    /// Load config from all layers: defaults → config.toml → config.local.toml → KOAN_* env vars.
    pub fn load() -> Result<Self, ConfigError> {
        let cfg: Self = Self::figment()
            .extract()
            .map_err(|e| ConfigError::Figment(Box::new(e)))?;

        // Security: refuse to start if config files containing secrets are
        // tracked by git.
        check_secrets_in_git();

        Ok(cfg)
    }

    /// Load config, logging and falling back to defaults on error.
    ///
    /// Served from the cache, so what this costs is a clone of the struct
    /// rather than two file reads and a figment merge.
    pub fn load_or_default() -> Self {
        (*Self::cached()).clone()
    }

    /// The merged config, reloaded only when it changed on disk.
    ///
    /// `load()` re-reads both TOML files and re-runs the whole figment merge,
    /// and koan reaches it from paths that run per frame — `library_folders()`
    /// is read from a SwiftUI list body. Callers that want to avoid even the
    /// clone `load_or_default()` does can hold this `Arc`.
    pub fn cached() -> Arc<Config> {
        let generation = CONFIG_GENERATION.load(std::sync::atomic::Ordering::SeqCst);
        let stamp = config_stamp();
        if let Some((seen, cfg)) = CONFIG_CACHE.read().as_ref()
            && *seen == stamp
        {
            return cfg.clone();
        }

        let cfg = Arc::new(Self::load().unwrap_or_else(|e| {
            log::warn!("failed to load config, using defaults: {}", e);
            Self::default()
        }));
        // Kept only if nothing moved while it was read: a switch of
        // directory or a write between the stamp and the load would keep one
        // directory's config under the other's stamp. Checked under the lock
        // a switch takes to drop the cache, so none slips in between.
        let mut cache = CONFIG_CACHE.write();
        if CONFIG_GENERATION.load(std::sync::atomic::Ordering::SeqCst) == generation
            && config_stamp() == stamp
        {
            *cache = Some((stamp, cfg.clone()));
        }
        cfg
    }

    /// Drop the cached config. koan's own writes invalidate explicitly rather
    /// than relying on the mtime, which can land in the same filesystem tick as
    /// the read before it.
    pub fn invalidate_cache() {
        let mut cache = CONFIG_CACHE.write();
        CONFIG_GENERATION.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        *cache = None;
    }

    /// Load from a specific TOML file (no env var overlay).
    pub fn load_from(path: &Path) -> Result<Self, ConfigError> {
        let contents = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&contents)?;
        Ok(config)
    }

    /// The two files merged, without the env layer.
    ///
    /// `persist` diffs against this rather than against `config.toml` alone, so
    /// a mutation setting a value the user already has writes nothing at all.
    /// `KOAN_*` is left out because there is no file to write it back to.
    fn from_files() -> Result<Self, ConfigError> {
        Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(config_file_path()))
            .merge(Toml::file(config_local_file_path()))
            .extract()
            .map_err(|e| ConfigError::Figment(Box::new(e)))
    }

    /// Apply a mutation and write each changed setting to the file that owns it.
    ///
    /// Only what the closure actually changed is written, so comments, layout
    /// and every untouched key survive — including the commented-out defaults
    /// `koan config init` leaves as a reference. `layer_of` decides the file.
    ///
    /// A shared write also clears any copy of that key from
    /// `config.local.toml`: the local layer wins, so leaving one there would
    /// make the write silently do nothing. A machine write clears the key from
    /// `config.toml` for the same reason in reverse, which drains settings that
    /// older versions of koan wrongly wrote to the shared file.
    pub fn persist<F>(mutate: F) -> Result<(), ConfigError>
    where
        F: FnOnce(&mut Config),
    {
        // Each write reads both files and writes them back whole, so two at
        // once would lose whichever finished first: the player's volume and a
        // setting changed in the app, say.
        static ONE_AT_A_TIME: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
        let _one = ONE_AT_A_TIME.lock();
        let before = Self::from_files()?;
        let mut after = before.clone();
        mutate(&mut after);

        let mut changes = Vec::new();
        diff_into(
            "",
            &toml::Value::try_from(&before)?,
            &toml::Value::try_from(&after)?,
            &mut changes,
        );
        if changes.is_empty() {
            return Ok(());
        }

        let base_path = config_file_path();
        let local_path = config_local_file_path();
        let mut base = read_document(&base_path)?;
        let mut local = read_document(&local_path)?;

        for (path, value) in &changes {
            let (target, other) = match layer_of(path) {
                Layer::Shared => (&mut base, &mut local),
                Layer::Machine => (&mut local, &mut base),
            };
            match value {
                Some(v) => doc_set(target, path, v),
                None => doc_remove(target, path),
            }
            doc_remove(other, path);
        }

        write_document(&base_path, &base, false)?;
        write_document(&local_path, &local, true)?;
        Self::invalidate_cache();
        Ok(())
    }

    /// Resolved cache directory — the explicit setting, or `default_cache_dir`.
    pub fn cache_dir(&self) -> PathBuf {
        self.remote
            .cache_dir
            .clone()
            .unwrap_or_else(default_cache_dir)
    }

    /// Parsed cache limit in bytes, or None if unlimited.
    pub fn cache_limit_bytes(&self) -> Option<u64> {
        self.remote
            .cache_limit
            .as_deref()
            .and_then(parse_size_bytes)
    }
}

/// Collect the leaf settings that differ between two serialized configs, as
/// `(dotted path, new value)`. `None` means the key is gone and should be
/// removed rather than written — which is how an emptied password or a cleared
/// output device reaches the file as an absent key rather than a blank one.
fn diff_into(
    prefix: &str,
    before: &toml::Value,
    after: &toml::Value,
    out: &mut Vec<(String, Option<toml::Value>)>,
) {
    let (b, a) = match (before.as_table(), after.as_table()) {
        (Some(b), Some(a)) => (b, a),
        _ => {
            if before != after {
                out.push((prefix.to_string(), Some(after.clone())));
            }
            return;
        }
    };

    let empty = toml::Value::Table(toml::map::Map::new());
    for key in b
        .keys()
        .chain(a.keys())
        .collect::<std::collections::BTreeSet<_>>()
    {
        let path = if prefix.is_empty() {
            key.clone()
        } else {
            format!("{prefix}.{key}")
        };
        match (b.get(key), a.get(key)) {
            (Some(bv), Some(av)) => diff_into(&path, bv, av, out),
            // Newly present: recurse into tables so a new pattern writes one
            // key rather than replacing the whole table.
            (None, Some(av)) => diff_into(&path, &empty, av, out),
            (Some(_), None) => out.push((path, None)),
            (None, None) => unreachable!("key came from one of the two tables"),
        }
    }
}

fn read_document(path: &Path) -> Result<toml_edit::DocumentMut, ConfigError> {
    let Ok(contents) = fs::read_to_string(path) else {
        return Ok(toml_edit::DocumentMut::new());
    };
    contents
        .parse::<toml_edit::DocumentMut>()
        .map_err(|e| ConfigError::Io(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))
}

/// Write a document, skipping files that would be created empty. `secret` marks
/// the file 0o600 — it is the one that holds passwords.
fn write_document(
    path: &Path,
    doc: &toml_edit::DocumentMut,
    secret: bool,
) -> Result<(), ConfigError> {
    let contents = doc.to_string();
    if contents.trim().is_empty() && !path.exists() {
        return Ok(());
    }
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    // Written beside the file and renamed over it, so a read never sees it
    // half written.
    let target = link_target(path);
    let temp = unique_beside(&target);
    let written = write_new(&temp, &target, contents.as_bytes(), secret)
        .and_then(|()| fs::rename(&temp, &target));
    if let Err(e) = written {
        let _ = fs::remove_file(&temp);
        return Err(e.into());
    }
    #[cfg(target_os = "tvos")]
    kept::written(path, contents.as_bytes());
    Ok(())
}

/// Where a write to `path` lands: a symlink, as dotfiles keep, is followed
/// rather than replaced, even when what it points at does not exist yet.
fn link_target(path: &Path) -> PathBuf {
    let mut target = path.to_path_buf();
    for _ in 0..40 {
        let Ok(next) = fs::read_link(&target) else {
            break;
        };
        target = match target.parent() {
            Some(parent) if next.is_relative() => parent.join(next),
            _ => next,
        };
    }
    target
}

/// A name beside `target` no other writer, in this process or another, is
/// using.
fn unique_beside(target: &Path) -> PathBuf {
    static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let name = target
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    target.with_file_name(format!(".{name}.{}.{n}.tmp", std::process::id()))
}

/// Create `temp` holding `contents`, never readable by more than `target`
/// already is: a secret file is 0600 from the moment it exists, any other
/// keeps the mode the file it replaces had.
fn write_new(temp: &Path, target: &Path, contents: &[u8], secret: bool) -> std::io::Result<()> {
    use std::io::Write as _;
    let mut options = fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    let kept_mode = {
        use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
        let kept_mode = (!secret)
            .then(|| fs::metadata(target).ok())
            .flatten()
            .map(|m| m.permissions().mode() & 0o7777);
        options.mode(if secret {
            0o600
        } else {
            kept_mode.unwrap_or(0o666)
        });
        kept_mode
    };
    #[cfg(not(unix))]
    let _ = (target, secret);
    let mut file = options.open(temp)?;
    file.write_all(contents)?;
    // The umask can narrow the mode asked for at creation; the file being
    // replaced had what it had.
    #[cfg(unix)]
    if let Some(mode) = kept_mode {
        use std::os::unix::fs::PermissionsExt;
        file.set_permissions(fs::Permissions::from_mode(mode))?;
    }
    Ok(())
}

fn implicit_table() -> toml_edit::Item {
    let mut table = toml_edit::Table::new();
    // Implicit: the header prints only if the table ends up holding something,
    // so writing a nested key never leaves a bare `[organize]` behind.
    table.set_implicit(true);
    toml_edit::Item::Table(table)
}

fn doc_set(doc: &mut toml_edit::DocumentMut, path: &str, value: &toml::Value) {
    let segments: Vec<&str> = path.split('.').collect();
    let (last, parents) = segments.split_last().expect("a diffed path is never empty");

    let mut table = doc.as_table_mut();
    for segment in parents {
        let item = table.entry(segment).or_insert_with(implicit_table);
        // A scalar sitting where a section belongs is malformed either way;
        // the setting koan is writing wins.
        if !item.is_table() {
            *item = implicit_table();
        }
        table = item.as_table_mut().expect("just ensured it is a table");
    }
    // Comments attach to the key, and `insert` replaces the key. Overwrite the
    // value in place where one already exists so the line keeps its notes.
    match table.get_mut(last) {
        Some(existing) => *existing = to_edit_item(value),
        None => {
            table.insert(last, to_edit_item(value));
        }
    }
}

/// A list of tables — DSP profiles — is written as `[[sections]]`, and any list
/// of tables inside one a line per entry, so the file stays editable by hand.
fn to_edit_item(value: &toml::Value) -> toml_edit::Item {
    let toml::Value::Array(items) = value else {
        return toml_edit::value(to_edit_value(value));
    };
    if items.is_empty() || !items.iter().all(toml::Value::is_table) {
        return toml_edit::value(to_edit_value(value));
    }
    let mut sections = toml_edit::ArrayOfTables::new();
    for item in items {
        let mut section = toml_edit::Table::new();
        for (k, v) in item.as_table().expect("checked above") {
            let mut v = to_edit_value(v);
            if let toml_edit::Value::Array(list) = &mut v
                && list.iter().any(toml_edit::Value::is_inline_table)
            {
                for entry in list.iter_mut() {
                    entry.decor_mut().set_prefix("\n    ");
                    if let Some(t) = entry.as_inline_table_mut() {
                        t.sort_values_by(|a, _, b, _| key_rank(a).cmp(&key_rank(b)));
                    }
                }
                list.set_trailing("\n");
                list.set_trailing_comma(true);
            }
            section.insert(k, toml_edit::value(v));
        }
        section.sort_values_by(|a, _, b, _| key_rank(a).cmp(&key_rank(b)));
        sections.push(section);
    }
    toml_edit::Item::ArrayOfTables(sections)
}

/// What an entry is reads first: a profile's name, a band's type.
fn key_rank(key: &toml_edit::Key) -> (u8, &str) {
    let k = key.get();
    (if k == "name" || k == "type" { 0 } else { 1 }, k)
}

fn doc_remove(doc: &mut toml_edit::DocumentMut, path: &str) {
    let segments: Vec<&str> = path.split('.').collect();
    let (last, parents) = segments.split_last().expect("a diffed path is never empty");

    let mut table = doc.as_table_mut();
    for segment in parents {
        match table.get_mut(segment).and_then(|i| i.as_table_mut()) {
            Some(child) => table = child,
            None => return,
        }
    }
    // An emptied table keeps its header: it still carries the commented-out
    // defaults `koan config init` wrote, and those are the reference.
    table.remove(last);
}

fn to_edit_value(value: &toml::Value) -> toml_edit::Value {
    match value {
        toml::Value::String(s) => s.as_str().into(),
        toml::Value::Integer(i) => (*i).into(),
        toml::Value::Float(f) => (*f).into(),
        toml::Value::Boolean(b) => (*b).into(),
        toml::Value::Datetime(d) => d.to_string().into(),
        toml::Value::Array(items) => items
            .iter()
            .map(to_edit_value)
            .collect::<toml_edit::Array>()
            .into(),
        toml::Value::Table(t) => {
            let mut inline = toml_edit::InlineTable::new();
            for (k, v) in t {
                inline.insert(k, to_edit_value(v));
            }
            inline.into()
        }
    }
}

/// Where koan keeps its configuration, library database and cache.
///
/// `~/.config/koan/` (on iOS, `Library/Application Support/koan`) unless
/// pointed elsewhere. `KOAN_CONFIG_DIR` is the
/// user-facing way to do that — one machine, more than one library — and
/// `set_config_dir` is the in-process one, which is what tests need: without
/// it they read whatever configuration belongs to whoever ran them, right down
/// to that person's server and their password.
pub fn config_dir() -> PathBuf {
    if let Some(dir) = CONFIG_DIR.read().clone() {
        return dir;
    }
    if let Some(dir) = std::env::var_os("KOAN_CONFIG_DIR") {
        return PathBuf::from(dir);
    }
    platform_config_dir()
}

#[cfg(not(any(target_os = "ios", target_os = "tvos")))]
fn platform_config_dir() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".config")
        .join("koan")
}

/// An iOS app may write inside its container's `Documents`, `Library` and
/// `tmp`, and nowhere else: `~/.config` is refused on a device, though the
/// simulator allows it. Application Support is where an app's own state goes.
#[cfg(target_os = "ios")]
fn platform_config_dir() -> PathBuf {
    ios_library().join("Application Support").join("koan")
}

/// A tvOS app has no persistent storage of its own: `Library/Caches` is the
/// one directory it can write, and the system empties it when space runs short.
/// The files live there, beside the download cache rather than inside it, which
/// is trimmed; `kept` holds a copy that survives a purge.
#[cfg(target_os = "tvos")]
fn platform_config_dir() -> PathBuf {
    let dir = ios_library().join("Caches").join("koan-config");
    kept::restore(&dir);
    dir
}

/// The configuration files of a tvOS app, mirrored into its preferences.
///
/// tvOS keeps an app's preferences (up to 500 KB) when it purges
/// `Library/Caches`, and a purge would otherwise sign the television out and
/// forget its settings. Every write to the platform directory is copied here,
/// and the first look at the directory puts back a file that has gone. The
/// Keychain would also survive, and is deliberately not used for credentials.
/// The library database is not kept: the next sync rebuilds it.
#[cfg(target_os = "tvos")]
mod kept {
    use std::fs;
    use std::os::unix::fs::PermissionsExt;
    use std::path::Path;
    use std::sync::Once;

    use core_foundation::base::TCFType;
    use core_foundation::data::{CFData, CFDataRef};
    use core_foundation::string::CFString;
    use core_foundation_sys::base::{CFGetTypeID, CFRelease};
    use core_foundation_sys::data::CFDataGetTypeID;
    use core_foundation_sys::preferences::{
        CFPreferencesAppSynchronize, CFPreferencesCopyAppValue, CFPreferencesSetAppValue,
        kCFPreferencesCurrentApplication,
    };

    const FILES: [&str; 2] = ["config.toml", "config.local.toml"];

    /// Once per process: a file that is missing comes back from the
    /// preferences, and one that is present is copied into them, which is how
    /// a configuration written before this existed starts being kept.
    pub(super) fn restore(dir: &Path) {
        static ONCE: Once = Once::new();
        ONCE.call_once(|| {
            for name in FILES {
                let path = dir.join(name);
                match fs::read(&path) {
                    Ok(contents) => save(name, &contents),
                    Err(_) => {
                        let Some(contents) = load(name) else { continue };
                        let _ = fs::create_dir_all(dir);
                        if fs::write(&path, &contents).is_ok() {
                            let _ = fs::set_permissions(&path, fs::Permissions::from_mode(0o600));
                            log::info!("config: restored {name} after the system cleared it");
                        }
                    }
                }
            }
        });
    }

    /// Mirror a write, if it went to the platform directory rather than one a
    /// test or `KOAN_CONFIG_DIR` chose.
    pub(super) fn written(path: &Path, contents: &[u8]) {
        let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
            return;
        };
        if FILES.contains(&name) && path.parent() == Some(super::platform_config_dir().as_path()) {
            save(name, contents);
        }
    }

    fn save(name: &str, contents: &[u8]) {
        let key = CFString::new(name);
        let data = CFData::from_buffer(contents);
        // SAFETY: both are live CF objects for the length of the call, and the
        // preferences retain what they keep.
        unsafe {
            CFPreferencesSetAppValue(
                key.as_concrete_TypeRef(),
                data.as_CFTypeRef(),
                kCFPreferencesCurrentApplication,
            );
            CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
        }
    }

    fn load(name: &str) -> Option<Vec<u8>> {
        let key = CFString::new(name);
        // SAFETY: a Copy function returns an owned reference or null. Anything
        // but data is released here; data is owned by the wrapper.
        unsafe {
            let value = CFPreferencesCopyAppValue(
                key.as_concrete_TypeRef(),
                kCFPreferencesCurrentApplication,
            );
            if value.is_null() {
                return None;
            }
            if CFGetTypeID(value) != CFDataGetTypeID() {
                CFRelease(value);
                return None;
            }
            Some(
                CFData::wrap_under_create_rule(value as CFDataRef)
                    .bytes()
                    .to_vec(),
            )
        }
    }
}

#[cfg(any(target_os = "ios", target_os = "tvos"))]
fn ios_library() -> PathBuf {
    dirs::home_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("Library")
}

/// Where downloads are kept when the config names nowhere.
///
/// Beside the config everywhere but iOS. There it is `Library/Caches`, which
/// is not backed up to iCloud — a cache of lossless files would otherwise count
/// against someone's iCloud storage — and which iOS may clear when the device
/// is short of space, which is what a cache is for.
fn default_cache_dir() -> PathBuf {
    #[cfg(any(target_os = "ios", target_os = "tvos"))]
    if CONFIG_DIR.read().is_none() && std::env::var_os("KOAN_CONFIG_DIR").is_none() {
        return ios_library().join("Caches").join("koan");
    }
    config_dir().join("cache")
}

/// Point koan's configuration at `dir` for the life of the process.
///
/// Takes precedence over `KOAN_CONFIG_DIR`, and drops the cached config, which
/// was keyed on the mtimes of files in a directory that is no longer the one
/// being read. Set it before anything spawns: background threads resolve the
/// directory when they run, not when they are created.
pub fn set_config_dir(dir: impl Into<PathBuf>) {
    *CONFIG_DIR.write() = Some(dir.into());
    Config::invalidate_cache();
}

/// Point configuration at a directory belonging to this process alone.
///
/// Tests call this before anything reads configuration. Without it they read
/// whatever belongs to whoever ran them — that person's library folders, their
/// remote server, and their password — so the same test does
/// different things on different machines, and passes on CI only because it
/// finds nothing there at all.
///
/// Process-wide rather than per-test on purpose: the threads koan spawns
/// resolve the directory when they run, which is often after the test that
/// started them has finished.
///
/// It waits for a test that holds `SWITCHING` (koan-core's `PERSIST_LOCK`)
/// to finish before switching, so that test never has the directory moved
/// out from under it mid-way.
pub fn isolate_config_for_tests() {
    let _one = SWITCHING.lock().unwrap_or_else(|e| e.into_inner());
    let dir = std::env::temp_dir().join(format!("koan-test-config-{}", std::process::id()));
    let _ = fs::create_dir_all(&dir);
    set_config_dir(dir);
}

/// Held by a test that points config at a directory of its own for its
/// whole run, and by `isolate_config_for_tests` while it switches, so no
/// test's directory changes under it.
#[doc(hidden)]
pub static SWITCHING: std::sync::Mutex<()> = std::sync::Mutex::new(());

static CONFIG_DIR: LazyLock<parking_lot::RwLock<Option<PathBuf>>> =
    LazyLock::new(|| parking_lot::RwLock::new(None));

/// Path to the base config TOML file (committable to dotfiles).
pub fn config_file_path() -> PathBuf {
    config_dir().join("config.toml")
}

/// Path to the local override config (gitignored, machine-specific).
pub fn config_local_file_path() -> PathBuf {
    config_dir().join("config.local.toml")
}

/// Path to the database file.
pub fn db_path() -> PathBuf {
    config_dir().join("koan.db")
}

/// Above this, `koan.log` is moved aside to `koan.log.1`, replacing the one
/// before, so the log never holds more than twice it.
const LOG_LIMIT: u64 = 16 * 1024 * 1024;
/// How many lines go by between looks at the file's size: the app stays open
/// for days, so checking only when the file is opened is not enough.
const LOG_CHECK_EVERY: u32 = 4096;

/// `koan.log`, kept to a size. Every logger writes through one of these.
#[derive(Default)]
pub struct LogFile {
    file: Option<fs::File>,
    lines: u32,
}

impl LogFile {
    pub fn write(&mut self, line: std::fmt::Arguments) {
        use std::io::Write as _;
        if self.file.is_none() {
            self.file = open_log();
        }
        let Some(file) = self.file.as_mut() else {
            return;
        };
        let _ = writeln!(file, "{line}");
        self.lines = self.lines.wrapping_add(1);
        if self.lines.is_multiple_of(LOG_CHECK_EVERY)
            && file.metadata().is_ok_and(|m| m.len() > LOG_LIMIT)
        {
            self.file = open_log();
        }
    }

    pub fn flush(&mut self) {
        if let Some(file) = self.file.as_mut() {
            let _ = std::io::Write::flush(file);
        }
    }
}

/// Open `koan.log` for appending, creating the configuration directory first
/// and moving an oversized log aside.
///
/// A logger starts before anything else has had reason to create the
/// directory, so on a first launch it would otherwise find nowhere to write.
fn open_log() -> Option<fs::File> {
    let dir = config_dir();
    fs::create_dir_all(&dir).ok()?;
    let path = dir.join("koan.log");
    if fs::metadata(&path).is_ok_and(|m| m.len() > LOG_LIMIT) {
        let _ = fs::rename(&path, dir.join("koan.log.1"));
    }
    fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)
        .ok()
}

/// Refuse to start when credentials are sitting in version control, which is a
/// security incident rather than a warning anyone would act on.
///
/// Runs once per process. It reads both config files and, when a password is
/// present, forks `git ls-files` — and `load()` is reached from UI paths that
/// run per frame.
fn check_secrets_in_git() {
    static ONCE: Once = Once::new();
    ONCE.call_once(scan_for_tracked_secrets);
}

fn scan_for_tracked_secrets() {
    let sensitive_fields = ["password", "api_key", "refresh_token"];

    for (label, path) in [
        ("config.toml", config_file_path()),
        ("config.local.toml", config_local_file_path()),
    ] {
        let Ok(contents) = std::fs::read_to_string(&path) else {
            continue;
        };

        // Check if this file contains any sensitive fields with non-empty values.
        let has_secrets = sensitive_fields.iter().any(|field| {
            contents.lines().any(|line| {
                let line = line.trim();
                if let Some(rest) = line.strip_prefix(field) {
                    let rest = rest.trim_start();
                    if let Some(value) = rest.strip_prefix('=') {
                        let value = value.trim().trim_matches('"').trim_matches('\'');
                        return !value.is_empty();
                    }
                }
                false
            })
        });

        if !has_secrets {
            continue;
        }

        // Check if this file is tracked by git.
        if is_tracked_by_git(&path) {
            eprintln!();
            eprintln!("╔══════════════════════════════════════════════════════════════╗");
            eprintln!("║  SECURITY: {label} contains credentials and is tracked by git!  ║");
            eprintln!("╠══════════════════════════════════════════════════════════════╣");
            eprintln!("║                                                              ║");
            eprintln!("║  File: {:<52} ║", path.display());
            eprintln!("║                                                              ║");
            eprintln!("║  Your password is in version control. You should:            ║");
            eprintln!("║  1. Remove the file from git: git rm --cached <file>         ║");
            eprintln!("║  2. Add it to .gitignore                                     ║");
            eprintln!("║  3. Rotate your credentials immediately                      ║");
            eprintln!("║  4. Move secrets to config.local.toml (gitignored)           ║");
            eprintln!("║     `koan remote login` writes there for you                 ║");
            eprintln!("║                                                              ║");
            eprintln!("╚══════════════════════════════════════════════════════════════╝");
            eprintln!();
            panic!("Refusing to start: credentials tracked by git in {label}. See above.");
        }
    }
}

/// Check if a file is tracked by git (staged or committed, not just in a repo).
fn is_tracked_by_git(path: &Path) -> bool {
    let Some(parent) = path.parent() else {
        return false;
    };
    // `git ls-files --error-unmatch <file>` exits 0 if tracked, 1 if not.
    std::process::Command::new("git")
        .args(["ls-files", "--error-unmatch"])
        .arg(path)
        .current_dir(parent)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok_and(|s| s.success())
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use std::fs;

    fn tmp_dir() -> PathBuf {
        let dir = std::env::temp_dir().join(format!("koan-test-{}", std::process::id()));
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn test_defaults() {
        let cfg = Config::default();
        assert_eq!(cfg.playback.replaygain, ReplayGainMode::Off);
        assert!(!cfg.remote.enabled);
    }

    #[test]
    fn test_roundtrip_toml() {
        let cfg = Config::default();
        let serialized = toml::to_string_pretty(&cfg).unwrap();
        let deserialized: Config = toml::from_str(&serialized).unwrap();
        assert_eq!(deserialized.playback.replaygain, cfg.playback.replaygain);
        assert_eq!(
            deserialized.remote.download_workers,
            cfg.remote.download_workers
        );
    }

    #[test]
    fn test_load_from_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(
            &path,
            r#"
[library]
folders = ["/tmp/music"]

[playback]
replaygain = "track"
"#,
        )
        .unwrap();

        let cfg = Config::load_from(&path).unwrap();
        assert_eq!(cfg.library.folders, vec![PathBuf::from("/tmp/music")]);
        assert_eq!(cfg.playback.replaygain, ReplayGainMode::Track);
        assert!(!cfg.remote.enabled);
    }

    #[test]
    fn test_partial_toml_uses_defaults() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("partial.toml");
        fs::write(&path, "[playback]\ntarget_fps = 30\n").unwrap();

        let cfg = Config::load_from(&path).unwrap();
        assert_eq!(cfg.playback.target_fps, 30);
        assert_eq!(cfg.playback.replaygain, ReplayGainMode::Off);
    }

    #[test]
    fn test_figment_layered_loading() {
        let dir = tempfile::tempdir().unwrap();
        let base_path = dir.path().join("config.toml");
        let local_path = dir.path().join("config.local.toml");

        fs::write(
            &base_path,
            r#"
[remote]
url = "https://base.example.com"
"#,
        )
        .unwrap();
        fs::write(
            &local_path,
            r#"
[remote]
enabled = true
url = "https://local.example.com"
username = "admin"
password = "secret"
"#,
        )
        .unwrap();

        // Build a figment with explicit paths (can't use load() since it reads from ~/.config).
        let cfg: Config = Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(&base_path))
            .merge(Toml::file(&local_path))
            .extract()
            .unwrap();

        assert!(cfg.remote.enabled);
        assert_eq!(cfg.remote.url, "https://local.example.com");
        assert_eq!(cfg.remote.username, "admin");
        assert_eq!(cfg.remote.password, "secret");
    }

    #[test]
    fn test_figment_missing_keys_preserved() {
        let dir = tempfile::tempdir().unwrap();
        let base_path = dir.path().join("config.toml");
        let local_path = dir.path().join("config.local.toml");

        fs::write(
            &base_path,
            r#"
[remote]
url = "https://keep.me"
username = "keepuser"
"#,
        )
        .unwrap();
        fs::write(
            &local_path,
            r#"
[remote]
password = "secret"
"#,
        )
        .unwrap();

        let cfg: Config = Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(&base_path))
            .merge(Toml::file(&local_path))
            .extract()
            .unwrap();

        assert_eq!(cfg.remote.url, "https://keep.me");
        assert_eq!(cfg.remote.username, "keepuser");
        assert_eq!(cfg.remote.password, "secret");
    }

    #[test]
    fn test_env_var_override() {
        let dir = tempfile::tempdir().unwrap();
        let base_path = dir.path().join("config.toml");

        fs::write(
            &base_path,
            r#"
[remote]
url = "https://file.example.com"
"#,
        )
        .unwrap();

        // SAFETY: test is single-threaded and vars are cleaned up immediately after.
        unsafe {
            std::env::set_var("KOAN_REMOTE__URL", "https://env.example.com");
            std::env::set_var("KOAN_REMOTE__PASSWORD", "env-secret");
            std::env::set_var("KOAN_GRAPHQL__PORT", "9999");
        }

        let cfg: Config = Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(&base_path))
            .merge(Env::prefixed("KOAN_").split("__"))
            .extract()
            .unwrap();

        assert_eq!(cfg.remote.url, "https://env.example.com");
        assert_eq!(cfg.remote.password, "env-secret");
        assert_eq!(cfg.graphql.port, 9999);

        // Clean up env vars.
        unsafe {
            std::env::remove_var("KOAN_REMOTE__URL");
            std::env::remove_var("KOAN_REMOTE__PASSWORD");
            std::env::remove_var("KOAN_GRAPHQL__PORT");
        }
    }

    #[test]
    fn test_cache_dir_default() {
        let cfg = Config::default();
        assert!(cfg.cache_dir().ends_with("cache"));
    }

    #[test]
    fn test_cache_dir_explicit() {
        let mut cfg = Config::default();
        cfg.remote.cache_dir = Some(PathBuf::from("/custom/cache"));
        assert_eq!(cfg.cache_dir(), PathBuf::from("/custom/cache"));
    }

    #[test]
    fn test_organize_config_defaults() {
        let cfg = Config::default();
        assert!(cfg.organize.default.is_none());
        assert!(cfg.organize.patterns.is_empty());
    }

    #[test]
    fn test_organize_config_from_toml() {
        let dir = tmp_dir();
        let path = dir.join("organize.toml");
        fs::write(
            &path,
            r#"
[organize]
default = "standard"

[organize.patterns]
standard = "%album artist%/(%date%) %album%/%tracknumber%. %title%"
va-aware = "%album artist%/$if($stricmp(%album artist%,Various Artists),,%album%)"
"#,
        )
        .unwrap();

        let cfg = Config::load_from(&path).unwrap();
        assert_eq!(cfg.organize.default.as_deref(), Some("standard"));
        assert_eq!(cfg.organize.patterns.len(), 2);
        assert!(cfg.organize.patterns.contains_key("standard"));
        assert!(cfg.organize.patterns.contains_key("va-aware"));

        fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn test_figment_organize_patterns_merge() {
        let dir = tempfile::tempdir().unwrap();
        let base_path = dir.path().join("config.toml");
        let local_path = dir.path().join("config.local.toml");

        fs::write(
            &base_path,
            r#"
[organize]
default = "standard"

[organize.patterns]
standard = "base-pattern"
"#,
        )
        .unwrap();
        fs::write(
            &local_path,
            r#"
[organize]
default = "custom"

[organize.patterns]
custom = "local-pattern"
"#,
        )
        .unwrap();

        let cfg: Config = Figment::from(Serialized::defaults(Config::default()))
            .merge(Toml::file(&base_path))
            .merge(Toml::file(&local_path))
            .extract()
            .unwrap();

        // Local default wins.
        assert_eq!(cfg.organize.default.as_deref(), Some("custom"));
        // Both patterns present (figment merges maps).
        assert_eq!(cfg.organize.patterns.len(), 2);
        assert_eq!(cfg.organize.patterns["standard"], "base-pattern");
        assert_eq!(cfg.organize.patterns["custom"], "local-pattern");
    }

    #[test]
    fn test_output_device_config_roundtrip() {
        let mut cfg = Config::default();
        cfg.playback.output_device = Some("My DAC".into());

        let serialized = toml::to_string_pretty(&cfg).unwrap();
        let deserialized: Config = toml::from_str(&serialized).unwrap();
        assert_eq!(
            deserialized.playback.output_device.as_deref(),
            Some("My DAC")
        );
    }

    #[test]
    fn test_output_device_config_default_is_none() {
        let cfg = Config::default();
        assert!(cfg.playback.output_device.is_none());

        // Roundtrip: None should not appear in serialized output.
        let serialized = toml::to_string_pretty(&cfg).unwrap();
        assert!(!serialized.contains("output_device"));
        let deserialized: Config = toml::from_str(&serialized).unwrap();
        assert!(deserialized.playback.output_device.is_none());
    }

    #[test]
    fn test_output_device_config_from_toml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(
            &path,
            r#"
[playback]
output_device = "External Speakers"
"#,
        )
        .unwrap();

        let cfg = Config::load_from(&path).unwrap();
        assert_eq!(
            cfg.playback.output_device.as_deref(),
            Some("External Speakers")
        );
    }

    #[test]
    fn test_graphql_bind_defaults_to_localhost() {
        let cfg = GraphqlConfig::default();
        assert_eq!(
            cfg.bind,
            std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
        );
    }

    #[test]
    fn test_graphql_bind_from_toml() {
        let toml_str = r#"
[graphql]
bind = "0.0.0.0"
port = 5000
"#;
        let cfg: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(
            cfg.graphql.bind,
            std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)
        );
        assert_eq!(cfg.graphql.port, 5000);
    }

    #[test]
    fn test_graphql_bind_omitted_defaults_to_localhost() {
        let toml_str = r#"
[graphql]
port = 4000
"#;
        let cfg: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(
            cfg.graphql.bind,
            std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST)
        );
    }

    #[test]
    fn test_organize_config_roundtrip() {
        let mut cfg = Config::default();
        cfg.organize.default = Some("standard".into());
        cfg.organize
            .patterns
            .insert("standard".into(), "%artist%/%title%".into());

        let serialized = toml::to_string_pretty(&cfg).unwrap();
        let deserialized: Config = toml::from_str(&serialized).unwrap();
        assert_eq!(deserialized.organize.default.as_deref(), Some("standard"));
        assert_eq!(
            deserialized.organize.patterns["standard"],
            "%artist%/%title%"
        );
    }

    #[test]
    fn test_parse_size_bytes() {
        assert_eq!(parse_size_bytes("50GB"), Some(50 * 1024 * 1024 * 1024));
        assert_eq!(parse_size_bytes("500MB"), Some(500 * 1024 * 1024));
        assert_eq!(parse_size_bytes("1TB"), Some(1024 * 1024 * 1024 * 1024));
        assert_eq!(parse_size_bytes("100KB"), Some(100 * 1024));
        assert_eq!(parse_size_bytes("1024B"), Some(1024));
        assert_eq!(parse_size_bytes("1024"), Some(1024));

        // Case insensitive.
        assert_eq!(parse_size_bytes("50gb"), Some(50 * 1024 * 1024 * 1024));
        assert_eq!(parse_size_bytes("50Gb"), Some(50 * 1024 * 1024 * 1024));

        // Short suffixes.
        assert_eq!(parse_size_bytes("50G"), Some(50 * 1024 * 1024 * 1024));
        assert_eq!(parse_size_bytes("500M"), Some(500 * 1024 * 1024));

        // Spaces.
        assert_eq!(parse_size_bytes("50 GB"), Some(50 * 1024 * 1024 * 1024));
        assert_eq!(parse_size_bytes(" 50GB "), Some(50 * 1024 * 1024 * 1024));

        // Decimal.
        assert_eq!(
            parse_size_bytes("1.5GB"),
            Some((1.5 * 1024.0 * 1024.0 * 1024.0) as u64)
        );

        // Invalid.
        assert_eq!(parse_size_bytes(""), None);
        assert_eq!(parse_size_bytes("abc"), None);
        assert_eq!(parse_size_bytes("50XB"), None);
    }

    #[test]
    fn test_cache_limit_config_from_toml() {
        let toml_str = r#"
[remote]
cache_limit = "50GB"
"#;
        let cfg: Config = toml::from_str(toml_str).unwrap();
        assert_eq!(cfg.remote.cache_limit.as_deref(), Some("50GB"));
        assert_eq!(cfg.cache_limit_bytes(), Some(50 * 1024 * 1024 * 1024));
    }

    #[test]
    fn test_cache_limit_none_by_default() {
        let cfg = Config::default();
        assert!(cfg.remote.cache_limit.is_none());
        assert!(cfg.cache_limit_bytes().is_none());
    }

    #[test]
    fn test_cache_limit_not_serialized_when_none() {
        let cfg = Config::default();
        let serialized = toml::to_string_pretty(&cfg).unwrap();
        assert!(!serialized.contains("cache_limit"));
    }

    #[test]
    fn player_uses_config_on_init() {
        // Verify that Config::load_from correctly picks up playback settings
        // that Player::new() would consume. This tests the contract between
        // config and player initialization without requiring audio hardware.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        fs::write(
            &path,
            r#"
[playback]
replaygain = "track"
output_device = "My Fancy DAC"
pre_amp_db = -3.5
target_fps = 30
art_size = 32

[visualizer]
enabled = false
mode = "oscilloscope"
fps = 30
"#,
        )
        .unwrap();

        let cfg = Config::load_from(&path).unwrap();

        // These are the fields Player::new() reads from config.
        assert_eq!(
            cfg.playback.replaygain,
            ReplayGainMode::Track,
            "replaygain should be 'track'"
        );
        assert_eq!(
            cfg.playback.output_device.as_deref(),
            Some("My Fancy DAC"),
            "output_device should match config"
        );
        assert!(
            (cfg.playback.pre_amp_db - (-3.5)).abs() < f64::EPSILON,
            "pre_amp_db should be -3.5"
        );
        assert_eq!(cfg.playback.target_fps, 30, "target_fps should be 30");
        assert_eq!(cfg.playback.art_size, 32, "art_size should be 32");

        // Visualizer config is also consumed at player init.
        assert!(!cfg.visualizer.enabled, "visualizer should be disabled");
        assert_eq!(cfg.visualizer.mode, "oscilloscope");
        assert_eq!(cfg.visualizer.fps, 30);
    }

    #[test]
    fn a_missing_config_file_stamps_as_absent() {
        let dir = tempfile::tempdir().unwrap();
        let base = dir.path().join("config.toml");
        let local = dir.path().join("config.local.toml");

        assert_eq!(stamp_of(&base, &local), (None, None));

        fs::write(&base, "[remote]\nurl = \"https://example.com\"\n").unwrap();
        let (base_stamp, local_stamp) = stamp_of(&base, &local);
        assert!(base_stamp.is_some(), "creating the file must be a change");
        assert!(local_stamp.is_none());
    }

    #[test]
    fn editing_a_config_file_changes_its_stamp() {
        let dir = tempfile::tempdir().unwrap();
        let base = dir.path().join("config.toml");
        let local = dir.path().join("config.local.toml");
        fs::write(&base, "[playback]\ntarget_fps = 60\n").unwrap();

        let before = stamp_of(&base, &local);
        // Coarse-grained filesystems would otherwise stamp both writes alike.
        std::thread::sleep(std::time::Duration::from_millis(20));
        fs::write(&base, "[playback]\ntarget_fps = 30\n").unwrap();

        assert_ne!(
            before,
            stamp_of(&base, &local),
            "a config edited by hand has to be picked up"
        );
    }

    #[test]
    fn invalidating_forces_a_reload() {
        let first = Config::cached();
        Config::invalidate_cache();
        assert!(
            !Arc::ptr_eq(&first, &Config::cached()),
            "koan's own writes invalidate explicitly; the next read must re-parse"
        );
    }

    // ---- persist: which file a setting lands in -------------------------

    /// `persist` reads and writes process-global paths, so these run one at a
    /// time rather than racing each other through `set_config_dir`.
    pub(crate) static PERSIST_LOCK: &std::sync::Mutex<()> = &super::SWITCHING;

    /// Point config at a fresh directory and hand back (base, local) paths.
    fn persist_sandbox(name: &str) -> (PathBuf, PathBuf) {
        let dir =
            std::env::temp_dir().join(format!("koan-persist-{}-{}", name, std::process::id()));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        set_config_dir(&dir);
        (config_file_path(), config_local_file_path())
    }

    /// A write lands where a symlink points, even one pointing at nothing
    /// yet; the secret file is 0600, the other keeps its mode; nothing is left
    /// beside them.
    #[cfg(unix)]
    #[test]
    fn a_write_keeps_links_modes_and_no_leftovers() {
        use std::os::unix::fs::PermissionsExt;
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().unwrap();
        let dotfiles = tempfile::tempdir().unwrap();
        std::os::unix::fs::symlink(
            dotfiles.path().join("config.toml"),
            dir.path().join("config.toml"),
        )
        .unwrap();
        set_config_dir(dir.path());

        Config::persist(|c| c.graphql.port = 4100).unwrap();
        assert!(
            fs::symlink_metadata(config_file_path())
                .unwrap()
                .is_symlink()
        );
        assert!(
            fs::read_to_string(dotfiles.path().join("config.toml"))
                .unwrap()
                .contains("4100")
        );

        fs::set_permissions(
            dotfiles.path().join("config.toml"),
            fs::Permissions::from_mode(0o640),
        )
        .unwrap();
        Config::persist(|c| {
            c.graphql.port = 4101;
            c.remote.password = "hunter2".into();
        })
        .unwrap();
        let mode = |p: PathBuf| fs::metadata(p).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode(dotfiles.path().join("config.toml")), 0o640);
        assert_eq!(mode(config_local_file_path()), 0o600);
        assert!(
            fs::symlink_metadata(config_file_path())
                .unwrap()
                .is_symlink()
        );

        let names = |d: &Path| -> Vec<String> {
            fs::read_dir(d)
                .unwrap()
                .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
                .collect()
        };
        assert!(!names(dir.path()).iter().any(|n| n.ends_with(".tmp")));
        assert_eq!(names(dotfiles.path()), vec!["config.toml".to_string()]);
    }

    /// A read racing directory switches never caches one directory's config
    /// under the other's, even when the directory switches away and back
    /// while the read is loading.
    #[test]
    fn a_read_during_switches_is_never_kept_for_the_wrong_directory() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dirs: Vec<tempfile::TempDir> = (0..2).map(|_| tempfile::tempdir().unwrap()).collect();
        for (dir, port) in dirs.iter().zip([4001, 4002]) {
            fs::write(
                dir.path().join("config.toml"),
                format!("[graphql]\nport = {port}\n"),
            )
            .unwrap();
        }
        let done = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let reader = std::thread::spawn({
            let done = done.clone();
            move || {
                while !done.load(std::sync::atomic::Ordering::Relaxed) {
                    Config::cached();
                }
            }
        });
        for i in 0..2000 {
            let (dir, port) = if i % 2 == 0 {
                (&dirs[0], 4001)
            } else {
                (&dirs[1], 4002)
            };
            set_config_dir(dir.path());
            for _ in 0..3 {
                assert_eq!(Config::cached().graphql.port, port, "switch {i}");
            }
        }
        done.store(true, std::sync::atomic::Ordering::Relaxed);
        reader.join().unwrap();
    }

    #[test]
    fn persist_keeps_comments_and_untouched_keys() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, _local) = persist_sandbox("comments");
        fs::write(
            &base,
            "# koan — shareable defaults\n\n[visualizer]\n# fps = 60\npalette = \"fire\"\n",
        )
        .unwrap();

        Config::persist(|cfg| cfg.visualizer.palette = "neon".into()).unwrap();

        let written = fs::read_to_string(&base).unwrap();
        assert!(
            written.contains("# koan — shareable defaults"),
            "the header comment must survive a write: {written}"
        );
        assert!(
            written.contains("# fps = 60"),
            "commented-out defaults are the template's whole point: {written}"
        );
        assert!(written.contains("palette = \"neon\""));
        assert!(
            !written.contains("[graphql]"),
            "an untouched section must not be invented: {written}"
        );
    }

    #[test]
    fn persist_routes_machine_settings_to_the_local_file() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, local) = persist_sandbox("routing");

        Config::persist(|cfg| {
            cfg.playback.replaygain = ReplayGainMode::Album;
            cfg.playback.output_device = Some("My DAC".into());
            cfg.playback.art_size = 40;
            cfg.visualizer.mode = "starfield".into();
        })
        .unwrap();

        let shared = fs::read_to_string(&base).unwrap();
        let machine = fs::read_to_string(&local).unwrap();

        assert!(shared.contains("replaygain = \"album\""), "{shared}");
        for machine_only in ["output_device", "art_size", "starfield"] {
            assert!(
                !shared.contains(machine_only),
                "{machine_only} is this machine's, not the dotfiles repo's: {shared}"
            );
            assert!(machine.contains(machine_only), "{machine}");
        }
    }

    #[test]
    fn persist_writes_dsp_profiles_to_the_local_file_as_sections() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, local) = persist_sandbox("dsp");

        let profile = DspProfile {
            name: "HD 600".into(),
            devices: vec!["Topping E30".into()],
            preamp_db: None,
            filters: vec![
                DspFilter::Band(EqFilter {
                    kind: EqFilterKind::Peaking,
                    freq: 20.0,
                    gain_db: -1.3,
                    q: 2.0,
                    channels: vec![],
                }),
                DspFilter::Band(EqFilter {
                    kind: EqFilterKind::HighShelfFirstOrder,
                    freq: 10000.0,
                    gain_db: 2.5,
                    q: 0.7,
                    channels: vec![],
                }),
                DspFilter::Delay(Delay {
                    ms: 1.5,
                    channels: vec![1],
                    ..Default::default()
                }),
                DspFilter::Mix(Mix {
                    outputs: vec![vec![(0, 0.5), (1, 0.5)], vec![]],
                }),
                DspFilter::Graphic(GraphicEq {
                    points: vec![(20.0, -1.0), (1000.0, 0.0)],
                    channels: vec![],
                }),
            ],
            impulses: vec![],
            source: vec![],
            target: None,
            layers: vec![],
            group: false,
            preset: false,
            scope: None,
            uid: None,
            origin: None,
            role: None,
            measurement: None,
            fitted: None,
            tuned_for: None,
            original: None,
        };
        Config::persist(|cfg| cfg.dsp.profiles.push(profile.clone())).unwrap();

        assert!(!base.exists() || !fs::read_to_string(&base).unwrap().contains("dsp"));
        let machine = fs::read_to_string(&local).unwrap();
        assert!(machine.contains("[[dsp.profiles]]"), "{machine}");
        assert!(
            machine.contains("\n    { type = \"peaking\""),
            "one band per line: {machine}"
        );
        assert_eq!(Config::from_files().unwrap().dsp.profiles, vec![profile]);
        assert_eq!(
            Config::from_files()
                .unwrap()
                .dsp
                .profile_for("Topping E30")
                .map(|p| p.name.as_str()),
            Some("HD 600")
        );
    }

    /// Every bound holds, whatever a profile says: values clamped, ones
    /// that are not numbers dropped, counts cut off, each change noted.
    #[test]
    fn a_profile_is_played_within_bounds() {
        let band = |freq: f64, gain_db: f64, q: f64| {
            DspFilter::Band(EqFilter {
                kind: EqFilterKind::Peaking,
                freq,
                gain_db,
                q,
                channels: vec![],
            })
        };
        let mut p = DspProfile {
            name: "Loud\u{7}".into(),
            preamp_db: Some(60.0),
            filters: vec![
                DspFilter::Delay(Delay {
                    ms: 1e12,
                    ..Default::default()
                }),
                band(0.5, 200.0, 0.0),
                band(f64::NAN, 0.0, 1.0),
                DspFilter::Band(EqFilter {
                    kind: EqFilterKind::Peaking,
                    freq: 1000.0,
                    gain_db: 0.0,
                    q: 1.0,
                    channels: vec![99],
                }),
                DspFilter::Mix(Mix {
                    outputs: vec![vec![(0, 1e9), (1, f64::INFINITY)]],
                }),
                DspFilter::Graphic(GraphicEq {
                    points: (0..3000).map(|i| (i as f64, 1.0)).collect(),
                    channels: vec![],
                }),
            ],
            ..Default::default()
        };
        p.filters.extend((0..300).map(|_| band(1000.0, 1.0, 1.0)));
        let notes = p.sanitize();
        let has = |n: &str| notes.iter().any(|x| x == n);
        assert!(has("delay clamped to 2000 ms"), "{notes:?}");
        assert!(has("preamp clamped to 30 dB"), "{notes:?}");
        assert!(has("a band's gain clamped to 30 dB"), "{notes:?}");
        assert!(has("a band's frequency clamped to 1 Hz"), "{notes:?}");
        assert!(has("a filter that could not play dropped"), "{notes:?}");
        assert!(has("bands past 64 a channel dropped"), "{notes:?}");
        assert!(has("graphic EQ points past 2048 dropped"), "{notes:?}");
        assert_eq!(p.name, "Loud");
        assert_eq!(p.preamp_db, Some(30.0));
        assert_eq!(
            p.filters[0],
            DspFilter::Delay(Delay {
                ms: 2000.0,
                ..Default::default()
            })
        );
        assert_eq!(p.filters[1], band(1.0, 30.0, 0.01));
        let DspFilter::Mix(m) = &p.filters[2] else {
            panic!("{:?}", p.filters[2])
        };
        assert_eq!(m.outputs, vec![vec![(0, dsp_bounds::MIX_GAIN)]]);
        let bands = p
            .filters
            .iter()
            .filter(|f| matches!(f, DspFilter::Band(_)))
            .count();
        assert_eq!(bands, dsp_bounds::BANDS_PER_CHANNEL);
        assert!(p.clone().sanitize().is_empty(), "within bounds now");
    }

    #[test]
    fn persist_never_writes_the_default_library_folder_into_the_shared_file() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, _local) = persist_sandbox("folders");

        // Toggling the visualiser used to serialise the whole struct, baking
        // the *default* music directory into the file people commit.
        Config::persist(|cfg| cfg.visualizer.enabled = false).unwrap();

        let shared = fs::read_to_string(&base).unwrap_or_default();
        assert!(
            !shared.contains("folders"),
            "a visualiser toggle must not invent library folders: {shared}"
        );
    }

    #[test]
    fn persist_drains_machine_settings_an_older_koan_left_in_the_shared_file() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, local) = persist_sandbox("drain");
        fs::write(&base, "[playback]\nart_size = 24\ntarget_fps = 60\n").unwrap();

        Config::persist(|cfg| cfg.playback.art_size = 48).unwrap();

        let shared = fs::read_to_string(&base).unwrap();
        assert!(
            !shared.contains("art_size"),
            "the stale shared copy has to go, or dotfiles keep carrying it: {shared}"
        );
        assert!(shared.contains("target_fps"), "{shared}");
        assert!(
            fs::read_to_string(&local)
                .unwrap()
                .contains("art_size = 48")
        );
    }

    #[test]
    fn persist_clears_the_local_copy_so_a_shared_write_takes_effect() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (_base, local) = persist_sandbox("shadow");
        fs::write(&local, "[playback]\ntarget_fps = 30\n").unwrap();

        Config::persist(|cfg| cfg.playback.target_fps = 120).unwrap();

        assert_eq!(
            Config::from_files().unwrap().playback.target_fps,
            120,
            "local wins the merge, so a shared write over a local copy would \
             otherwise be silently ignored: {}",
            fs::read_to_string(&local).unwrap()
        );
    }

    #[test]
    fn persist_writes_nothing_when_the_mutation_changes_nothing() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, _local) = persist_sandbox("noop");
        fs::write(&base, "# untouched\n[playback]\ntarget_fps = 60\n").unwrap();

        Config::persist(|cfg| cfg.playback.target_fps = 60).unwrap();

        assert_eq!(
            fs::read_to_string(&base).unwrap(),
            "# untouched\n[playback]\ntarget_fps = 60\n"
        );
    }

    #[test]
    fn persist_keeps_passwords_out_of_the_shared_file() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, local) = persist_sandbox("secrets");

        Config::persist(|cfg| {
            cfg.remote.password = "hunter2".into();
            cfg.subsonic.password = "s3cret".into();
            cfg.visualizer.palette = "mono".into();
        })
        .unwrap();

        let shared = fs::read_to_string(&base).unwrap();
        assert!(!shared.contains("hunter2"), "{shared}");
        assert!(!shared.contains("s3cret"), "{shared}");
        assert!(shared.contains("mono"));

        let machine = fs::read_to_string(&local).unwrap();
        assert!(machine.contains("hunter2") && machine.contains("s3cret"));
    }

    #[test]
    fn persist_removes_a_cleared_password_rather_than_blanking_it() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (_base, local) = persist_sandbox("clear-secret");
        fs::write(
            &local,
            "[remote]\nurl = \"https://a.example\"\npassword = \"old\"\n",
        )
        .unwrap();

        Config::persist(|cfg| cfg.remote.password = String::new()).unwrap();

        let machine = fs::read_to_string(&local).unwrap();
        assert!(
            !machine.contains("password"),
            "an emptied secret should leave no key behind: {machine}"
        );
        assert!(machine.contains("url"), "{machine}");
    }

    #[test]
    fn persist_adds_one_organize_pattern_without_disturbing_the_others() {
        let _guard = PERSIST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let (base, _local) = persist_sandbox("patterns");
        fs::write(
            &base,
            "[organize.patterns]\nflat = \"%artist% - %title%\"\n",
        )
        .unwrap();

        Config::persist(|cfg| {
            cfg.organize
                .patterns
                .insert("standard".into(), "%album artist%/%album%".into());
        })
        .unwrap();

        let cfg = Config::from_files().unwrap();
        assert_eq!(cfg.organize.patterns["flat"], "%artist% - %title%");
        assert_eq!(cfg.organize.patterns["standard"], "%album artist%/%album%");
    }
}