cantara-songlib 0.2.1

Functionalities to import, manage and export songs in various formats
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
//! LilyPond exporter — generates complete standalone `.ly` files from a Song.
//!
//! Supports two export approaches:
//!
//! 1. **Paper score** (`lilypond_from_song`): Produces a hymn-book–style layout
//!    where all verse lyrics appear as `\addlyrics` under one combined melody
//!    staff. This is the traditional printed music approach.
//!
//! 2. **Sequential** (`lilypond_sequential_from_song`): Produces the song with
//!    every part printed in the exact order as it is sung (e.g. stanza 1 →
//!    refrain → stanza 2 → refrain). Each part gets its own `\score` block.
//!
//! Both approaches use Handlebars templates and produce variable-based LilyPond
//! output with `\relative c'` wrapping, `\paper`, `\layout`, and named
//! variables.
//!
//! Additionally, the module provides functions to:
//! - Generate standalone `.ly` files for individual song parts
//!   (`lilypond_parts_from_song`)
//! - Render LilyPond content to SVG or PDF via the LilyPond binary
//!   (`render_lilypond_to_svg`, `render_lilypond_to_pdf`)
//! - Render all song parts as cropped SVGs (`render_song_parts_to_svg`)
//! - Render the paper score as SVG or PDF (`render_paper_score_to_svg`,
//!   `render_paper_score_to_pdf`)

use std::path::Path;

use handlebars::Handlebars;
use serde::Serialize;

use crate::song::{Song, SongPart, SongPartContent, SongPartContentType, SongPartType};

/// Font configuration for LilyPond export.
#[derive(Clone, PartialEq, Debug)]
#[derive(Default)]
pub enum FontSetting {
    /// Use LilyPond's default font settings.
    #[default]
    Default,
    /// Use a specific font family for the roman (text) font.
    Specific { family: String },
}


/// Configuration for LilyPond export output.
#[derive(Clone, PartialEq, Debug)]
pub struct LilypondSettings {
    /// Paper size for the `\paper` block (default: "a4")
    pub paper_size: String,
    /// Indent setting for `\layout` block (default: "#0")
    pub layout_indent: String,
    /// Font configuration (default: LilyPond defaults)
    pub font: FontSetting,
    /// Optional global staff size override (LilyPond default is 20)
    pub staff_size: Option<f32>,
}

impl Default for LilypondSettings {
    fn default() -> Self {
        LilypondSettings {
            paper_size: "a4".to_string(),
            layout_indent: "#0".to_string(),
            font: FontSetting::Default,
            staff_size: None,
        }
    }
}

/// One `\addlyrics` line of the paper score.
///
/// The content is a sequence of variable references, e.g. `\verseOne \chorus`
/// for the first verse of a song whose refrain has its own melody.
#[derive(Serialize)]
struct LyricLine {
    refs: String,
}

/// Data for a voice definition (e.g. sopranoVoiceStanza, sopranoVoiceRefrain).
#[derive(Serialize)]
struct VoiceDefinition {
    var_name: String,
    content: String,
    /// Whether to include `\global` at the top of this voice definition.
    /// Only the first voice in the combined sequence should include it,
    /// so that key/time signatures are not repeated.
    include_global: bool,
}

/// All data needed to render the paper score LilyPond template.
#[derive(Serialize)]
struct LilypondTemplateData {
    version: String,
    title: String,
    composer: Option<String>,
    paper_size: String,
    layout_indent: String,
    global_content: String,
    has_chords: bool,
    chord_content: String,
    voice_defs: Vec<VoiceDefinition>,
    /// Combined voice variable references for the Staff, e.g. `\sopranoVoiceStanza \sopranoVoiceRefrain`
    combined_voice_refs: String,
    voice_part_name: String,
    /// The part reference including backslash, e.g. `\sopranoVoicePart`
    voice_part_ref: String,
    midi_instrument: String,
    /// Every `\lyricmode` variable of the score: the verses, and — when the
    /// refrain has its own melody — the `chorus` and `chorusSkip` variables.
    lyric_defs: Vec<LyricsDefinition>,
    /// One entry per `\addlyrics` line, in printing order.
    lyric_lines: Vec<LyricLine>,
    staff_size: Option<f32>,
    font_block: Option<String>,
}

/// The Handlebars template for the **paper score** LilyPond file structure.
///
/// Notes on escaping:
/// - LilyPond `\commands` (like `\version`, `\header`) pass through Handlebars
///   unchanged since they are never followed by `{{`.
/// - All variable insertions use triple-braces `{{{...}}}` to avoid HTML-escaping.
/// - Variable references with backslashes (e.g. `\sopranoVoice`) are stored
///   pre-escaped in the data and output via `{{{var_ref}}}`.
const LILYPOND_TEMPLATE: &str = r#"\version "{{{version}}}"

\header {
  title = "{{{title}}}"
{{#if composer}}  composer = "{{{composer}}}"
{{/if}}  tagline = ##f
}
{{#if staff_size}}
#(set-global-staff-size {{{staff_size}}})
{{/if}}
\paper {
  #(set-paper-size "{{{paper_size}}}")
{{#if font_block}}{{{font_block}}}
{{/if}}}

\layout {
  indent = {{{layout_indent}}}
  \context {
    \Voice
    \consists "Melody_engraver"
    \override Stem.neutral-direction = #'()
  }
}

global = {
{{{global_content}}}
}

{{#if has_chords}}
chordNames = \chordmode {
  \global
{{{chord_content}}}
}

{{/if}}
{{#each voice_defs}}
{{{this.var_name}}} = \relative c' {
{{#if this.include_global}}  \global
{{/if}}{{{this.content}}}
}

{{/each}}
{{#each lyric_defs}}
{{{this.var_name}}} = \lyricmode {
{{#if this.stanza}}  \set stanza = "{{{this.stanza}}}"
{{/if}}{{{this.content}}}
}

{{/each}}
{{{voice_part_name}}} = \new Staff \with {
  midiInstrument = "{{{midi_instrument}}}"
} { {{{combined_voice_refs}}} }
{{#each lyric_lines}}
\addlyrics { {{{this.refs}}} }
{{/each}}

{{#if has_chords}}
chordsPart = \new ChordNames \chordNames

{{/if}}
\score {
  <<
{{#if has_chords}}
    \chordsPart
{{/if}}
    {{{voice_part_ref}}}
  >>
  \layout { }
  \midi { }
}
"#;

/// Convert a verse number to an English word for LilyPond variable naming.
fn number_to_word(n: u32) -> String {
    match n {
        1 => "One".to_string(),
        2 => "Two".to_string(),
        3 => "Three".to_string(),
        4 => "Four".to_string(),
        5 => "Five".to_string(),
        6 => "Six".to_string(),
        7 => "Seven".to_string(),
        8 => "Eight".to_string(),
        9 => "Nine".to_string(),
        10 => "Ten".to_string(),
        11 => "Eleven".to_string(),
        12 => "Twelve".to_string(),
        13 => "Thirteen".to_string(),
        14 => "Fourteen".to_string(),
        15 => "Fifteen".to_string(),
        16 => "Sixteen".to_string(),
        17 => "Seventeen".to_string(),
        18 => "Eighteen".to_string(),
        19 => "Nineteen".to_string(),
        20 => "Twenty".to_string(),
        _ => format!("N{}", n),
    }
}

/// Map a voice content type to a LilyPond variable name.
fn voice_type_to_var_name(vt: &SongPartContentType) -> &str {
    match vt {
        SongPartContentType::LeadVoice | SongPartContentType::SupranoVoice => "sopranoVoice",
        SongPartContentType::AltoVoice => "altoVoice",
        SongPartContentType::TenorVoice => "tenorVoice",
        SongPartContentType::BassVoice => "bassVoice",
        _ => "sopranoVoice",
    }
}

/// Convert a human-readable key string (e.g. "f major") to LilyPond format (`\key f \major`).
fn format_lilypond_key(key_str: &str) -> Option<String> {
    let parts: Vec<&str> = key_str.split_whitespace().collect();
    if parts.len() == 2 {
        let note = parts[0].to_lowercase();
        let mode = parts[1].to_lowercase();
        Some(format!("\\key {} \\{}", note, mode))
    } else {
        None
    }
}

/// Indent each line of a multiline string by the given prefix.
fn indent_lines(text: &str, prefix: &str) -> String {
    text.trim()
        .lines()
        .map(|line| format!("{}{}", prefix, line.trim()))
        .collect::<Vec<_>>()
        .join("\n")
}

/// Check if a part has its own voice content directly (not inherited via `is_repetition_of`).
fn find_own_voice(part: &SongPart) -> Option<&SongPartContent> {
    part.contents.iter().find(|c| {
        matches!(
            c.content_type,
            SongPartContentType::LeadVoice
                | SongPartContentType::SupranoVoice
                | SongPartContentType::AltoVoice
                | SongPartContentType::TenorVoice
                | SongPartContentType::BassVoice
        )
    })
}

/// Find the first lyrics content in a part.
fn find_lyrics(part: &SongPart) -> Option<&SongPartContent> {
    part.contents.iter().find(|c| c.content_type.is_lyrics())
}

/// Count how many syllable slots a `\lyricmode` block occupies.
///
/// This is what `\skip 1` has to be repeated for when a verse needs to start
/// after another lyrics block. Syllable separators (`--`), extender lines
/// (`__`) and inline commands together with their arguments take no slot,
/// while `_` (a blank syllable) does.
fn count_lyric_syllables(lyrics: &str) -> usize {
    let mut count = 0usize;
    let mut words = lyrics.split_whitespace().peekable();

    while let Some(word) = words.next() {
        if word.starts_with('\\') {
            // Drop the command, its target and an optional `= value`.
            words.next();
            if words.peek() == Some(&"=") {
                words.next();
                words.next();
            }
            continue;
        }

        if word == "--" || word == "__" {
            continue;
        }

        // A word may carry the separator without spaces ("hei--lig").
        count += word.split("--").filter(|part| !part.is_empty()).count();
    }

    count
}

/// Ensure voice content ends with `\bar "|."` (final bar line).
fn ensure_final_bar(content: &str) -> String {
    let trimmed = content.trim_end();
    if trimmed.ends_with("\\bar \"|.\"") {
        trimmed.to_string()
    } else {
        format!("{} \\bar \"|.\"", trimmed)
    }
}

/// Escape a string for safe inclusion in a LilyPond double-quoted string literal.
/// Currently escapes backslashes (`\` → `\\`) and double quotes (`"` → `\"`).
fn escape_lilypond_string(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Build the LilyPond `#(define fonts ...)` block for a custom font setting.
fn build_font_block(font: &FontSetting) -> Option<String> {
    match font {
        FontSetting::Default => None,
        FontSetting::Specific { family } => {
            let escaped_family = escape_lilypond_string(family);
            Some(format!(
                "  #(define fonts\n    (set-global-fonts\n      #:roman \"{}\"\n    ))",
                escaped_family
            ))
        }
    }
}

/// Build the global content string (key, time, partial) from song tags.
fn build_global_content(song: &Song) -> String {
    let mut global_lines: Vec<String> = Vec::new();
    if let Some(key_str) = song.score.key.as_ref()
        && let Some(ly_key) = format_lilypond_key(key_str) {
            global_lines.push(ly_key);
        }
    if let Some(time_str) = song.score.time.as_ref() {
        global_lines.push(format!("\\time {}", time_str));
    }
    if let Some(partial_str) = song.score.partial {
        global_lines.push(format!("\\partial {}", partial_str));
    }
    indent_lines(&global_lines.join("\n"), "  ")
}

// ---------------------------------------------------------------------------
// Sequential export types and template
// ---------------------------------------------------------------------------

/// Data for a lyrics definition in the sequential template.
#[derive(Serialize)]
struct LyricsDefinition {
    var_name: String,
    stanza: Option<String>,
    content: String,
}

/// Data for a single section in the sequential output (one `\score` block).
#[derive(Serialize)]
struct SequentialSection {
    label: String,
    voice_ref: String,
    lyrics_ref: String,
    midi_instrument: String,
}

/// All data needed to render the sequential LilyPond template.
#[derive(Serialize)]
struct SequentialTemplateData {
    version: String,
    title: String,
    composer: Option<String>,
    paper_size: String,
    layout_indent: String,
    global_content: String,
    voice_defs: Vec<VoiceDefinition>,
    lyrics_defs: Vec<LyricsDefinition>,
    sections: Vec<SequentialSection>,
    staff_size: Option<f32>,
    font_block: Option<String>,
}

/// The Handlebars template for the **sequential** LilyPond file structure.
///
/// Each section of the singing order gets its own `\score` block, producing
/// output that mirrors exactly how the song is performed.
const LILYPOND_SEQUENTIAL_TEMPLATE: &str = r#"\version "{{{version}}}"

\header {
  title = "{{{title}}}"
{{#if composer}}  composer = "{{{composer}}}"
{{/if}}  tagline = ##f
}
{{#if staff_size}}
#(set-global-staff-size {{{staff_size}}})
{{/if}}
\paper {
  #(set-paper-size "{{{paper_size}}}")
{{#if font_block}}{{{font_block}}}
{{/if}}}

\layout {
  indent = {{{layout_indent}}}
  \context {
    \Voice
    \consists "Melody_engraver"
    \override Stem.neutral-direction = #'()
  }
}

global = {
{{{global_content}}}
}

{{#each voice_defs}}
{{{this.var_name}}} = \relative c' {
{{#if this.include_global}}  \global
{{/if}}{{{this.content}}}
}

{{/each}}
{{#each lyrics_defs}}
{{{this.var_name}}} = \lyricmode {
{{#if this.stanza}}  \set stanza = "{{{this.stanza}}}"
{{/if}}{{{this.content}}}
}

{{/each}}
{{#each sections}}
% === {{{this.label}}} ===
\score {
  <<
    \new Staff \with {
      midiInstrument = "{{{this.midi_instrument}}}"
    } { {{{this.voice_ref}}} }
    \addlyrics { {{{this.lyrics_ref}}} }
  >>
  \header { piece = "{{{this.label}}}" }
  \layout { }
}

{{/each}}"#;

// ---------------------------------------------------------------------------
// Per-part export types and template
// ---------------------------------------------------------------------------

/// A standalone LilyPond file for a single song part.
pub struct LilypondPart {
    /// Human-readable label (e.g. "Stanza 1", "Refrain")
    pub label: String,
    /// Complete `.ly` file content for this part
    pub ly_content: String,
}

/// All data needed to render a standalone per-part LilyPond file.
#[derive(Serialize)]
struct PartTemplateData {
    version: String,
    global_content: String,
    voice_var_name: String,
    voice_content: String,
    voice_ref: String,
    lyrics_var_name: String,
    lyrics_content: String,
    lyrics_ref: String,
    stanza: Option<String>,
    staff_size: Option<f32>,
    font_block: Option<String>,
}

/// The Handlebars template for a standalone single-part LilyPond file.
///
/// Used to generate cropped SVGs for individual song parts.
const LILYPOND_PART_TEMPLATE: &str = r#"\version "{{{version}}}"
{{#if staff_size}}
#(set-global-staff-size {{{staff_size}}})
{{/if}}
\paper {
  indent = #0
{{#if font_block}}{{{font_block}}}
{{/if}}}

\layout {
  \context {
    \Voice
    \consists "Melody_engraver"
    \override Stem.neutral-direction = #'()
  }
}

global = {
{{{global_content}}}
}

{{{voice_var_name}}} = \relative c' {
  \global
{{{voice_content}}}
}

{{{lyrics_var_name}}} = \lyricmode {
{{#if stanza}}  \set stanza = "{{{stanza}}}"
{{/if}}{{{lyrics_content}}}
}

\score {
  <<
    \new Staff { {{{voice_ref}}} }
    \addlyrics { {{{lyrics_ref}}} }
  >>
  \layout { }
}
"#;

/// Generate a complete LilyPond (.ly) file from a Song.
///
/// The output uses a variable-based structure with `\relative c'` wrapping,
/// configurable `\paper` and `\layout` blocks, and numbered verse variables.
///
/// When a refrain or chorus has its own melody (voice content), the exporter
/// creates separate voice variables for stanza and refrain (e.g.
/// `sopranoVoiceStanza` and `sopranoVoiceRefrain`) and a separate `chorus`
/// lyrics variable. The first `\addlyrics` line then references both, so the
/// refrain text stays an independent, reusable block:
///
/// ```text
/// \addlyrics { \verseOne \chorus }
/// \addlyrics { \verseTwo }
/// ```
///
/// For refrain-first songs the order is reversed. Because the refrain melody
/// then comes first on the staff, the later verses have to skip past it to end
/// up underneath the first verse:
///
/// ```text
/// \addlyrics { \chorus \verseOne }
/// \addlyrics { \chorusSkip \verseTwo }
/// ```
///
/// Returns an error if the song has no voice content to export.
pub fn lilypond_from_song(song: &Song, settings: &LilypondSettings) -> Result<String, String> {
    let parts = song.parts();

    // --- Step 1: Find the stanza (verse) voice ---
    let stanza_voice = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .find_map(|part| song.voice_for_part(part))
        .ok_or_else(|| "Song has no voice content for LilyPond export".to_string())?;

    let base_voice_name = voice_type_to_var_name(&stanza_voice.content_type).to_string();
    let voice_part_name = format!("{}Part", base_voice_name);
    let voice_part_ref = format!("\\{}", voice_part_name);

    // --- Step 2: Check refrain/chorus parts for their own independent voice ---
    let refrain_parts: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Refrain || p.part_type == SongPartType::Chorus)
        .collect();

    // A refrain "owns" its voice if the voice content is directly on the part,
    // not inherited from a verse via is_repetition_of.
    let refrain_own_voice: Option<&SongPartContent> =
        refrain_parts.iter().find_map(|part| find_own_voice(part));

    // The refrain only becomes its own `chorus` lyrics variable if it has its
    // own melody; otherwise it shares the stanza music and is printed as a
    // further `\addlyrics` line (see step 6).
    let chorus_lyrics: Option<String> = if refrain_own_voice.is_some() {
        refrain_parts
            .iter()
            .find_map(|part| find_lyrics(part))
            .map(|c| c.content.clone())
    } else {
        None
    };

    // --- Step 3: Determine ordering and build voice definitions ---
    let is_refrain_first = song
        .part_orders
        .first()
        .is_some_and(|o| o.is_refrain_first());

    let mut voice_defs: Vec<VoiceDefinition> = Vec::new();
    let combined_voice_refs: String;

    if let Some(rv) = refrain_own_voice {
        // Separate voice variables for stanza and refrain
        let stanza_var = format!("{}Stanza", base_voice_name);
        let refrain_var = format!("{}Refrain", base_voice_name);

        let stanza_content = indent_lines(stanza_voice.content.trim(), "  ");
        let refrain_content = indent_lines(rv.content.trim(), "  ");

        if is_refrain_first {
            combined_voice_refs = format!("\\{} \\{}", refrain_var, stanza_var);
            voice_defs.push(VoiceDefinition {
                var_name: refrain_var.clone(),
                content: refrain_content,
                include_global: true,
            });
            voice_defs.push(VoiceDefinition {
                var_name: stanza_var.clone(),
                content: ensure_final_bar(&stanza_content),
                include_global: false,
            });
        } else {
            combined_voice_refs = format!("\\{} \\{}", stanza_var, refrain_var);
            voice_defs.push(VoiceDefinition {
                var_name: stanza_var.clone(),
                content: stanza_content,
                include_global: true,
            });
            voice_defs.push(VoiceDefinition {
                var_name: refrain_var.clone(),
                content: ensure_final_bar(&refrain_content),
                include_global: false,
            });
        }
    } else {
        // Single voice variable (no independent refrain melody)
        voice_defs.push(VoiceDefinition {
            var_name: base_voice_name.clone(),
            content: ensure_final_bar(&indent_lines(stanza_voice.content.trim(), "  ")),
            include_global: true,
        });
        combined_voice_refs = format!("\\{}", base_voice_name);
    }

    // --- Step 4: Build global content (key, time, partial) ---
    let global_content = build_global_content(song);

    // --- Step 5: Collect verse lyrics as numbered variables ---
    let mut verse_parts_sorted: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .collect();
    verse_parts_sorted.sort_by_key(|p| p.number);

    let mut lyric_defs: Vec<LyricsDefinition> = Vec::new();
    let mut verse_refs: Vec<String> = Vec::new();
    let mut verse_number: u32 = 1;

    for part in &verse_parts_sorted {
        for content in &part.contents {
            if content.content_type.is_lyrics() {
                let var_name = format!("verse{}", number_to_word(verse_number));
                verse_refs.push(format!("\\{}", var_name));
                lyric_defs.push(LyricsDefinition {
                    var_name,
                    stanza: Some(format!("{}.", verse_number)),
                    content: indent_lines(&content.content, "  "),
                });
                verse_number += 1;
            }
        }
    }

    // --- Step 6: The refrain lyrics ---
    let mut lyric_lines: Vec<LyricLine> = Vec::new();

    if let Some(ref chorus_text) = chorus_lyrics {
        // The refrain has its own melody, so it becomes its own `chorus`
        // variable that is referenced from the first `\addlyrics` line instead
        // of being pasted into the first verse's text.
        lyric_defs.push(LyricsDefinition {
            var_name: "chorus".to_string(),
            // No `\set stanza` — the refrain carries no verse number.
            stanza: None,
            content: indent_lines(chorus_text.trim(), "  "),
        });

        if verse_refs.is_empty() {
            // A refrain melody but no verse lyrics — print the refrain alone
            // rather than leaving the variable unreferenced.
            lyric_lines.push(LyricLine {
                refs: "\\chorus".to_string(),
            });
        } else if is_refrain_first {
            // The refrain melody comes first on the staff. Verses after the
            // first therefore have to skip its syllables, otherwise LilyPond
            // would align them with the refrain instead of the stanza.
            let skip_count = count_lyric_syllables(chorus_text);
            if verse_refs.len() > 1 && skip_count > 0 {
                lyric_defs.push(LyricsDefinition {
                    var_name: "chorusSkip".to_string(),
                    stanza: None,
                    content: format!("  \\repeat unfold {} {{ \\skip 1 }}", skip_count),
                });
            }

            for (index, verse_ref) in verse_refs.iter().enumerate() {
                lyric_lines.push(LyricLine {
                    refs: if index == 0 {
                        format!("\\chorus {}", verse_ref)
                    } else if skip_count > 0 {
                        format!("\\chorusSkip {}", verse_ref)
                    } else {
                        verse_ref.clone()
                    },
                });
            }
        } else {
            for (index, verse_ref) in verse_refs.iter().enumerate() {
                lyric_lines.push(LyricLine {
                    refs: if index == 0 {
                        format!("{} \\chorus", verse_ref)
                    } else {
                        verse_ref.clone()
                    },
                });
            }
        }
    } else {
        // The refrain shares the verse melody (or there is none), so every
        // verse gets a plain line and the refrain follows as an extra one.
        for verse_ref in &verse_refs {
            lyric_lines.push(LyricLine {
                refs: verse_ref.clone(),
            });
        }

        let mut refrain_number: u32 = 1;
        for part in &refrain_parts {
            for content in &part.contents {
                if content.content_type.is_lyrics() {
                    let var_name = format!("refrain{}", number_to_word(refrain_number));
                    lyric_lines.push(LyricLine {
                        refs: format!("\\{}", var_name),
                    });
                    lyric_defs.push(LyricsDefinition {
                        var_name,
                        stanza: Some(format!("R{}.", refrain_number)),
                        content: indent_lines(&content.content, "  "),
                    });
                    refrain_number += 1;
                }
            }
        }
    }

    // --- Step 7: Check for chord content ---
    let chord_content_opt = parts.iter().find_map(|part| {
        part.contents
            .iter()
            .find(|c| matches!(c.content_type, SongPartContentType::Chords))
            .map(|c| c.content.clone())
    });
    let has_chords = chord_content_opt.is_some();
    let chord_content = chord_content_opt
        .map(|c| indent_lines(&c, "  "))
        .unwrap_or_default();

    // --- Step 8: Build template data and render ---
    let data = LilypondTemplateData {
        version: "2.24.0".to_string(),
        title: song.title.clone(),
        composer: song.tag("author").cloned(),
        paper_size: settings.paper_size.clone(),
        layout_indent: settings.layout_indent.clone(),
        global_content,
        has_chords,
        chord_content,
        voice_defs,
        combined_voice_refs,
        voice_part_name,
        voice_part_ref,
        midi_instrument: "choir aahs".to_string(),
        lyric_defs,
        lyric_lines,
        staff_size: settings.staff_size,
        font_block: build_font_block(&settings.font),
    };

    let handlebars = Handlebars::new();
    handlebars
        .render_template(LILYPOND_TEMPLATE, &data)
        .map_err(|e| format!("Template rendering failed: {}", e))
}

// ---------------------------------------------------------------------------
// Sequential export
// ---------------------------------------------------------------------------

/// Generate a **sequential** LilyPond (.ly) file from a Song.
///
/// The output contains one `\score` block for every part in the singing order
/// (e.g. stanza 1, refrain, stanza 2, refrain, …). Voice and lyrics definitions
/// are shared via LilyPond variables and referenced from each score block.
///
/// Returns an error if the song has no voice content to export.
pub fn lilypond_sequential_from_song(
    song: &Song,
    settings: &LilypondSettings,
) -> Result<String, String> {
    let parts = song.parts();

    // --- Find the stanza (verse) voice ---
    let stanza_voice = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .find_map(|part| song.voice_for_part(part))
        .ok_or_else(|| "Song has no voice content for LilyPond export".to_string())?;

    let base_voice_name = voice_type_to_var_name(&stanza_voice.content_type).to_string();

    // --- Check refrain/chorus parts for their own independent voice ---
    let refrain_parts: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Refrain || p.part_type == SongPartType::Chorus)
        .collect();

    let refrain_own_voice: Option<&SongPartContent> =
        refrain_parts.iter().find_map(|part| find_own_voice(part));

    // --- Build voice definitions ---
    let mut voice_defs: Vec<VoiceDefinition> = Vec::new();
    let stanza_voice_ref: String;
    let refrain_voice_ref: Option<String>;

    if let Some(rv) = refrain_own_voice {
        let stanza_var = format!("{}Stanza", base_voice_name);
        let refrain_var = format!("{}Refrain", base_voice_name);

        stanza_voice_ref = format!("\\{}", stanza_var);
        refrain_voice_ref = Some(format!("\\{}", refrain_var));

        voice_defs.push(VoiceDefinition {
            var_name: stanza_var,
            content: ensure_final_bar(&indent_lines(stanza_voice.content.trim(), "  ")),
            include_global: true,
        });
        voice_defs.push(VoiceDefinition {
            var_name: refrain_var,
            content: ensure_final_bar(&indent_lines(rv.content.trim(), "  ")),
            include_global: false,
        });
    } else {
        voice_defs.push(VoiceDefinition {
            var_name: base_voice_name.clone(),
            content: ensure_final_bar(&indent_lines(stanza_voice.content.trim(), "  ")),
            include_global: true,
        });
        stanza_voice_ref = format!("\\{}", base_voice_name);
        refrain_voice_ref = None;
    }

    // --- Build lyrics definitions ---
    let mut lyrics_defs: Vec<LyricsDefinition> = Vec::new();

    // Collect verse lyrics
    let mut verse_parts_sorted: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .collect();
    verse_parts_sorted.sort_by_key(|p| p.number);

    let mut verse_var_refs: Vec<(u32, String)> = Vec::new(); // (verse_number, var_ref)
    let mut verse_number: u32 = 1;

    for part in &verse_parts_sorted {
        for content in &part.contents {
            if content.content_type.is_lyrics() {
                let var_name = format!("verse{}", number_to_word(verse_number));
                let var_ref = format!("\\{}", var_name);
                verse_var_refs.push((verse_number, var_ref));
                lyrics_defs.push(LyricsDefinition {
                    var_name,
                    stanza: Some(format!("{}.", verse_number)),
                    content: indent_lines(&content.content, "  "),
                });
                verse_number += 1;
            }
        }
    }

    // Collect refrain lyrics
    let mut refrain_lyrics_ref: Option<String> = None;
    for part in &refrain_parts {
        for content in &part.contents {
            if content.content_type.is_lyrics() && refrain_lyrics_ref.is_none() {
                let var_name = "refrainLyrics".to_string();
                let var_ref = format!("\\{}", var_name);
                refrain_lyrics_ref = Some(var_ref);
                lyrics_defs.push(LyricsDefinition {
                    var_name,
                    stanza: None,
                    content: indent_lines(&content.content, "  "),
                });
            }
        }
    }

    // --- Build the singing order sections ---
    let is_refrain_first = song
        .part_orders
        .first()
        .is_some_and(|o| o.is_refrain_first());

    let r_voice_ref = refrain_voice_ref
        .as_deref()
        .unwrap_or(&stanza_voice_ref);
    let midi_instrument = "choir aahs".to_string();

    let mut sections: Vec<SequentialSection> = Vec::new();

    if is_refrain_first
        && let Some(ref r_lyrics_ref) = refrain_lyrics_ref {
            sections.push(SequentialSection {
                label: "Refrain".to_string(),
                voice_ref: r_voice_ref.to_string(),
                lyrics_ref: r_lyrics_ref.clone(),
                midi_instrument: midi_instrument.clone(),
            });
        }

    for (vnum, v_lyrics_ref) in &verse_var_refs {
        sections.push(SequentialSection {
            label: format!("Stanza {}", vnum),
            voice_ref: stanza_voice_ref.clone(),
            lyrics_ref: v_lyrics_ref.clone(),
            midi_instrument: midi_instrument.clone(),
        });
        if let Some(ref r_lyrics_ref) = refrain_lyrics_ref {
            sections.push(SequentialSection {
                label: "Refrain".to_string(),
                voice_ref: r_voice_ref.to_string(),
                lyrics_ref: r_lyrics_ref.clone(),
                midi_instrument: midi_instrument.clone(),
            });
        }
    }

    // --- Build global content and render ---
    let global_content = build_global_content(song);

    let data = SequentialTemplateData {
        version: "2.24.0".to_string(),
        title: song.title.clone(),
        composer: song.tag("author").cloned(),
        paper_size: settings.paper_size.clone(),
        layout_indent: settings.layout_indent.clone(),
        global_content,
        voice_defs,
        lyrics_defs,
        sections,
        staff_size: settings.staff_size,
        font_block: build_font_block(&settings.font),
    };

    let handlebars = Handlebars::new();
    handlebars
        .render_template(LILYPOND_SEQUENTIAL_TEMPLATE, &data)
        .map_err(|e| format!("Template rendering failed: {}", e))
}

// ---------------------------------------------------------------------------
// Per-part export
// ---------------------------------------------------------------------------

/// Generate standalone LilyPond (.ly) files for each part in singing order.
///
/// Each returned [`LilypondPart`] contains a complete, self-contained `.ly`
/// file that can be compiled independently to produce a cropped SVG for that
/// song part.
pub fn lilypond_parts_from_song(
    song: &Song,
    settings: &LilypondSettings,
) -> Result<Vec<LilypondPart>, String> {
    let parts = song.parts();

    // --- Find the stanza (verse) voice ---
    let stanza_voice = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .find_map(|part| song.voice_for_part(part))
        .ok_or_else(|| "Song has no voice content for LilyPond export".to_string())?;

    let base_voice_name = voice_type_to_var_name(&stanza_voice.content_type).to_string();

    // --- Check refrain/chorus for own voice ---
    let refrain_parts: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Refrain || p.part_type == SongPartType::Chorus)
        .collect();

    let refrain_own_voice: Option<&SongPartContent> =
        refrain_parts.iter().find_map(|part| find_own_voice(part));

    // --- Determine voice content for stanza and refrain ---
    let stanza_voice_content =
        ensure_final_bar(&indent_lines(stanza_voice.content.trim(), "  "));
    let stanza_voice_var = if refrain_own_voice.is_some() {
        format!("{}Stanza", base_voice_name)
    } else {
        base_voice_name.clone()
    };

    let refrain_voice_var: String;
    let refrain_voice_content: String;
    if let Some(rv) = refrain_own_voice {
        refrain_voice_var = format!("{}Refrain", base_voice_name);
        refrain_voice_content =
            ensure_final_bar(&indent_lines(rv.content.trim(), "  "));
    } else {
        refrain_voice_var = stanza_voice_var.clone();
        refrain_voice_content = stanza_voice_content.clone();
    }

    // --- Collect verse lyrics ---
    let mut verse_parts_sorted: Vec<_> = parts
        .iter()
        .filter(|p| p.part_type == SongPartType::Verse)
        .collect();
    verse_parts_sorted.sort_by_key(|p| p.number);

    let mut verse_lyrics: Vec<(u32, String)> = Vec::new(); // (number, content)
    let mut verse_number: u32 = 1;
    for part in &verse_parts_sorted {
        for content in &part.contents {
            if content.content_type.is_lyrics() {
                verse_lyrics.push((verse_number, content.content.clone()));
                verse_number += 1;
            }
        }
    }

    // --- Collect refrain lyrics ---
    let refrain_lyrics: Option<String> = refrain_parts
        .iter()
        .find_map(|part| find_lyrics(part))
        .map(|c| c.content.clone());

    // --- Build global content ---
    let global_content = build_global_content(song);

    // --- Build singing order ---
    let is_refrain_first = song
        .part_orders
        .first()
        .is_some_and(|o| o.is_refrain_first());

    let handlebars = Handlebars::new();
    let font_block = build_font_block(&settings.font);

    let mut result: Vec<LilypondPart> = Vec::new();

    // Helper closure to create a part LY from a voice/lyrics pair
    let render_part = |voice_var: &str,
                       voice_content: &str,
                       lyrics_var: &str,
                       lyrics_content: &str,
                       stanza: Option<String>|
     -> Result<String, String> {
        let data = PartTemplateData {
            version: "2.24.0".to_string(),
            global_content: global_content.clone(),
            voice_var_name: voice_var.to_string(),
            voice_content: voice_content.to_string(),
            voice_ref: format!("\\{}", voice_var),
            lyrics_var_name: lyrics_var.to_string(),
            lyrics_content: indent_lines(lyrics_content, "  "),
            lyrics_ref: format!("\\{}", lyrics_var),
            stanza,
            staff_size: settings.staff_size,
            font_block: font_block.clone(),
        };
        handlebars
            .render_template(LILYPOND_PART_TEMPLATE, &data)
            .map_err(|e| format!("Template rendering failed: {}", e))
    };

    if is_refrain_first
        && let Some(ref r_lyrics) = refrain_lyrics {
            let ly = render_part(
                &refrain_voice_var,
                &refrain_voice_content,
                "refrainLyrics",
                r_lyrics,
                None,
            )?;
            result.push(LilypondPart {
                label: "Refrain".to_string(),
                ly_content: ly,
            });
        }

    for (vnum, v_lyrics) in &verse_lyrics {
        let lyrics_var = format!("verse{}", number_to_word(*vnum));
        let ly = render_part(
            &stanza_voice_var,
            &stanza_voice_content,
            &lyrics_var,
            v_lyrics,
            Some(format!("{}.", vnum)),
        )?;
        result.push(LilypondPart {
            label: format!("Stanza {}", vnum),
            ly_content: ly,
        });

        if let Some(ref r_lyrics) = refrain_lyrics {
            let ly = render_part(
                &refrain_voice_var,
                &refrain_voice_content,
                "refrainLyrics",
                r_lyrics,
                None,
            )?;
            result.push(LilypondPart {
                label: "Refrain".to_string(),
                ly_content: ly,
            });
        }
    }

    Ok(result)
}

// ---------------------------------------------------------------------------
// LilyPond rendering (SVG / PDF via the LilyPond binary)
// ---------------------------------------------------------------------------

/// A rendered song part with its label and SVG content.
pub struct RenderedPart {
    /// Human-readable label (e.g. "Stanza 1", "Refrain")
    pub label: String,
    /// The SVG content as bytes
    pub svg: Vec<u8>,
}

/// Render a LilyPond string to a **cropped SVG** via the LilyPond binary.
///
/// # Arguments
/// * `ly_content` – Complete `.ly` file content
/// * `lilypond_bin` – Path to the `lilypond` executable
///
/// # Returns
/// The SVG file content as a byte vector.
pub fn render_lilypond_to_svg(ly_content: &str, lilypond_bin: &Path) -> Result<Vec<u8>, String> {
    let temp_dir =
        tempfile::tempdir().map_err(|e| format!("Failed to create temp dir: {}", e))?;
    let ly_path = temp_dir.path().join("input.ly");
    let out_base = temp_dir.path().join("output");

    std::fs::write(&ly_path, ly_content)
        .map_err(|e| format!("Failed to write LY file: {}", e))?;

    let output = std::process::Command::new(lilypond_bin)
        .arg("-dcrop")
        .arg("-dbackend=svg")
        .arg("-o")
        .arg(&out_base)
        .arg(&ly_path)
        .output()
        .map_err(|e| format!("Failed to run LilyPond: {}", e))?;

    if !output.status.success() {
        return Err(format!(
            "LilyPond failed:\n{}",
            String::from_utf8_lossy(&output.stderr)
        ));
    }

    // LilyPond with -dcrop generates output.cropped.svg
    let svg_path = temp_dir.path().join("output.cropped.svg");
    if svg_path.exists() {
        return std::fs::read(&svg_path)
            .map_err(|e| format!("Failed to read cropped SVG output: {}", e));
    }

    // Fallback: try output.svg (older LilyPond versions)
    let svg_fallback = temp_dir.path().join("output.svg");
    std::fs::read(&svg_fallback)
        .map_err(|e| format!("No SVG output found (neither cropped nor standard): {}", e))
}

/// Render a LilyPond string to **PDF** via the LilyPond binary.
///
/// # Arguments
/// * `ly_content` – Complete `.ly` file content
/// * `lilypond_bin` – Path to the `lilypond` executable
///
/// # Returns
/// The PDF file content as a byte vector.
pub fn render_lilypond_to_pdf(ly_content: &str, lilypond_bin: &Path) -> Result<Vec<u8>, String> {
    let temp_dir =
        tempfile::tempdir().map_err(|e| format!("Failed to create temp dir: {}", e))?;
    let ly_path = temp_dir.path().join("input.ly");
    let out_base = temp_dir.path().join("output");

    std::fs::write(&ly_path, ly_content)
        .map_err(|e| format!("Failed to write LY file: {}", e))?;

    let output = std::process::Command::new(lilypond_bin)
        .arg("-o")
        .arg(&out_base)
        .arg(&ly_path)
        .output()
        .map_err(|e| format!("Failed to run LilyPond: {}", e))?;

    if !output.status.success() {
        return Err(format!(
            "LilyPond failed:\n{}",
            String::from_utf8_lossy(&output.stderr)
        ));
    }

    let pdf_path = temp_dir.path().join("output.pdf");
    std::fs::read(&pdf_path)
        .map_err(|e| format!("Failed to read PDF output (file may not have been generated): {}", e))
}

/// Render each song part as a **cropped SVG** via LilyPond.
///
/// Generates a standalone `.ly` file for each part in singing order, compiles
/// each one with LilyPond, and returns the cropped SVG for every part.
///
/// # Arguments
/// * `song` – The song to render
/// * `settings` – LilyPond export settings
/// * `lilypond_bin` – Path to the `lilypond` executable
pub fn render_song_parts_to_svg(
    song: &Song,
    settings: &LilypondSettings,
    lilypond_bin: &Path,
) -> Result<Vec<RenderedPart>, String> {
    let ly_parts = lilypond_parts_from_song(song, settings)?;
    let mut rendered: Vec<RenderedPart> = Vec::new();

    for part in ly_parts {
        let svg = render_lilypond_to_svg(&part.ly_content, lilypond_bin)?;
        rendered.push(RenderedPart {
            label: part.label,
            svg,
        });
    }

    Ok(rendered)
}

/// Render the **paper score** of a song as SVG via LilyPond.
///
/// # Arguments
/// * `song` – The song to render
/// * `settings` – LilyPond export settings (paper_size controls the SVG dimensions)
/// * `lilypond_bin` – Path to the `lilypond` executable
pub fn render_paper_score_to_svg(
    song: &Song,
    settings: &LilypondSettings,
    lilypond_bin: &Path,
) -> Result<Vec<u8>, String> {
    let ly_content = lilypond_from_song(song, settings)?;
    render_lilypond_to_svg(&ly_content, lilypond_bin)
}

/// Render the **paper score** of a song as PDF via LilyPond.
///
/// # Arguments
/// * `song` – The song to render
/// * `settings` – LilyPond export settings
/// * `lilypond_bin` – Path to the `lilypond` executable
pub fn render_paper_score_to_pdf(
    song: &Song,
    settings: &LilypondSettings,
    lilypond_bin: &Path,
) -> Result<Vec<u8>, String> {
    let ly_content = lilypond_from_song(song, settings)?;
    render_lilypond_to_pdf(&ly_content, lilypond_bin)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::importer::song_yml;

    #[test]
    fn test_lilypond_export_simple_verses() {
        // Amazing Grace: verses only, no refrain with its own melody
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // Version
        assert!(ly_output.contains("\\version \"2.24.0\""));
        // Header
        assert!(ly_output.contains("title = \"Amazing Grace\""));
        assert!(ly_output.contains("composer = \"John Newton\""));
        assert!(ly_output.contains("tagline = ##f"));
        // Paper and layout
        assert!(ly_output.contains("#(set-paper-size \"a4\")"));
        assert!(ly_output.contains("indent = #0"));
        assert!(ly_output.contains("Melody_engraver"));
        // Global variable
        assert!(ly_output.contains("global = {"));
        assert!(ly_output.contains("\\key f \\major"));
        assert!(ly_output.contains("\\time 3/4"));
        assert!(ly_output.contains("\\partial 4"));
        // Voice in \relative c'
        assert!(ly_output.contains("sopranoVoice = \\relative c'"));
        assert!(ly_output.contains("\\global"));
        assert!(ly_output.contains("c4 | f2 a8( f)"));
        // Verse variables with stanza numbering
        assert!(ly_output.contains("verseOne = \\lyricmode"));
        assert!(ly_output.contains("\\set stanza = \"1.\""));
        assert!(ly_output.contains("A -- ma -- zing grace"));
        assert!(ly_output.contains("verseTwo = \\lyricmode"));
        assert!(ly_output.contains("\\set stanza = \"2.\""));
        assert!(ly_output.contains("verseThree = \\lyricmode"));
        assert!(ly_output.contains("\\set stanza = \"3.\""));
        // Part assembly
        assert!(ly_output.contains("sopranoVoicePart = \\new Staff"));
        assert!(ly_output.contains("midiInstrument = \"choir aahs\""));
        assert!(ly_output.contains("\\addlyrics { \\verseOne }"));
        assert!(ly_output.contains("\\addlyrics { \\verseTwo }"));
        assert!(ly_output.contains("\\addlyrics { \\verseThree }"));
        // Score
        assert!(ly_output.contains("\\score {"));
        assert!(ly_output.contains("\\layout { }"));
        assert!(ly_output.contains("\\midi { }"));
    }

    #[test]
    fn test_lilypond_export_stanza_refrain() {
        // "Sei nicht stolz" has stanza voice + refrain voice (separate melodies)
        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // Separate voice variables for stanza and refrain
        assert!(
            ly_output.contains("sopranoVoiceStanza = \\relative c'"),
            "Stanza voice variable missing"
        );
        assert!(
            ly_output.contains("sopranoVoiceRefrain = \\relative c'"),
            "Refrain voice variable missing"
        );

        // Both melodies should be present
        assert!(
            ly_output.contains("d8 e | fis4 fis g4 fis8 e"),
            "Stanza melody missing"
        );
        assert!(
            ly_output.contains("fis8( g ) | a8 a a a d,4. d8"),
            "Refrain melody missing"
        );

        // Staff should reference both voice variables in stanza-refrain order
        assert!(
            ly_output.contains("\\sopranoVoiceStanza \\sopranoVoiceRefrain"),
            "Staff should combine stanza and refrain voices"
        );

        // There should NOT be a single combined sopranoVoice variable
        assert!(
            !ly_output.contains("sopranoVoice = \\relative c'"),
            "Should not have a single combined sopranoVoice"
        );

        // The refrain gets its own lyrics variable — it must not be pasted
        // into the first verse's text.
        assert!(ly_output.contains("chorus = \\lyricmode"));
        assert!(
            ly_output.contains("Denn wer sich"),
            "Refrain lyrics missing"
        );
        assert!(
            !ly_output.contains("refrainOne"),
            "Refrain with own voice should not produce a numbered refrain variable"
        );

        // The chorus carries no verse number.
        let chorus_start = ly_output.find("chorus = \\lyricmode").unwrap();
        let chorus_end = chorus_start + ly_output[chorus_start..].find("\n}\n").unwrap();
        let chorus_block = &ly_output[chorus_start..chorus_end];
        assert!(
            !chorus_block.contains("\\set stanza"),
            "The refrain must not get a stanza number:\n{}",
            chorus_block
        );

        // Verse 1 holds only its own text …
        let verse_one_start = ly_output.find("verseOne = \\lyricmode").unwrap();
        let verse_one_end = verse_one_start + ly_output[verse_one_start..].find("\n}\n").unwrap();
        let verse_one_block = &ly_output[verse_one_start..verse_one_end];
        assert!(verse_one_block.contains("Sei nicht stolz"));
        assert!(
            !verse_one_block.contains("Denn wer sich"),
            "Refrain text must not be concatenated into verse 1:\n{}",
            verse_one_block
        );

        // … and the refrain is attached in the \addlyrics line instead.
        assert!(
            ly_output.contains("\\addlyrics { \\verseOne \\chorus }"),
            "Refrain should be referenced from the first lyrics line:\n{}",
            ly_output
        );
        assert!(ly_output.contains("\\addlyrics { \\verseTwo }"));
        assert!(ly_output.contains("\\addlyrics { \\verseThree }"));

        // A stanza-first song needs no skip variable.
        assert!(
            !ly_output.contains("chorusSkip"),
            "chorusSkip is only needed when the refrain comes first"
        );

        // There should be exactly 3 \addlyrics (one per verse)
        let addlyrics_count = ly_output.matches("\\addlyrics").count();
        assert_eq!(
            addlyrics_count, 3,
            "Expected 3 addlyrics, got {}",
            addlyrics_count
        );
    }

    #[test]
    fn test_custom_settings() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            paper_size: "a5".to_string(),
            layout_indent: "#10".to_string(),
            ..LilypondSettings::default()
        };

        let ly_output = lilypond_from_song(&song, &settings).unwrap();

        assert!(ly_output.contains("#(set-paper-size \"a5\")"));
        assert!(ly_output.contains("indent = #10"));
    }

    #[test]
    fn test_format_lilypond_key() {
        assert_eq!(
            format_lilypond_key("f major"),
            Some("\\key f \\major".to_string())
        );
        assert_eq!(
            format_lilypond_key("d minor"),
            Some("\\key d \\minor".to_string())
        );
        assert_eq!(format_lilypond_key("invalid"), None);
    }

    #[test]
    fn test_number_to_word() {
        assert_eq!(number_to_word(1), "One");
        assert_eq!(number_to_word(5), "Five");
        assert_eq!(number_to_word(10), "Ten");
        assert_eq!(number_to_word(20), "Twenty");
        assert_eq!(number_to_word(21), "N21");
    }

    #[test]
    fn test_lilypond_export_refrain_stanza_refrain() {
        // Test refrain-stanza-refrain ordering: refrain melody comes first
        let yml = r#"
version: 0.1
title: Refrain First Song
default_language: en
tags:
  author: Test Author
score:
  key: c major
  time: 4/4
orders:
  - refrain-stanza-refrain
parts:
  - type: refrain
    contents:
    - type: voice
      number: 1
      content: |
        c4 d e f | g2 g2
    - type: lyrics
      number: 1
      content: |
        Refrain text here, la la la la
  - type: stanza
    contents:
    - type: voice
      number: 1
      content: |
        e4 f g a | b2 b2
    - type: lyrics
      number: 1
      content: |
        First verse lyrics here
    - type: lyrics
      number: 2
      content: |
        Second verse lyrics here
"#;
        let song = song_yml::import_from_yml_string(yml).unwrap();
        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // Separate voice variables for refrain and stanza
        assert!(
            ly_output.contains("sopranoVoiceRefrain = \\relative c'"),
            "Refrain voice variable missing"
        );
        assert!(
            ly_output.contains("sopranoVoiceStanza = \\relative c'"),
            "Stanza voice variable missing"
        );

        // Staff should reference refrain BEFORE stanza (refrain-first order)
        assert!(
            ly_output.contains("\\sopranoVoiceRefrain \\sopranoVoiceStanza"),
            "Staff should have refrain before stanza for refrain-first songs"
        );

        // The refrain is its own variable, referenced first in the lyrics line.
        assert!(ly_output.contains("chorus = \\lyricmode"));
        assert!(
            ly_output.contains("\\addlyrics { \\chorus \\verseOne }"),
            "The first lyrics line should start with the refrain:\n{}",
            ly_output
        );

        // Verses after the first have to skip the refrain's syllables so that
        // they end up underneath the first verse, not underneath the refrain.
        // "Refrain text here, la la la la" = 7 syllables.
        assert!(
            ly_output.contains("chorusSkip = \\lyricmode"),
            "A refrain-first song needs a skip variable:\n{}",
            ly_output
        );
        assert!(
            ly_output.contains("\\repeat unfold 7 { \\skip 1 }"),
            "chorusSkip must skip one slot per refrain syllable:\n{}",
            ly_output
        );
        assert!(
            ly_output.contains("\\addlyrics { \\chorusSkip \\verseTwo }"),
            "Verse 2 should skip past the refrain:\n{}",
            ly_output
        );

        // Neither verse variable may contain the refrain's text.
        let verse_one_start = ly_output.find("verseOne = \\lyricmode").unwrap();
        let verse_two_start = ly_output.find("verseTwo = \\lyricmode").unwrap();
        let verse_one_block = &ly_output[verse_one_start..verse_two_start];
        assert!(verse_one_block.contains("First verse"));
        assert!(
            !verse_one_block.contains("Refrain text"),
            "Refrain text must not be concatenated into verse 1:\n{}",
            verse_one_block
        );

        let verse_two_end = ly_output[verse_two_start..].find("\n}\n").unwrap();
        let verse_two_block = &ly_output[verse_two_start..verse_two_start + verse_two_end];
        assert!(
            !verse_two_block.contains("Refrain text"),
            "Verse 2 should not contain refrain lyrics"
        );
        assert!(
            verse_two_block.contains("Second verse"),
            "Verse 2 should contain its own stanza lyrics"
        );

        // There should be exactly 2 \addlyrics (one per verse)
        let addlyrics_count = ly_output.matches("\\addlyrics").count();
        assert_eq!(
            addlyrics_count, 2,
            "Expected 2 addlyrics, got {}",
            addlyrics_count
        );
    }

    #[test]
    fn test_count_lyric_syllables() {
        assert_eq!(count_lyric_syllables("Refrain text here, la la la la"), 7);
        // `--` joins syllables and is not a slot of its own.
        assert_eq!(count_lyric_syllables("hei -- lig ist."), 3);
        assert_eq!(count_lyric_syllables("hei--lig ist."), 3);
        // `_` is a blank syllable and does occupy a note.
        assert_eq!(count_lyric_syllables("weg -- ge -- tan. _"), 4);
        // Commands and their arguments occupy nothing.
        assert_eq!(
            count_lyric_syllables("\\set ignoreMelismata = ##t Da -- rum \\unset ignoreMelismata"),
            2
        );
        assert_eq!(count_lyric_syllables(""), 0);
    }

    #[test]
    fn test_refrain_first_with_a_single_verse_needs_no_skip() {
        let yml = r#"
version: 0.1
title: One Verse
score:
  key: c major
  time: 4/4
orders:
  - refrain-stanza-refrain
parts:
  - type: refrain
    contents:
    - type: voice
      number: 1
      content: |
        c4 d e f
    - type: lyrics
      number: 1
      content: |
        one two three four
  - type: stanza
    contents:
    - type: voice
      number: 1
      content: |
        e4 f g a
    - type: lyrics
      number: 1
      content: |
        five six se -- ven
"#;
        let song = song_yml::import_from_yml_string(yml).unwrap();
        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        assert!(ly_output.contains("\\addlyrics { \\chorus \\verseOne }"));
        assert!(
            !ly_output.contains("chorusSkip"),
            "with only one verse nothing has to be skipped:\n{}",
            ly_output
        );
    }

    #[test]
    fn test_ensure_final_bar() {
        // Should add \bar "|." when not present
        assert_eq!(
            ensure_final_bar("  c4 d e f"),
            "  c4 d e f \\bar \"|.\""
        );
        // Should not duplicate when already present
        assert_eq!(
            ensure_final_bar("  f2. \\bar \"|.\""),
            "  f2. \\bar \"|.\""
        );
        // Should handle trailing whitespace
        assert_eq!(
            ensure_final_bar("  c4 d e f  \n"),
            "  c4 d e f \\bar \"|.\""
        );
    }

    #[test]
    fn test_final_bar_added_multi_section_melody() {
        // "Sei nicht stolz" has separate stanza and refrain voices; the stanza voice
        // does NOT end with \bar "|." in the source, but the export should add it.
        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();
        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // The last voice definition (refrain) should end with \bar "|."
        // Ensure that the final occurrence of \bar "|." appears after the last
        // voice variable header (sopranoVoiceRefrain), so we fail if the bar
        // line is added in the wrong place.
        let last_voice_header = "sopranoVoiceRefrain = \\relative c'";
        let last_voice_start = ly_output
            .find(last_voice_header)
            .expect("Last voice header (refrain) not found in LilyPond output");
        let final_bar_pos = ly_output
            .rfind("\\bar \"|.\"")
            .expect("LilyPond output should contain final bar line");
        assert!(
            final_bar_pos > last_voice_start,
            "Final bar line should appear after the last voice definition header"
        );
    }

    #[test]
    fn test_final_bar_not_duplicated() {
        // Amazing Grace already has \bar "|." in source - should not be duplicated
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();
        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        let bar_count = ly_output.matches("\\bar \"|.\"").count();
        assert_eq!(
            bar_count, 1,
            "Expected exactly 1 final bar, got {}",
            bar_count
        );
    }

    #[test]
    fn test_global_only_in_first_voice() {
        // "Sei nicht stolz" has separate stanza and refrain voices.
        // \global should only appear in the first voice definition.
        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();
        let ly_output = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // The stanza voice (first in stanza-refrain order) should have \global
        let stanza_start = ly_output.find("sopranoVoiceStanza = \\relative c'").unwrap();
        let refrain_start = ly_output.find("sopranoVoiceRefrain = \\relative c'").unwrap();
        let stanza_block = &ly_output[stanza_start..refrain_start];
        assert!(
            stanza_block.contains("\\global"),
            "First voice (stanza) should include \\global"
        );

        // The refrain voice (second) should NOT have \global
        let refrain_end = ly_output[refrain_start..].find("\n}\n").unwrap();
        let refrain_block = &ly_output[refrain_start..refrain_start + refrain_end];
        assert!(
            !refrain_block.contains("\\global"),
            "Second voice (refrain) should NOT include \\global"
        );
    }

    // ===================================================================
    // Sequential export tests
    // ===================================================================

    #[test]
    fn test_sequential_export_simple_verses() {
        // Amazing Grace: verses only → each verse gets its own \score
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly = lilypond_sequential_from_song(&song, &LilypondSettings::default()).unwrap();

        // Version and header
        assert!(ly.contains("\\version \"2.24.0\""));
        assert!(ly.contains("title = \"Amazing Grace\""));

        // Global variable
        assert!(ly.contains("global = {"));
        assert!(ly.contains("\\key f \\major"));

        // Voice definition
        assert!(ly.contains("sopranoVoice = \\relative c'"));

        // Verse lyrics as separate variables
        assert!(ly.contains("verseOne = \\lyricmode"));
        assert!(ly.contains("verseTwo = \\lyricmode"));
        assert!(ly.contains("verseThree = \\lyricmode"));

        // Three \score blocks (one per verse)
        let score_count = ly.matches("\\score {").count();
        assert_eq!(score_count, 3, "Expected 3 score blocks, got {}", score_count);

        // Section labels
        assert!(ly.contains("piece = \"Stanza 1\""));
        assert!(ly.contains("piece = \"Stanza 2\""));
        assert!(ly.contains("piece = \"Stanza 3\""));

        // Each score should reference the voice
        assert!(ly.contains("\\sopranoVoice"));

        // Should NOT have embedded refrain lyrics in verse
        // (sequential mode keeps them separate)
        assert!(
            !ly.contains("\\addlyrics { \\verseOne }\\n\\addlyrics"),
            "Sequential mode should not combine all addlyrics"
        );
    }

    #[test]
    fn test_sequential_export_stanza_refrain() {
        // "Sei nicht stolz" has stanza + refrain with separate melodies
        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly = lilypond_sequential_from_song(&song, &LilypondSettings::default()).unwrap();

        // Separate voice variables
        assert!(
            ly.contains("sopranoVoiceStanza = \\relative c'"),
            "Stanza voice variable missing"
        );
        assert!(
            ly.contains("sopranoVoiceRefrain = \\relative c'"),
            "Refrain voice variable missing"
        );

        // Separate lyrics variables
        assert!(ly.contains("verseOne = \\lyricmode"));
        assert!(ly.contains("verseTwo = \\lyricmode"));
        assert!(ly.contains("verseThree = \\lyricmode"));
        assert!(
            ly.contains("refrainLyrics = \\lyricmode"),
            "Refrain lyrics variable missing"
        );

        // 6 score blocks: stanza1, refrain, stanza2, refrain, stanza3, refrain
        let score_count = ly.matches("\\score {").count();
        assert_eq!(score_count, 6, "Expected 6 score blocks, got {}", score_count);

        // Section labels
        assert!(ly.contains("piece = \"Stanza 1\""));
        assert!(ly.contains("piece = \"Refrain\""));
        assert!(ly.contains("piece = \"Stanza 2\""));
        assert!(ly.contains("piece = \"Stanza 3\""));

        // Refrain sections should reference the refrain voice
        // Find the first refrain section and check it references refrain voice
        let refrain_section_pos = ly.find("piece = \"Refrain\"").unwrap();
        let section_start = ly[..refrain_section_pos].rfind("\\score {").unwrap();
        let section_block = &ly[section_start..refrain_section_pos + 50];
        assert!(
            section_block.contains("\\sopranoVoiceRefrain"),
            "Refrain score block should reference refrain voice"
        );
    }

    #[test]
    fn test_sequential_export_refrain_first() {
        let yml = r#"
version: 0.1
title: Refrain First Song
default_language: en
tags:
  author: Test Author
score:
  key: c major
  time: 4/4
orders:
  - refrain-stanza-refrain
parts:
  - type: refrain
    contents:
    - type: voice
      number: 1
      content: |
        c4 d e f | g2 g2
    - type: lyrics
      number: 1
      content: |
        Refrain text here, la la la la
  - type: stanza
    contents:
    - type: voice
      number: 1
      content: |
        e4 f g a | b2 b2
    - type: lyrics
      number: 1
      content: |
        First verse lyrics here
    - type: lyrics
      number: 2
      content: |
        Second verse lyrics here
"#;
        let song = song_yml::import_from_yml_string(yml).unwrap();
        let ly = lilypond_sequential_from_song(&song, &LilypondSettings::default()).unwrap();

        // 5 score blocks: refrain, stanza1, refrain, stanza2, refrain
        let score_count = ly.matches("\\score {").count();
        assert_eq!(score_count, 5, "Expected 5 score blocks, got {}", score_count);

        // The first score block should be the refrain
        let first_piece = ly.find("piece = ").unwrap();
        let first_piece_block = &ly[first_piece..first_piece + 30];
        assert!(
            first_piece_block.contains("Refrain"),
            "First section should be Refrain for refrain-first songs"
        );
    }

    #[test]
    fn test_sequential_no_refrain_lyrics_when_no_refrain() {
        // Amazing Grace has no refrain → no refrainLyrics variable
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly = lilypond_sequential_from_song(&song, &LilypondSettings::default()).unwrap();

        assert!(
            !ly.contains("refrainLyrics"),
            "Should not have refrain lyrics variable when no refrain exists"
        );
    }

    // ===================================================================
    // Per-part export tests
    // ===================================================================

    #[test]
    fn test_parts_export_simple_verses() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let parts = lilypond_parts_from_song(&song, &LilypondSettings::default()).unwrap();

        // 3 verses → 3 parts
        assert_eq!(parts.len(), 3, "Expected 3 parts, got {}", parts.len());

        // Check labels
        assert_eq!(parts[0].label, "Stanza 1");
        assert_eq!(parts[1].label, "Stanza 2");
        assert_eq!(parts[2].label, "Stanza 3");

        // Each part should be a standalone LY file
        for part in &parts {
            assert!(part.ly_content.contains("\\version \"2.24.0\""));
            assert!(part.ly_content.contains("\\score {"));
            assert!(part.ly_content.contains("global = {"));
            assert!(part.ly_content.contains("\\relative c'"));
        }

        // First part should have stanza 1 lyrics
        assert!(parts[0].ly_content.contains("A -- ma -- zing grace"));
        assert!(parts[0].ly_content.contains("\\set stanza = \"1.\""));

        // Second part should have stanza 2 lyrics
        assert!(parts[1].ly_content.contains("Twas grace"));
        assert!(parts[1].ly_content.contains("\\set stanza = \"2.\""));
    }

    #[test]
    fn test_parts_export_stanza_refrain() {
        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let parts = lilypond_parts_from_song(&song, &LilypondSettings::default()).unwrap();

        // 3 stanzas + 3 refrains = 6 parts
        assert_eq!(parts.len(), 6, "Expected 6 parts, got {}", parts.len());

        assert_eq!(parts[0].label, "Stanza 1");
        assert_eq!(parts[1].label, "Refrain");
        assert_eq!(parts[2].label, "Stanza 2");
        assert_eq!(parts[3].label, "Refrain");
        assert_eq!(parts[4].label, "Stanza 3");
        assert_eq!(parts[5].label, "Refrain");

        // Stanza parts should use stanza voice
        assert!(parts[0].ly_content.contains("sopranoVoiceStanza"));
        assert!(parts[0].ly_content.contains("Sei nicht stolz"));

        // Refrain parts should use refrain voice
        assert!(parts[1].ly_content.contains("sopranoVoiceRefrain"));
        assert!(parts[1].ly_content.contains("Denn wer sich"));

        // Refrain parts should NOT have a stanza marker
        assert!(!parts[1].ly_content.contains("\\set stanza"));
    }

    #[test]
    fn test_parts_export_refrain_first() {
        let yml = r#"
version: 0.1
title: Refrain First Song
default_language: en
tags:
  author: Test Author
score:
  key: c major
  time: 4/4
orders:
  - refrain-stanza-refrain
parts:
  - type: refrain
    contents:
    - type: voice
      number: 1
      content: |
        c4 d e f | g2 g2
    - type: lyrics
      number: 1
      content: |
        Refrain lyrics
  - type: stanza
    contents:
    - type: voice
      number: 1
      content: |
        e4 f g a | b2 b2
    - type: lyrics
      number: 1
      content: |
        First verse
"#;
        let song = song_yml::import_from_yml_string(yml).unwrap();
        let parts = lilypond_parts_from_song(&song, &LilypondSettings::default()).unwrap();

        // refrain, stanza1, refrain = 3 parts
        assert_eq!(parts.len(), 3, "Expected 3 parts, got {}", parts.len());
        assert_eq!(parts[0].label, "Refrain");
        assert_eq!(parts[1].label, "Stanza 1");
        assert_eq!(parts[2].label, "Refrain");
    }

    #[test]
    fn test_parts_each_is_standalone() {
        // Each part LY should be independently compilable
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let parts = lilypond_parts_from_song(&song, &LilypondSettings::default()).unwrap();

        for (i, part) in parts.iter().enumerate() {
            // Must have version
            assert!(
                part.ly_content.contains("\\version"),
                "Part {} ({}) missing \\version",
                i,
                part.label
            );
            // Must have global
            assert!(
                part.ly_content.contains("global = {"),
                "Part {} ({}) missing global",
                i,
                part.label
            );
            // Must have exactly one \score
            let score_count = part.ly_content.matches("\\score {").count();
            assert_eq!(
                score_count, 1,
                "Part {} ({}) should have exactly 1 score, got {}",
                i, part.label, score_count
            );
            // Must have key signature
            assert!(
                part.ly_content.contains("\\key f \\major"),
                "Part {} ({}) missing key signature",
                i,
                part.label
            );
        }
    }

    // ===================================================================
    // Font and staff size setting tests
    // ===================================================================

    #[test]
    fn test_font_setting_default_no_font_block() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let ly = lilypond_from_song(&song, &LilypondSettings::default()).unwrap();

        // Default font should not produce a #(define fonts ...) block
        assert!(
            !ly.contains("define fonts"),
            "Default font should not produce a fonts block"
        );
        // Should not have staff size override
        assert!(
            !ly.contains("set-global-staff-size"),
            "Default should not set staff size"
        );
    }

    #[test]
    fn test_font_setting_specific() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            font: FontSetting::Specific {
                family: "Times New Roman".to_string(),
            },
            ..LilypondSettings::default()
        };

        let ly = lilypond_from_song(&song, &settings).unwrap();

        assert!(
            ly.contains("#(define fonts"),
            "Specific font should produce a fonts block"
        );
        assert!(
            ly.contains("Times New Roman"),
            "Font family should appear in output"
        );
    }

    #[test]
    fn test_staff_size_setting() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            staff_size: Some(18.0),
            ..LilypondSettings::default()
        };

        let ly = lilypond_from_song(&song, &settings).unwrap();

        assert!(
            ly.contains("#(set-global-staff-size 18"),
            "Staff size should appear in output"
        );
    }

    #[test]
    fn test_font_and_staff_size_in_sequential() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            font: FontSetting::Specific {
                family: "Helvetica".to_string(),
            },
            staff_size: Some(16.0),
            ..LilypondSettings::default()
        };

        let ly = lilypond_sequential_from_song(&song, &settings).unwrap();

        assert!(
            ly.contains("#(set-global-staff-size 16"),
            "Staff size should appear in sequential output"
        );
        assert!(
            ly.contains("Helvetica"),
            "Font family should appear in sequential output"
        );
    }

    #[test]
    fn test_font_and_staff_size_in_parts() {
        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            font: FontSetting::Specific {
                family: "Garamond".to_string(),
            },
            staff_size: Some(22.0),
            ..LilypondSettings::default()
        };

        let parts = lilypond_parts_from_song(&song, &settings).unwrap();

        for part in &parts {
            assert!(
                part.ly_content.contains("#(set-global-staff-size 22"),
                "Staff size should appear in part output"
            );
            assert!(
                part.ly_content.contains("Garamond"),
                "Font family should appear in part output"
            );
        }
    }

    // ===================================================================
    // Rendering tests (require LilyPond binary)
    // ===================================================================

    /// Try to find the LilyPond binary. Returns None if not available.
    fn find_lilypond_bin() -> Option<std::path::PathBuf> {
        std::process::Command::new("lilypond")
            .arg("--version")
            .output()
            .ok()
            .filter(|o| o.status.success())
            .map(|_| std::path::PathBuf::from("lilypond"))
    }

    #[test]
    fn test_render_paper_score_svg() {
        let lilypond_bin = match find_lilypond_bin() {
            Some(bin) => bin,
            None => {
                eprintln!("Skipping test_render_paper_score_svg: lilypond not found");
                return;
            }
        };

        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let svg = render_paper_score_to_svg(&song, &LilypondSettings::default(), &lilypond_bin)
            .unwrap();

        assert!(!svg.is_empty(), "SVG output should not be empty");
        let svg_str = String::from_utf8_lossy(&svg);
        assert!(
            svg_str.contains("<svg") || svg_str.contains("<?xml"),
            "Output should be valid SVG"
        );
    }

    #[test]
    fn test_render_paper_score_pdf() {
        let lilypond_bin = match find_lilypond_bin() {
            Some(bin) => bin,
            None => {
                eprintln!("Skipping test_render_paper_score_pdf: lilypond not found");
                return;
            }
        };

        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let pdf = render_paper_score_to_pdf(&song, &LilypondSettings::default(), &lilypond_bin)
            .unwrap();

        assert!(!pdf.is_empty(), "PDF output should not be empty");
        // PDF files start with %PDF-
        assert!(
            pdf.starts_with(b"%PDF-"),
            "Output should be a valid PDF file"
        );
    }

    #[test]
    fn test_render_song_parts_svg() {
        let lilypond_bin = match find_lilypond_bin() {
            Some(bin) => bin,
            None => {
                eprintln!("Skipping test_render_song_parts_svg: lilypond not found");
                return;
            }
        };

        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let rendered =
            render_song_parts_to_svg(&song, &LilypondSettings::default(), &lilypond_bin).unwrap();

        assert_eq!(rendered.len(), 3, "Expected 3 rendered parts");

        for part in &rendered {
            assert!(!part.svg.is_empty(), "SVG for {} should not be empty", part.label);
            let svg_str = String::from_utf8_lossy(&part.svg);
            assert!(
                svg_str.contains("<svg") || svg_str.contains("<?xml"),
                "Output for {} should be valid SVG",
                part.label
            );
        }
    }

    #[test]
    fn test_render_paper_score_svg_with_settings() {
        let lilypond_bin = match find_lilypond_bin() {
            Some(bin) => bin,
            None => {
                eprintln!(
                    "Skipping test_render_paper_score_svg_with_settings: lilypond not found"
                );
                return;
            }
        };

        let content = std::fs::read_to_string("tests/data/Amazing Grace.song.yml").unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let settings = LilypondSettings {
            paper_size: "a5".to_string(),
            staff_size: Some(16.0),
            ..LilypondSettings::default()
        };

        let svg = render_paper_score_to_svg(&song, &settings, &lilypond_bin).unwrap();

        assert!(!svg.is_empty(), "SVG output should not be empty");
    }

    #[test]
    fn test_render_stanza_refrain_parts_svg() {
        let lilypond_bin = match find_lilypond_bin() {
            Some(bin) => bin,
            None => {
                eprintln!(
                    "Skipping test_render_stanza_refrain_parts_svg: lilypond not found"
                );
                return;
            }
        };

        let content = std::fs::read_to_string(
            "tests/data/Sei nicht stolz auf das, was du bist.song.yml",
        )
        .unwrap();
        let song = song_yml::import_from_yml_string(&content).unwrap();

        let rendered =
            render_song_parts_to_svg(&song, &LilypondSettings::default(), &lilypond_bin).unwrap();

        // 3 stanzas + 3 refrains = 6 parts
        assert_eq!(rendered.len(), 6, "Expected 6 rendered parts");

        for part in &rendered {
            assert!(!part.svg.is_empty(), "SVG for {} should not be empty", part.label);
        }
    }
}