audex 0.2.0

Audio metadata reading and writing library with flexible I/O and easy wrappers
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
//! ID3v2 data type specifications comprehensive implementation id3
//!
//! This module provides a complete specification system for ID3v2 frame parsing and serialization,
//! supporting all standard ID3v2 data types with proper encoding, validation, and version compatibility.

use super::util::{
    BitPaddedInt, Unsynch, decode_synchsafe_int_checked, encode_synchsafe_int, is_valid_frame_id,
};
use crate::{AudexError, Result};
use flate2::{Compression, read::ZlibDecoder, write::ZlibEncoder};
use std::io::{Read, Write};

/// Parsed ID3v2 frame header (10 bytes for v2.3/v2.4, 6 bytes for v2.2)
///
/// Contains the frame identifier, payload size, flags (compression,
/// encryption, grouping, unsynchronization), and the ID3 version used
/// for parsing.
#[derive(Debug, Clone)]
pub struct FrameHeader {
    /// Four-character frame identifier (e.g. `"TIT2"`, `"APIC"`)
    pub frame_id: String,
    /// Size of the frame payload in bytes (excluding this header)
    pub size: u32,
    /// Frame flags parsed from the 2-byte flags field
    pub flags: FrameFlags,
    /// ID3v2 version as (2, major) — e.g. (2, 4) for v2.4, (2, 3) for v2.3
    pub version: (u8, u8),
    /// Whether the global unsynchronization flag is set on the tag header
    pub global_unsync: bool,
}

impl FrameHeader {
    pub fn new(frame_id: String, size: u32, flags: u16, version: (u8, u8)) -> Self {
        Self {
            frame_id,
            size,
            flags: FrameFlags::from_raw(flags, version),
            version,
            global_unsync: false,
        }
    }

    /// Parse frame header from bytes
    pub fn from_bytes(data: &[u8], version: (u8, u8)) -> Result<Self> {
        if data.len() < 10 {
            return Err(AudexError::InvalidData(
                "Frame header too short".to_string(),
            ));
        }

        let frame_id = String::from_utf8(data[0..4].to_vec())
            .map_err(|_| AudexError::InvalidData("Invalid frame ID".to_string()))?;

        if !is_valid_frame_id(&frame_id) {
            return Err(AudexError::InvalidData(format!(
                "Invalid frame ID: {}",
                frame_id
            )));
        }

        let size = match version {
            (2, 4) => decode_synchsafe_int_checked(&data[4..8])?,
            _ => u32::from_be_bytes([data[4], data[5], data[6], data[7]]),
        };

        // Validate the declared frame size against the remaining tag data
        // (excluding the 10-byte header itself) to prevent oversized allocations.
        let remaining_after_header = u32::try_from(data.len() - 10)
            .map_err(|_| AudexError::InvalidData("Frame data exceeds u32 capacity".to_string()))?;
        if size > remaining_after_header {
            return Err(AudexError::InvalidData(format!(
                "Frame '{}' size {} exceeds remaining tag data ({})",
                frame_id, size, remaining_after_header
            )));
        }

        let flags = u16::from_be_bytes([data[8], data[9]]);

        Ok(Self::new(frame_id, size, flags, version))
    }

    /// Convert header to bytes for writing.
    /// Returns an error if the frame size exceeds the synchsafe encoding limit
    /// for ID3v2.4 tags.
    pub fn to_bytes(&self) -> crate::Result<Vec<u8>> {
        let mut bytes = Vec::with_capacity(10);
        bytes.extend_from_slice(self.frame_id.as_bytes());

        let size_bytes = match self.version {
            (2, 4) => {
                let encoded = super::util::encode_synchsafe_int(self.size)?;
                encoded.to_vec()
            }
            _ => self.size.to_be_bytes().to_vec(),
        };
        bytes.extend_from_slice(&size_bytes);

        let flags_bytes = self.flags.to_raw(self.version).to_be_bytes();
        bytes.extend_from_slice(&flags_bytes);

        Ok(bytes)
    }

    /// Parse frame header for ID3v2.2 (3-character frame ID).
    ///
    /// The `data` slice should contain the remaining tag data starting at
    /// the frame header position. The frame size is validated against the
    /// available data to prevent oversized allocations from malformed tags.
    pub fn from_bytes_v22(data: &[u8]) -> Result<Self> {
        if data.len() < 6 {
            return Err(AudexError::InvalidData(
                "ID3v2.2 frame header too short".to_string(),
            ));
        }

        let frame_id = String::from_utf8(data[0..3].to_vec())
            .map_err(|_| AudexError::InvalidData("Invalid frame ID".to_string()))?;

        if !crate::id3::util::is_valid_frame_id(&frame_id) {
            return Err(AudexError::InvalidData(format!(
                "Invalid frame ID: {}",
                frame_id
            )));
        }

        let size = (data[3] as u32) << 16 | (data[4] as u32) << 8 | (data[5] as u32);

        // Validate the declared frame size against the remaining tag data
        // (excluding the 6-byte header itself). A 3-byte size field can hold
        // up to ~16 MB, which could trigger a large allocation if unchecked.
        let remaining_after_header = u32::try_from(data.len() - 6).map_err(|_| {
            AudexError::InvalidData("ID3v2.2 frame data exceeds u32 capacity".to_string())
        })?;
        if size > remaining_after_header {
            return Err(AudexError::InvalidData(format!(
                "ID3v2.2 frame '{}' size {} exceeds remaining tag data ({})",
                frame_id, size, remaining_after_header
            )));
        }

        Ok(Self {
            frame_id,
            size,
            flags: FrameFlags::new(), // No flags in ID3v2.2
            version: (2, 2),
            global_unsync: false,
        })
    }

    /// Parse frame header for ID3v2.3
    pub fn from_bytes_v23(data: &[u8]) -> Result<Self> {
        Self::from_bytes(data, (2, 3))
    }

    /// Parse frame header for ID3v2.4
    pub fn from_bytes_v24(data: &[u8]) -> Result<Self> {
        Self::from_bytes(data, (2, 4))
    }

    /// Parse frame header for ID3v2.4 with a custom size parser.
    /// Used by determine_bpi to handle iTunes files that use regular ints
    /// instead of synchsafe ints for frame sizes.
    pub fn from_bytes_v24_with_size_parser(
        data: &[u8],
        size_parser: fn(&[u8]) -> Result<u32>,
    ) -> Result<Self> {
        if data.len() < 10 {
            return Err(AudexError::InvalidData(
                "Frame header too short".to_string(),
            ));
        }

        let frame_id = String::from_utf8(data[0..4].to_vec())
            .map_err(|_| AudexError::InvalidData("Invalid frame ID".to_string()))?;

        if !is_valid_frame_id(&frame_id) {
            return Err(AudexError::InvalidData(format!(
                "Invalid frame ID: {}",
                frame_id
            )));
        }

        let size = size_parser(&data[4..8])?;

        // Validate the declared frame size against the remaining tag data
        // (excluding the 10-byte header) to prevent oversized allocations
        let remaining_after_header = u32::try_from(data.len() - 10)
            .map_err(|_| AudexError::InvalidData("Frame data exceeds u32 capacity".to_string()))?;
        if size > remaining_after_header {
            return Err(AudexError::InvalidData(format!(
                "Frame '{}' size {} exceeds remaining tag data ({})",
                frame_id, size, remaining_after_header
            )));
        }

        let flags = u16::from_be_bytes([data[8], data[9]]);

        Ok(Self::new(frame_id, size, flags, (2, 4)))
    }
}

/// Frame flags structure handling both ID3v2.3 and ID3v2.4
#[derive(Debug, Clone)]
/// Parsed frame flags from the 2-byte flags field in ID3v2.3/v2.4 headers
///
/// Flags control how the frame data should be interpreted (compression,
/// unsynchronization) and what should happen when the tag or file is modified
/// (discard on alter). v2.2 frames have no flags.
pub struct FrameFlags {
    /// Frame should be discarded if the tag is altered
    pub alter_tag: bool,
    /// Frame should be discarded if the file (audio) is altered
    pub alter_file: bool,
    /// Frame is read-only and should not be modified
    pub read_only: bool,
    /// Frame belongs to a group (has a group ID byte prefix)
    pub group_id: bool,
    /// Frame data is compressed with zlib
    pub compression: bool,
    /// Frame data is encrypted (not supported by this library)
    pub encryption: bool,
    /// Frame has per-frame unsynchronization applied (v2.4 only)
    pub unsync: bool,
    /// Frame has a 4-byte data length indicator prefix (v2.4 only)
    pub data_length: bool,
}

impl FrameFlags {
    /// Create a new `FrameFlags` with all flags cleared (no special processing).
    pub fn new() -> Self {
        Self {
            alter_tag: false,
            alter_file: false,
            read_only: false,
            group_id: false,
            compression: false,
            encryption: false,
            unsync: false,
            data_length: false,
        }
    }

    pub fn from_raw(flags: u16, version: (u8, u8)) -> Self {
        match version {
            (2, 3) => Self::from_v23(flags),
            (2, 4) => Self::from_v24(flags),
            _ => Self::new(),
        }
    }

    fn from_v23(flags: u16) -> Self {
        Self {
            alter_tag: (flags & 0x8000) != 0,
            alter_file: (flags & 0x4000) != 0,
            read_only: (flags & 0x2000) != 0,
            group_id: (flags & 0x0020) != 0,
            compression: (flags & 0x0080) != 0,
            encryption: (flags & 0x0040) != 0,
            unsync: false,      // Not supported in ID3v2.3
            data_length: false, // Not supported in ID3v2.3
        }
    }

    fn from_v24(flags: u16) -> Self {
        Self {
            alter_tag: (flags & 0x4000) != 0,
            alter_file: (flags & 0x2000) != 0,
            read_only: (flags & 0x1000) != 0,
            group_id: (flags & 0x0040) != 0,
            compression: (flags & 0x0008) != 0,
            encryption: (flags & 0x0004) != 0,
            unsync: (flags & 0x0002) != 0,
            data_length: (flags & 0x0001) != 0,
        }
    }

    pub fn to_raw(&self, version: (u8, u8)) -> u16 {
        match version {
            (2, 3) => self.to_v23(),
            (2, 4) => self.to_v24(),
            _ => 0,
        }
    }

    fn to_v23(&self) -> u16 {
        let mut flags = 0u16;
        if self.alter_tag {
            flags |= 0x8000;
        }
        if self.alter_file {
            flags |= 0x4000;
        }
        if self.read_only {
            flags |= 0x2000;
        }
        if self.group_id {
            flags |= 0x0020;
        }
        if self.compression {
            flags |= 0x0080;
        }
        if self.encryption {
            flags |= 0x0040;
        }
        flags
    }

    fn to_v24(&self) -> u16 {
        let mut flags = 0u16;
        if self.alter_tag {
            flags |= 0x4000;
        }
        if self.alter_file {
            flags |= 0x2000;
        }
        if self.read_only {
            flags |= 0x1000;
        }
        if self.group_id {
            flags |= 0x0040;
        }
        if self.compression {
            flags |= 0x0008;
        }
        if self.encryption {
            flags |= 0x0004;
        }
        if self.unsync {
            flags |= 0x0002;
        }
        if self.data_length {
            flags |= 0x0001;
        }
        flags
    }

    /// Validate flags for specific ID3 version
    pub fn validate(&self, version: (u8, u8)) -> Result<()> {
        if self.encryption {
            return Err(AudexError::UnsupportedFormat(
                "Frame encryption is not supported".to_string(),
            ));
        }

        match version {
            (2, 3) => {
                if self.unsync {
                    return Err(AudexError::InvalidData(
                        "Unsynchronization flag not valid in ID3v2.3".to_string(),
                    ));
                }
                if self.data_length {
                    return Err(AudexError::InvalidData(
                        "Data length flag not valid in ID3v2.3".to_string(),
                    ));
                }
            }
            (2, 4) => {
                // All flags are valid in ID3v2.4
            }
            (2, 2) => {
                // ID3v2.2 has no per-frame flags; silently ignore any that
                // were carried over from a higher-version source frame.
            }
            _ => {
                return Err(AudexError::UnsupportedFormat(format!(
                    "Unsupported ID3 version: {}.{}",
                    version.0, version.1
                )));
            }
        }

        Ok(())
    }
}

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

/// Frame data processing utilities
/// Applies frame-level transformations during read and write operations.
///
/// On read: strips group ID bytes, removes unsynchronization, decompresses
/// zlib data, and validates data length indicators.
/// On write: applies compression, unsynchronization, and adds data length
/// indicators as needed based on the frame flags.
pub struct FrameProcessor;

impl FrameProcessor {
    /// Process frame data based on flags (decompress, remove unsync, etc.)
    pub fn process_read(header: &FrameHeader, mut data: Vec<u8>) -> Result<Vec<u8>> {
        let flags = &header.flags;

        // Validate flags
        flags.validate(header.version)?;

        // Handle group ID - strip the group identifier byte first.
        // Per the ID3v2.4 spec, the group identifier precedes the data
        // length indicator in the frame's additional info area.
        if flags.group_id {
            if data.is_empty() {
                return Err(AudexError::InvalidData("Missing group ID byte".to_string()));
            }
            data = data[1..].to_vec(); // Skip group ID byte
        }

        // Handle data length indicator (ID3v2.4) - follows group ID in the stream.
        // The spec encodes this as a syncsafe integer (7 bits per byte).
        // Save the raw bytes for the compression fallback path.
        let mut datalen_saved: Option<Vec<u8>> = None;
        let mut indicated_length: Option<usize> = None;
        if flags.data_length && header.version == (2, 4) {
            if data.len() < 4 {
                return Err(AudexError::InvalidData(
                    "Missing data length indicator".to_string(),
                ));
            }
            indicated_length =
                Some(super::util::decode_synchsafe_int_checked(&data[0..4])? as usize);
            datalen_saved = Some(data[..4].to_vec());
            data = data[4..].to_vec();
        }

        // Handle unsynchronization - must happen before decompression.
        // Per-frame unsync (flags.unsync) is an ID3v2.4-only feature.
        // Global unsync (header.global_unsync) applies ONLY to v2.3;
        // in v2.4, unsynchronization is handled per-frame via flags.unsync.
        let needs_unsync = if header.global_unsync && header.version.1 == 3 {
            // v2.3: global unsync is the only mechanism (no per-frame flags)
            true
        } else {
            // v2.4: only per-frame unsync flags apply; global flag is ignored
            flags.unsync && header.version == (2, 4)
        };
        if needs_unsync {
            data = Unsynch::decode(&data).map_err(|_| {
                AudexError::InvalidData(format!(
                    "Frame '{}': unsynchronization decode failed on corrupt data",
                    header.frame_id,
                ))
            })?;
        }

        // For v2.3, compressed frames have a 4-byte uncompressed size prepended.
        // Save the declared size so we can validate it after decompression.
        // Also save the raw prefix bytes for use in the decompression fallback,
        // since v2.3 frames lack a data length indicator (that's a v2.4 feature).
        let mut declared_uncompressed_size: Option<u32> = None;
        let mut v23_size_prefix: Option<Vec<u8>> = None;
        if flags.compression && header.version == (2, 3) {
            if data.len() < 4 {
                return Err(AudexError::InvalidData(
                    "Compressed frame too short for size prefix".to_string(),
                ));
            }
            declared_uncompressed_size =
                Some(u32::from_be_bytes([data[0], data[1], data[2], data[3]]));
            v23_size_prefix = Some(data[..4].to_vec());
            data = data[4..].to_vec();
        }

        // Handle compression - after unsync removal (with fallback for BUG H8)
        if flags.compression {
            match Self::decompress_zlib(&data) {
                Ok(decompressed) => data = decompressed,
                Err(_) => {
                    // Fallback: some old taggers didn't write the uncompressed size
                    // correctly, so try prepending the stripped prefix bytes back.
                    // For v2.4, use the data length indicator; for v2.3, use the
                    // 4-byte uncompressed size prefix that was stripped earlier.
                    let fallback_bytes = datalen_saved.as_ref().or(v23_size_prefix.as_ref());
                    if let Some(prefix) = fallback_bytes {
                        let mut retry_data = prefix.clone();
                        retry_data.extend_from_slice(&data);
                        data = Self::decompress_zlib(&retry_data)?;
                    } else {
                        return Err(AudexError::InvalidData(
                            "Failed to decompress frame".to_string(),
                        ));
                    }
                }
            }

            // Validate the decompressed output against the declared size.
            // A mismatch may indicate a zip bomb or corrupt frame data.
            if let Some(declared) = declared_uncompressed_size {
                if data.len() != declared as usize {
                    return Err(AudexError::InvalidData(format!(
                        "Decompressed size ({}) does not match declared uncompressed size ({})",
                        data.len(),
                        declared
                    )));
                }
            }

            // ID3v2.4 spec requires compressed frames to carry a data length
            // indicator so the decompressed size can be validated. When both
            // declared sizes are absent we reject the frame rather than
            // silently accepting unvalidated data.
            if declared_uncompressed_size.is_none() && indicated_length.is_none() {
                return Err(AudexError::InvalidData(format!(
                    "Frame '{}': compressed without a declared size or data length indicator; \
                     decompressed output ({} bytes) cannot be validated against a known size",
                    header.frame_id,
                    data.len()
                )));
            }
        }

        if let Some(indicated_length) = indicated_length {
            if data.len() != indicated_length {
                return Err(AudexError::InvalidData(format!(
                    "Frame '{}' data length ({}) does not match indicated length ({})",
                    header.frame_id,
                    data.len(),
                    indicated_length
                )));
            }
        }

        Ok(data)
    }

    /// Process frame data for writing (compress, add unsync, etc.)
    pub fn process_write(header: &FrameHeader, mut data: Vec<u8>) -> Result<Vec<u8>> {
        let flags = &header.flags;

        // Validate flags
        flags.validate(header.version)?;

        let original_length = data.len();

        // Handle compression - apply first
        if flags.compression {
            data = Self::compress_zlib(&data)?;
        }

        // Handle unsynchronization (ID3v2.4 only) - apply after compression
        if flags.unsync && header.version == (2, 4) {
            data = Unsynch::encode(&data);
        }

        // Handle data length indicator (ID3v2.4) - prepend before group ID.
        // The spec requires this to be a syncsafe integer (7 bits per byte).
        // In the final byte stream, group_id comes first, then data_length,
        // so we prepend data_length first, then group_id in front of it.
        if flags.data_length && header.version == (2, 4) {
            let original_length_u32 = u32::try_from(original_length).map_err(|_| {
                AudexError::InvalidData(format!(
                    "Frame data length {} exceeds u32 maximum",
                    original_length
                ))
            })?;
            let syncsafe = super::util::encode_synchsafe_int(original_length_u32)?;
            let mut new_data = syncsafe.to_vec();
            new_data.extend(data);
            data = new_data;
        }

        // Handle group ID - prepend group ID byte in front of everything.
        // Per the ID3v2.4 spec, the group identifier byte precedes the
        // data length indicator in the frame's additional info area.
        //
        // NOTE: The actual group ID value is stripped during process_read and
        // not preserved in FrameFlags (which only stores a bool indicating
        // presence). Until FrameFlags or FrameHeader is extended to store the
        // original byte, round-tripping will reset the group ID to 0x00.
        // This is a known limitation -- see FrameFlags.group_id.
        if flags.group_id {
            let mut new_data = vec![0x00];
            new_data.extend(data);
            data = new_data;
        }

        Ok(data)
    }

    /// Compress data using zlib
    fn compress_zlib(data: &[u8]) -> Result<Vec<u8>> {
        let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
        encoder
            .write_all(data)
            .map_err(|e| AudexError::InvalidData(format!("Compression failed: {}", e)))?;
        encoder
            .finish()
            .map_err(|e| AudexError::InvalidData(format!("Compression failed: {}", e)))
    }

    /// Decompress data using zlib.
    ///
    /// Caps the decompressed output at 32 MB to prevent zip-bomb payloads from
    /// causing an out-of-memory condition. 32 MB is far beyond any realistic
    /// single-frame payload (cover art, lyrics, etc.) while still blocking
    /// adversarial inputs that expand a few hundred bytes into gigabytes.
    fn decompress_zlib(data: &[u8]) -> Result<Vec<u8>> {
        const MAX_DECOMPRESSED_SIZE: u64 = 32 * 1024 * 1024;

        let decoder = ZlibDecoder::new(data);
        let mut limited = decoder.take(MAX_DECOMPRESSED_SIZE + 1);
        let mut decompressed = Vec::new();
        limited
            .read_to_end(&mut decompressed)
            .map_err(|e| AudexError::InvalidData(format!("Decompression failed: {}", e)))?;

        if decompressed.len() as u64 > MAX_DECOMPRESSED_SIZE {
            return Err(AudexError::InvalidData(format!(
                "Decompressed frame data exceeds {} MB limit",
                MAX_DECOMPRESSED_SIZE / (1024 * 1024)
            )));
        }

        Ok(decompressed)
    }
}

/// Configuration for ID3 frame specification writing
#[derive(Debug, Clone)]
pub struct FrameWriteConfig {
    pub version: (u8, u8),
    pub use_synchsafe_ints: bool,
    pub default_encoding: TextEncoding,
    pub v23_separator: u8,
}

impl Default for FrameWriteConfig {
    fn default() -> Self {
        Self {
            version: (2, 4),
            use_synchsafe_ints: true,
            default_encoding: TextEncoding::Utf8,
            v23_separator: b'/', // ID3v2.3 uses "/" for multi-value text fields
        }
    }
}

/// Frame data container for specification processing
#[derive(Debug, Clone)]
pub struct FrameData {
    pub frame_id: String,
    pub size: u32,
    pub flags: u16,
    pub version: (u8, u8),
}

impl FrameData {
    pub fn new(frame_id: String, size: u32, flags: u16, version: (u8, u8)) -> Self {
        Self {
            frame_id,
            size,
            flags,
            version,
        }
    }

    pub fn is_v23(&self) -> bool {
        self.version.0 == 2 && self.version.1 == 3
    }

    pub fn is_v24(&self) -> bool {
        self.version.0 == 2 && self.version.1 == 4
    }
}

/// Text encoding types supported by ID3v2 tags
///
/// # Version Compatibility
///
/// | Encoding | ID3v2.3 | ID3v2.4 |
/// |----------|---------|---------|
/// | LATIN1   | ✅      | ✅      |
/// | UTF-16   | ✅      | ✅      |
/// | UTF-16BE | ❌      | ✅      |
/// | UTF-8    | ❌      | ✅      |
///
/// When saving as ID3v2.3, incompatible encodings (UTF-8 and UTF-16BE) are
/// automatically converted to UTF-16 to maintain specification compliance.
///
/// # Default Encoding
///
/// All text frames default to UTF-16 with BOM, which provides maximum
/// compatibility across all ID3v2 versions while supporting full Unicode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextEncoding {
    /// ISO-8859-1 (LATIN1) - Single-byte encoding for Western European characters
    ///
    /// Compatible with all ID3v2 versions. Most compact for ASCII text but
    /// limited character set.
    Latin1 = 0,

    /// UTF-16 with BOM (default) - Variable-width Unicode encoding
    ///
    /// The default encoding for all text frames. Compatible with all ID3v2 versions.
    /// Automatically includes byte-order mark (BOM) to indicate endianness.
    #[default]
    Utf16 = 1,

    /// UTF-16 big-endian without BOM - ID3v2.4 only
    ///
    /// Only valid in ID3v2.4 and later. Automatically converted to UTF-16 with BOM
    /// when saving as ID3v2.3.
    Utf16Be = 2,

    /// UTF-8 - ID3v2.4 only
    ///
    /// Variable-width Unicode encoding. Only valid in ID3v2.4 and later.
    /// Automatically converted to UTF-16 with BOM when saving as ID3v2.3.
    /// More space-efficient than UTF-16 for mostly-ASCII text.
    Utf8 = 3,
}

impl TextEncoding {
    pub fn from_byte(byte: u8) -> Result<Self> {
        match byte {
            0 => Ok(TextEncoding::Latin1),
            1 => Ok(TextEncoding::Utf16),
            2 => Ok(TextEncoding::Utf16Be),
            3 => Ok(TextEncoding::Utf8),
            _ => Err(AudexError::InvalidData(format!(
                "Invalid text encoding: {}",
                byte
            ))),
        }
    }

    pub fn to_byte(self) -> u8 {
        self as u8
    }

    /// Get the null terminator for this encoding
    pub fn null_terminator(&self) -> &'static [u8] {
        match self {
            TextEncoding::Latin1 | TextEncoding::Utf8 => b"\x00",
            TextEncoding::Utf16 | TextEncoding::Utf16Be => b"\x00\x00",
        }
    }

    /// Check if this encoding is valid for the given ID3 version
    pub fn is_valid_for_version(&self, version: (u8, u8)) -> bool {
        match self {
            // UTF-8 and UTF-16BE are only valid in ID3v2.4 and later
            TextEncoding::Utf8 | TextEncoding::Utf16Be => version >= (2, 4),
            // LATIN1 and UTF-16 (with BOM) are valid in all versions
            _ => true,
        }
    }

    /// Convert text to bytes using this encoding
    pub fn encode_text(&self, text: &str) -> Result<Vec<u8>> {
        match self {
            TextEncoding::Latin1 => {
                // Check if all characters can be represented in Latin1
                for ch in text.chars() {
                    if ch as u32 > 255 {
                        return Err(AudexError::InvalidData(format!(
                            "Character '{}' cannot be encoded in Latin1",
                            ch
                        )));
                    }
                }
                // Convert each character to its Latin-1 byte value (0-255)
                // NOT the UTF-8 byte representation
                Ok(text.chars().map(|c| c as u8).collect())
            }
            TextEncoding::Utf8 => Ok(text.as_bytes().to_vec()),
            TextEncoding::Utf16 => {
                let mut bytes = vec![0xFF, 0xFE]; // Little-endian BOM
                for ch in text.encode_utf16() {
                    bytes.extend_from_slice(&ch.to_le_bytes());
                }
                Ok(bytes)
            }
            TextEncoding::Utf16Be => {
                let mut bytes = Vec::new();
                for ch in text.encode_utf16() {
                    bytes.extend_from_slice(&ch.to_be_bytes());
                }
                Ok(bytes)
            }
        }
    }

    /// Decode bytes to text using this encoding
    pub fn decode_text(&self, data: &[u8]) -> Result<String> {
        match self {
            TextEncoding::Latin1 => Ok(data.iter().map(|&b| b as char).collect()),
            TextEncoding::Utf8 => String::from_utf8(data.to_vec())
                .map_err(|e| AudexError::InvalidData(format!("Invalid UTF-8: {}", e))),
            TextEncoding::Utf16 => Self::decode_utf16(data, true),
            TextEncoding::Utf16Be => Self::decode_utf16(data, false),
        }
    }

    fn decode_utf16(data: &[u8], detect_bom: bool) -> Result<String> {
        if data.len() < 2 {
            return Ok(String::new());
        }

        let (data, little_endian) = if detect_bom {
            if data.len() >= 2 && data[0] == 0xFF && data[1] == 0xFE {
                (&data[2..], true)
            } else if data.len() >= 2 && data[0] == 0xFE && data[1] == 0xFF {
                (&data[2..], false)
            } else {
                (data, true) // Default to little-endian if no BOM (most real-world tags are LE)
            }
        } else {
            // Even without BOM detection, a UTF-16BE stream may carry a
            // big-endian BOM (0xFE 0xFF). Strip it so the decoded text
            // does not start with U+FEFF (zero-width no-break space).
            if data.len() >= 2 && data[0] == 0xFE && data[1] == 0xFF {
                (&data[2..], false)
            } else {
                (data, false) // Big-endian without BOM
            }
        };

        // UTF-16 requires an even number of bytes. If the data has an odd
        // length, the trailing orphan byte cannot form a valid code unit.
        // Truncate it rather than padding with 0x00 (which would create a
        // phantom character that isn't in the original data).
        let data = if data.len() % 2 != 0 {
            data[..data.len() - 1].to_vec()
        } else {
            data.to_vec()
        };

        let mut utf16_chars = Vec::new();
        for chunk in data.chunks(2) {
            let ch = if little_endian {
                u16::from_le_bytes([chunk[0], chunk[1]])
            } else {
                u16::from_be_bytes([chunk[0], chunk[1]])
            };
            utf16_chars.push(ch);
        }

        String::from_utf16(&utf16_chars)
            .map_err(|e| AudexError::InvalidData(format!("Invalid UTF-16: {}", e)))
    }
}

impl TryFrom<u8> for TextEncoding {
    type Error = AudexError;

    fn try_from(value: u8) -> std::result::Result<Self, Self::Error> {
        Self::from_byte(value)
    }
}

/// ID3v2.4 timestamp format
#[derive(Debug, Clone, PartialEq)]
pub struct ID3TimeStamp {
    pub text: String,
    pub year: Option<u16>,
    pub month: Option<u8>,
    pub day: Option<u8>,
    pub hour: Option<u8>,
    pub minute: Option<u8>,
    pub second: Option<u8>,
}

impl ID3TimeStamp {
    pub fn new(text: String) -> Self {
        Self {
            text,
            year: None,
            month: None,
            day: None,
            hour: None,
            minute: None,
            second: None,
        }
    }

    pub fn parse(text: &str) -> Self {
        // ID3v2.4 timestamp format: YYYY[-MM[-DD[THH[:MM[:SS]]]]]
        let mut timestamp = Self::new(text.to_string());

        // Timestamp fields use fixed byte offsets; reject non-ASCII input
        // to avoid panicking on multi-byte character boundaries.
        if !text.is_ascii() {
            return timestamp;
        }

        if text.len() >= 4 {
            if let Ok(year) = text[0..4].parse() {
                timestamp.year = Some(year);
            }
        }

        if text.len() >= 7 && text.as_bytes()[4] == b'-' {
            if let Ok(month) = text[5..7].parse::<u8>() {
                // Month must be in the range 1-12
                if (1..=12).contains(&month) {
                    timestamp.month = Some(month);
                }
            }
        }

        if text.len() >= 10 && text.as_bytes()[7] == b'-' {
            if let Ok(day) = text[8..10].parse::<u8>() {
                // Day must be in the range 1-31
                if (1..=31).contains(&day) {
                    timestamp.day = Some(day);
                }
            }
        }

        if text.len() >= 13 && text.as_bytes()[10] == b'T' {
            if let Ok(hour) = text[11..13].parse::<u8>() {
                // Hour must be in the range 0-23
                if hour <= 23 {
                    timestamp.hour = Some(hour);
                }
            }
        }

        if text.len() >= 16 && text.as_bytes()[13] == b':' {
            if let Ok(minute) = text[14..16].parse::<u8>() {
                // Minute must be in the range 0-59
                if minute <= 59 {
                    timestamp.minute = Some(minute);
                }
            }
        }

        if text.len() >= 19 && text.as_bytes()[16] == b':' {
            if let Ok(second) = text[17..19].parse::<u8>() {
                // Second must be in the range 0-59
                if second <= 59 {
                    timestamp.second = Some(second);
                }
            }
        }

        timestamp
    }
}

/// Base trait for all ID3 specifications
///
/// This trait defines the core interface that all specification types must implement
/// for reading, writing, and validating ID3 frame data.
pub trait Spec {
    type Value: Clone;

    /// Read data from bytes using this specification
    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)>;

    /// Write value to bytes using this specification
    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>>;

    /// Validate value for this specification
    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value>;

    /// Validate value for ID3v2.3 compatibility
    fn validate23(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // Default implementation just calls validate
        self.validate(frame, value)
    }

    /// Get the name of this specification (for debugging)
    fn name(&self) -> &'static str {
        std::any::type_name::<Self>()
    }
}

/// Error type for specification-related errors
#[derive(Debug, Clone)]
pub struct SpecError {
    pub message: String,
    pub spec_name: String,
}

impl SpecError {
    pub fn new(spec_name: &str, message: String) -> Self {
        Self {
            message,
            spec_name: spec_name.to_string(),
        }
    }
}

impl std::fmt::Display for SpecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Spec error in {}: {}", self.spec_name, self.message)
    }
}

impl std::error::Error for SpecError {}

/// Convert SpecError to AudexError
impl From<SpecError> for AudexError {
    fn from(err: SpecError) -> Self {
        AudexError::InvalidData(err.to_string())
    }
}

/// Single byte specification
#[derive(Debug, Clone)]
pub struct ByteSpec {
    pub name: String,
    pub default: u8,
}

impl ByteSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            default: 0,
        }
    }

    pub fn with_default(name: &str, default: u8) -> Self {
        Self {
            name: name.to_string(),
            default,
        }
    }
}

impl Spec for ByteSpec {
    type Value = u8;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Ok((self.default, 0));
        }
        Ok((data[0], 1))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(vec![*value])
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Text encoding specification
#[derive(Debug, Clone)]
pub struct EncodingSpec {
    pub name: String,
}

impl EncodingSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for EncodingSpec {
    type Value = TextEncoding;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Ok((TextEncoding::default(), 0));
        }
        let encoding = TextEncoding::from_byte(data[0])?;
        Ok((encoding, 1))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(vec![value.to_byte()])
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }

    fn validate23(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // ID3v2.3 only supports LATIN1 (0) and UTF-16 with BOM (1)
        // UTF-8 (3) and UTF-16BE (2) were added in ID3v2.4
        match value {
            TextEncoding::Utf8 | TextEncoding::Utf16Be => Ok(TextEncoding::Utf16),
            _ => Ok(value),
        }
    }
}

/// Fixed-size ASCII string specification
#[derive(Debug, Clone)]
pub struct StringSpec {
    pub name: String,
    pub size: usize,
}

impl StringSpec {
    pub fn new(name: &str, size: usize) -> Self {
        Self {
            name: name.to_string(),
            size,
        }
    }
}

impl Spec for StringSpec {
    type Value = String;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let bytes_to_read = std::cmp::min(self.size, data.len());
        let text_data = &data[..bytes_to_read];

        // Find null terminator or use all bytes
        let end = text_data
            .iter()
            .position(|&b| b == 0)
            .unwrap_or(text_data.len());

        // Validate UTF-8 properly - should fail on invalid UTF-8
        let text = String::from_utf8(text_data[..end].to_vec())
            .map_err(|_| SpecError::new("StringSpec", "Invalid UTF-8 data".to_string()))?;
        Ok((text, self.size))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let bytes = value.as_bytes();
        let copy_len = std::cmp::min(bytes.len(), self.size);

        if bytes.len() >= self.size {
            // If string is longer than or equal to size, truncate to exact size
            Ok(bytes[..copy_len].to_vec())
        } else {
            // If string is shorter, pad with null bytes to exact size
            let mut data = vec![0u8; self.size];
            data[..copy_len].copy_from_slice(&bytes[..copy_len]);
            Ok(data)
        }
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // Validate exact length requirement
        if value.len() != self.size {
            return Err(SpecError::new(
                "StringSpec",
                format!(
                    "String length {} does not match required size {}",
                    value.len(),
                    self.size
                ),
            )
            .into());
        }

        // Validate ASCII-only characters
        if !value.is_ascii() {
            return Err(SpecError::new(
                "StringSpec",
                format!("Non-ASCII characters in string: {}", value),
            )
            .into());
        }

        Ok(value)
    }
}

/// Frame ID specification extending StringSpec with validation
#[derive(Debug, Clone)]
pub struct FrameIDSpec {
    pub name: String,
    pub length: usize,
}

impl FrameIDSpec {
    pub fn new(name: &str, length: usize) -> Self {
        Self {
            name: name.to_string(),
            length,
        }
    }
}

impl Spec for FrameIDSpec {
    type Value = String;

    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        // Use StringSpec's read behavior
        let string_spec = StringSpec::new(&self.name, self.length);
        string_spec.read(header, frame, data)
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        // Use StringSpec's write behavior
        let string_spec = StringSpec::new(&self.name, self.length);
        string_spec.write(config, frame, value)
    }

    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // First validate using StringSpec
        let string_spec = StringSpec::new(&self.name, self.length);
        let validated_value = string_spec.validate(frame, value)?;

        // Then validate frame ID specifically
        if !is_valid_frame_id(&validated_value) {
            return Err(SpecError::new("FrameIDSpec", "Invalid frame ID".to_string()).into());
        }

        Ok(validated_value)
    }
}

/// Raw binary data specification
#[derive(Debug, Clone)]
pub struct BinaryDataSpec {
    pub name: String,
}

impl BinaryDataSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for BinaryDataSpec {
    type Value = Vec<u8>;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        Ok((data.to_vec(), data.len()))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(value.clone())
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Encoding-aware null terminator specification
#[derive(Debug, Clone)]
pub struct TerminatorSpec {
    pub name: String,
    pub encoding: TextEncoding,
}

impl TerminatorSpec {
    pub fn new(name: &str, encoding: TextEncoding) -> Self {
        Self {
            name: name.to_string(),
            encoding,
        }
    }
}

impl Spec for TerminatorSpec {
    type Value = Vec<u8>;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let terminator = self.encoding.null_terminator();
        if data.len() >= terminator.len() && &data[..terminator.len()] == terminator {
            Ok((terminator.to_vec(), terminator.len()))
        } else {
            Ok((vec![], 0))
        }
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        _value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(self.encoding.null_terminator().to_vec())
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Encoded text specification with encoding-dependent serialization
#[derive(Debug, Clone)]
pub struct EncodedTextSpec {
    pub name: String,
}

impl EncodedTextSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for EncodedTextSpec {
    type Value = String;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Ok((String::new(), 0));
        }

        // First byte is encoding
        let encoding = TextEncoding::from_byte(data[0])?;
        let text_data = &data[1..];

        // Find null terminator based on encoding.
        // For UTF-16 encodings, the search must step by 2 bytes to stay
        // aligned with character boundaries. A byte-level search can
        // produce false matches when adjacent characters happen to have
        // a 0x00 byte at their boundary.
        let terminator = encoding.null_terminator();
        let end = if terminator.len() == 2 {
            let mut pos = 0;
            let mut found = None;
            while pos + 1 < text_data.len() {
                if text_data[pos] == 0 && text_data[pos + 1] == 0 {
                    found = Some(pos);
                    break;
                }
                pos += 2;
            }
            found.unwrap_or(text_data.len())
        } else {
            text_data
                .windows(terminator.len())
                .position(|window| window == terminator)
                .unwrap_or(text_data.len())
        };

        let text = encoding.decode_text(&text_data[..end])?;

        // Calculate consumed bytes: encoding byte + text bytes + null terminator (if present)
        let consumed = if end < text_data.len() {
            // Null terminator was found
            1 + end + encoding.null_terminator().len()
        } else {
            // No null terminator, consume all data
            data.len()
        };

        Ok((text, consumed))
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let encoding = config.default_encoding;
        let mut result = vec![encoding.to_byte()];
        result.extend_from_slice(&encoding.encode_text(value)?);
        result.extend_from_slice(encoding.null_terminator());
        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Encoded numeric text specification for numeric text fields
#[derive(Debug, Clone)]
pub struct EncodedNumericTextSpec {
    pub name: String,
}

impl EncodedNumericTextSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for EncodedNumericTextSpec {
    type Value = String;

    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.read(header, frame, data)
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.write(config, frame, value)
    }

    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.validate(frame, value)
    }
}

/// Encoded numeric part text specification for track numbers and similar fields
#[derive(Debug, Clone)]
pub struct EncodedNumericPartTextSpec {
    pub name: String,
}

impl EncodedNumericPartTextSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for EncodedNumericPartTextSpec {
    type Value = String;

    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.read(header, frame, data)
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.write(config, frame, value)
    }

    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        let encoded_text_spec = EncodedTextSpec::new(&self.name);
        encoded_text_spec.validate(frame, value)
    }
}

/// Fixed Latin-1 text specification
#[derive(Debug, Clone)]
pub struct Latin1TextSpec {
    pub name: String,
}

impl Latin1TextSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for Latin1TextSpec {
    type Value = String;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        // Find null terminator
        let end = data.iter().position(|&b| b == 0).unwrap_or(data.len());
        let text = TextEncoding::Latin1.decode_text(&data[..end])?;
        let consumed = if end < data.len() { end + 1 } else { end };
        Ok((text, consumed))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let mut result = TextEncoding::Latin1.encode_text(value)?;
        result.push(0); // Null terminator
        Ok(result)
    }

    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // Validate that all characters can be encoded in Latin-1
        for ch in value.chars() {
            if ch as u32 > 255 {
                return Err(SpecError::new(
                    "Latin1TextSpec",
                    format!("Character '{}' cannot be encoded in Latin-1", ch),
                )
                .into());
            }
        }

        // Special validation for MIME types in APIC frames
        if frame.frame_id == "APIC"
            && self.name == "mime_type"
            && (!value.contains('/') || value.is_empty())
        {
            return Err(SpecError::new(
                "Latin1TextSpec",
                "MIME type must be in format 'type/subtype'".to_string(),
            )
            .into());
        }

        Ok(value)
    }
}

/// List of Latin-1 text strings with count prefix
#[derive(Debug, Clone)]
pub struct Latin1TextListSpec {
    pub name: String,
}

impl Latin1TextListSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for Latin1TextListSpec {
    type Value = Vec<String>;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Ok((vec![], 0));
        }

        let count = data[0] as usize;
        let mut result = Vec::with_capacity(count);
        let mut pos = 1;

        for _ in 0..count {
            if pos >= data.len() {
                break;
            }

            let remaining = &data[pos..];
            let end = remaining
                .iter()
                .position(|&b| b == 0)
                .unwrap_or(remaining.len());

            // Always add the text, even if empty
            let text = TextEncoding::Latin1.decode_text(&remaining[..end])?;
            result.push(text);

            pos += end + 1; // +1 for null terminator
        }

        Ok((result, pos))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        // The item count is stored as a single byte, so reject lists that
        // exceed the maximum representable value rather than silently wrapping.
        let count = u8::try_from(value.len()).map_err(|_| {
            SpecError::new(
                "Latin1TextListSpec",
                "Too many text entries (max 255)".to_string(),
            )
        })?;
        let mut result = vec![count];

        for text in value {
            result.extend_from_slice(&TextEncoding::Latin1.encode_text(text)?);
            result.push(0); // Null terminator
        }

        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // Validate count fits in u8
        if value.len() > 255 {
            return Err(SpecError::new(
                "Latin1TextListSpec",
                "Too many text entries (max 255)".to_string(),
            )
            .into());
        }

        // Validate each string can be encoded in Latin-1
        for text in &value {
            for ch in text.chars() {
                if ch as u32 > 255 {
                    return Err(SpecError::new(
                        "Latin1TextListSpec",
                        format!("Character '{}' cannot be encoded in Latin-1", ch),
                    )
                    .into());
                }
            }
        }

        Ok(value)
    }
}

/// Multiple specification (for arrays/lists)
#[derive(Debug, Clone)]
pub struct MultiSpec<T: Spec + Clone> {
    pub name: String,
    pub spec: T,
    pub separator: Vec<u8>,
    pub optional: bool,
}

impl<T: Spec + Clone> MultiSpec<T> {
    pub fn new(name: &str, spec: T) -> Self {
        Self {
            name: name.to_string(),
            spec,
            separator: vec![0],
            optional: false,
        }
    }

    pub fn with_separator(name: &str, spec: T, separator: Vec<u8>) -> Self {
        Self {
            name: name.to_string(),
            spec,
            separator,
            optional: false,
        }
    }

    pub fn optional(mut self) -> Self {
        self.optional = true;
        self
    }
}

impl<T: Spec + Clone> Spec for MultiSpec<T> {
    type Value = Vec<T::Value>;

    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let mut result = Vec::new();
        let mut pos = 0;

        while pos < data.len() {
            // Try to read one item
            let remaining = &data[pos..];
            match self.spec.read(header, frame, remaining) {
                Ok((value, consumed)) => {
                    if consumed == 0 {
                        break; // Avoid infinite loop
                    }
                    result.push(value);
                    pos += consumed;

                    // Skip separator if present
                    if pos < data.len()
                        && data.len() - pos >= self.separator.len()
                        && data[pos..pos + self.separator.len()] == *self.separator
                    {
                        pos += self.separator.len();
                    }
                }
                Err(_) => {
                    if result.is_empty() && !self.optional {
                        return Err(SpecError::new(
                            "MultiSpec",
                            "Failed to read required items".to_string(),
                        )
                        .into());
                    }
                    break;
                }
            }
        }

        Ok((result, pos))
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let mut result = Vec::new();

        for (i, item) in value.iter().enumerate() {
            if i > 0 {
                result.extend_from_slice(&self.separator);
            }
            result.extend_from_slice(&self.spec.write(config, frame, item)?);
        }

        Ok(result)
    }

    fn validate(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        let mut validated = Vec::new();

        for item in value {
            validated.push(self.spec.validate(frame, item)?);
        }

        Ok(validated)
    }

    fn validate23(&self, frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        let mut validated = Vec::new();

        for item in value {
            validated.push(self.spec.validate23(frame, item)?);
        }

        Ok(validated)
    }
}

/// Sized integer specification (fixed number of bytes)
#[derive(Debug, Clone)]
pub struct SizedIntegerSpec {
    pub name: String,
    pub size: usize,
    pub signed: bool,
}

impl SizedIntegerSpec {
    pub fn new(name: &str, size: usize) -> Self {
        Self {
            name: name.to_string(),
            size,
            signed: false,
        }
    }

    pub fn signed(mut self) -> Self {
        self.signed = true;
        self
    }
}

impl Spec for SizedIntegerSpec {
    type Value = i64;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.len() < self.size {
            return Err(SpecError::new(
                "SizedIntegerSpec",
                format!("Not enough bytes for {}-byte integer", self.size),
            )
            .into());
        }

        let bytes = &data[..self.size];
        let value = match self.size {
            1 => {
                if self.signed {
                    bytes[0] as i8 as i64
                } else {
                    bytes[0] as i64
                }
            }
            2 => {
                let val = u16::from_be_bytes([bytes[0], bytes[1]]);
                if self.signed {
                    val as i16 as i64
                } else {
                    val as i64
                }
            }
            3 => {
                let val = u32::from_be_bytes([0, bytes[0], bytes[1], bytes[2]]);
                if self.signed && bytes[0] & 0x80 != 0 {
                    // Sign extend
                    (val | 0xFF000000) as i32 as i64
                } else {
                    val as i64
                }
            }
            4 => {
                let val = u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
                if self.signed {
                    val as i32 as i64
                } else {
                    val as i64
                }
            }
            8 => {
                let val = u64::from_be_bytes([
                    bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
                ]);
                // Both signed and unsigned 8-byte values are cast to i64 the same way
                val as i64
            }
            _ => {
                return Err(SpecError::new(
                    "SizedIntegerSpec",
                    format!("Unsupported integer size: {}", self.size),
                )
                .into());
            }
        };

        Ok((value, self.size))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let bytes = match self.size {
            1 => {
                vec![*value as u8]
            }
            2 => (*value as u16).to_be_bytes().to_vec(),
            3 => {
                let val = *value as u32;
                vec![(val >> 16) as u8, (val >> 8) as u8, val as u8]
            }
            4 => (*value as u32).to_be_bytes().to_vec(),
            8 => (*value as u64).to_be_bytes().to_vec(),
            _ => {
                return Err(SpecError::new(
                    "SizedIntegerSpec",
                    format!("Unsupported integer size: {}", self.size),
                )
                .into());
            }
        };

        Ok(bytes)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        // Validate value fits in the specified size
        let (min_val, max_val) = match (self.size, self.signed) {
            (1, false) => (0, 255),
            (1, true) => (-128, 127),
            (2, false) => (0, 65535),
            (2, true) => (-32768, 32767),
            (3, false) => (0, 16777215),
            (3, true) => (-8388608, 8388607),
            (4, false) => (0, 4294967295),
            (4, true) => (-2147483648, 2147483647),
            (8, _) => (i64::MIN, i64::MAX),
            _ => return Ok(value),
        };

        if value < min_val || value > max_val {
            return Err(SpecError::new(
                "SizedIntegerSpec",
                format!(
                    "Value {} out of range for {}-byte {} integer",
                    value,
                    self.size,
                    if self.signed { "signed" } else { "unsigned" }
                ),
            )
            .into());
        }

        Ok(value)
    }
}

/// Integer specification for variable-width bit-padded integers
#[derive(Debug, Clone)]
pub struct IntegerSpec {
    pub name: String,
}

impl IntegerSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for IntegerSpec {
    type Value = u32;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Err(SpecError::new("IntegerSpec", "No data to read".to_string()).into());
        }

        // Use all data with variable width (width=-1 in specification)
        let bit_padded_int = BitPaddedInt::from_bytes(data, 7, true)?;
        let value: u32 = bit_padded_int.value();

        Ok((value, data.len()))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        // Use variable width (width=-1 in standard equivalent)
        BitPaddedInt::to_str(*value, None, Some(true), Some(-1), None)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Variable-length integer specification (supports synchsafe integers)
#[derive(Debug, Clone)]
pub struct VarLengthSpec {
    pub name: String,
    pub synchsafe: bool,
}

impl VarLengthSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
            synchsafe: false,
        }
    }

    pub fn synchsafe(mut self) -> Self {
        self.synchsafe = true;
        self
    }
}

impl Spec for VarLengthSpec {
    type Value = u32;

    fn read(
        &self,
        _header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.len() < 4 {
            return Err(SpecError::new(
                "VarLengthSpec",
                "Not enough bytes for variable-length integer".to_string(),
            )
            .into());
        }

        let bytes = &data[..4];
        let value = if self.synchsafe || frame.is_v24() {
            decode_synchsafe_int_checked(bytes)?
        } else {
            u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
        };

        Ok((value, 4))
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let bytes = if self.synchsafe || (config.use_synchsafe_ints && frame.is_v24()) {
            encode_synchsafe_int(*value)?
        } else {
            value.to_be_bytes()
        };

        Ok(bytes.to_vec())
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        if self.synchsafe && value > 0x0FFFFFFF {
            return Err(SpecError::new(
                "VarLengthSpec",
                "Value too large for synchsafe integer".to_string(),
            )
            .into());
        }
        Ok(value)
    }
}

/// ID3v2 header structure (10 bytes)
#[derive(Debug, Clone)]
pub struct ID3Header {
    pub major_version: u8,
    pub revision: u8,
    pub flags: u8,
    pub size: u32,
}

impl ID3Header {
    /// Create new ID3Header for testing
    pub fn new(major: u8, minor: u8, flags: u8, size: u32) -> Self {
        Self {
            major_version: major,
            revision: minor,
            flags,
            size,
        }
    }

    /// Parse ID3v2 header from bytes
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        if data.len() < 10 {
            return Err(AudexError::InvalidData("ID3 header too short".to_string()));
        }

        if &data[0..3] != b"ID3" {
            return Err(AudexError::InvalidData(
                "Invalid ID3 header signature".to_string(),
            ));
        }

        let major_version = data[3];
        let revision = data[4];
        let flags = data[5];

        // Only ID3v2.2, v2.3, and v2.4 are defined in the specification.
        // Reject all other major versions to avoid misinterpreting unknown formats.
        if ![2, 3, 4].contains(&major_version) {
            return Err(AudexError::InvalidData(format!(
                "Unsupported ID3v2 major version: {}",
                major_version
            )));
        }

        // Validate ID3v2 revision - for major version 2, only revisions 2, 3, and 4 are supported
        if major_version == 2 && revision > 4 {
            return Err(AudexError::InvalidData(format!(
                "Unsupported ID3v2 revision: v{}.{}",
                major_version, revision
            )));
        }

        // Validate that header size bytes are valid synchsafe (bit 7 must be 0 in each byte)
        if data[6..10].iter().any(|&b| b & 0x80 != 0) {
            return Err(AudexError::InvalidData(
                "Header size not synchsafe".to_string(),
            ));
        }

        // Size is stored as synchsafe integer
        let size = decode_synchsafe_int_checked(&data[6..10])?;

        Ok(Self {
            major_version,
            revision,
            flags,
            size,
        })
    }

    /// Convert header to bytes.
    /// Returns an error if the tag size exceeds the synchsafe encoding limit.
    pub fn to_bytes(&self) -> crate::Result<[u8; 10]> {
        let mut header = [0u8; 10];
        header[0..3].copy_from_slice(b"ID3");
        header[3] = self.major_version;
        header[4] = self.revision;
        header[5] = self.flags;

        let size_bytes = encode_synchsafe_int(self.size)?;
        header[6..10].copy_from_slice(&size_bytes);

        Ok(header)
    }

    /// Check if unsynchronization flag is set
    pub fn has_unsynchronization(&self) -> bool {
        self.flags & 0x80 != 0
    }

    /// Check if extended header flag is set
    pub fn has_extended_header(&self) -> bool {
        self.flags & 0x40 != 0
    }

    /// Check if experimental flag is set
    pub fn is_experimental(&self) -> bool {
        self.flags & 0x20 != 0
    }

    /// Check if footer is present
    pub fn has_footer(&self) -> bool {
        self.major_version >= 4 && (self.flags & 0x10 != 0)
    }

    /// Get version as tuple
    pub fn version(&self) -> (u8, u8) {
        (self.major_version, self.revision)
    }

    pub fn size(&self) -> u32 {
        self.size
    }

    pub fn flags(&self) -> u8 {
        self.flags
    }
}

/// Picture type enumeration for APIC frames
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
#[derive(Default)]
pub enum PictureType {
    Other = 0,
    FileIcon = 1, // 32x32 pixels 'file icon' (PNG only)
    OtherFileIcon = 2,
    #[default]
    CoverFront = 3,
    CoverBack = 4,
    LeafletPage = 5,
    Media = 6, // e.g. label side of CD
    LeadArtist = 7,
    Artist = 8,
    Conductor = 9,
    Band = 10,
    Composer = 11,
    Lyricist = 12,
    RecordingLocation = 13,
    DuringRecording = 14,
    DuringPerformance = 15,
    ScreenCapture = 16,
    Fish = 17, // A bright coloured fish
    Illustration = 18,
    BandLogotype = 19,
    PublisherLogotype = 20,
}

impl PictureType {
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0 => Some(Self::Other),
            1 => Some(Self::FileIcon),
            2 => Some(Self::OtherFileIcon),
            3 => Some(Self::CoverFront),
            4 => Some(Self::CoverBack),
            5 => Some(Self::LeafletPage),
            6 => Some(Self::Media),
            7 => Some(Self::LeadArtist),
            8 => Some(Self::Artist),
            9 => Some(Self::Conductor),
            10 => Some(Self::Band),
            11 => Some(Self::Composer),
            12 => Some(Self::Lyricist),
            13 => Some(Self::RecordingLocation),
            14 => Some(Self::DuringRecording),
            15 => Some(Self::DuringPerformance),
            16 => Some(Self::ScreenCapture),
            17 => Some(Self::Fish),
            18 => Some(Self::Illustration),
            19 => Some(Self::BandLogotype),
            20 => Some(Self::PublisherLogotype),
            _ => None,
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            Self::Other => "Other",
            Self::FileIcon => "32x32 pixels file icon",
            Self::OtherFileIcon => "Other file icon",
            Self::CoverFront => "Cover (front)",
            Self::CoverBack => "Cover (back)",
            Self::LeafletPage => "Leaflet page",
            Self::Media => "Media",
            Self::LeadArtist => "Lead artist/lead performer/soloist",
            Self::Artist => "Artist/performer",
            Self::Conductor => "Conductor",
            Self::Band => "Band/Orchestra",
            Self::Composer => "Composer",
            Self::Lyricist => "Lyricist/text writer",
            Self::RecordingLocation => "Recording Location",
            Self::DuringRecording => "During recording",
            Self::DuringPerformance => "During performance",
            Self::ScreenCapture => "Movie/video screen capture",
            Self::Fish => "A bright coloured fish",
            Self::Illustration => "Illustration",
            Self::BandLogotype => "Band/artist logotype",
            Self::PublisherLogotype => "Publisher/Studio logotype",
        }
    }
}

/// CTOC flags for table of contents frames
#[derive(Debug, Clone, Copy, Default)]
pub struct CTOCFlags(u8);

impl CTOCFlags {
    pub const TOP_LEVEL: u8 = 0x02; // Identifies the CTOC root frame
    pub const ORDERED: u8 = 0x01; // Child elements are ordered

    pub fn new(flags: u8) -> Self {
        Self(flags)
    }

    pub fn is_top_level(&self) -> bool {
        self.0 & Self::TOP_LEVEL != 0
    }

    pub fn is_ordered(&self) -> bool {
        self.0 & Self::ORDERED != 0
    }

    pub fn value(&self) -> u8 {
        self.0
    }
}

/// Picture type specification for APIC frames
#[derive(Debug, Clone)]
pub struct PictureTypeSpec {
    pub name: String,
}

impl PictureTypeSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for PictureTypeSpec {
    type Value = PictureType;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Err(
                SpecError::new("PictureTypeSpec", "No data for picture type".to_string()).into(),
            );
        }

        let picture_type = PictureType::from_u8(data[0]).unwrap_or(PictureType::Other);

        Ok((picture_type, 1))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(vec![*value as u8])
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// CTOC flags specification
#[derive(Debug, Clone)]
pub struct CTOCFlagsSpec {
    pub name: String,
}

impl CTOCFlagsSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for CTOCFlagsSpec {
    type Value = CTOCFlags;

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Err(
                SpecError::new("CTOCFlagsSpec", "No data for CTOC flags".to_string()).into(),
            );
        }

        Ok((CTOCFlags::new(data[0]), 1))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        Ok(vec![value.value()])
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// TimeStamp specification for TDRC and similar frames
#[derive(Debug, Clone)]
pub struct TimeStampSpec {
    pub name: String,
}

impl TimeStampSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for TimeStampSpec {
    type Value = ID3TimeStamp;

    fn read(
        &self,
        header: &FrameHeader,
        frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let text_spec = EncodedTextSpec::new("timestamp_text");
        let (text, consumed) = text_spec.read(header, frame, data)?;

        let timestamp = ID3TimeStamp::parse(&text);
        Ok((timestamp, consumed))
    }

    fn write(
        &self,
        config: &FrameWriteConfig,
        frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let text_spec = EncodedTextSpec::new("timestamp_text");
        // Convert space to T for ISO format when writing
        let iso_text = value.text.replace(' ', "T");
        text_spec.write(config, frame, &iso_text)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Volume adjustment specification (RVA2 frame)
#[derive(Debug, Clone)]
pub struct VolumeAdjustmentSpec {
    pub name: String,
}

impl VolumeAdjustmentSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for VolumeAdjustmentSpec {
    type Value = f32; // Volume adjustment in dB

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.len() < 2 {
            return Err(SpecError::new(
                "VolumeAdjustmentSpec",
                "Not enough data for volume adjustment".to_string(),
            )
            .into());
        }

        let raw_value = i16::from_be_bytes([data[0], data[1]]);
        let volume_db = raw_value as f32 / 512.0;

        Ok((volume_db, 2))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        // Enforce the same range used by validate() so that extreme values
        // are rejected rather than silently saturated during the f32→i16 cast.
        if !(-64.0..=64.0).contains(value) {
            return Err(SpecError::new(
                "VolumeAdjustmentSpec",
                "Volume adjustment out of valid range (-64 to +64 dB)".to_string(),
            )
            .into());
        }

        let raw_value = (value * 512.0).round() as i16;

        Ok(raw_value.to_be_bytes().to_vec())
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        if !(-64.0..=64.0).contains(&value) {
            return Err(SpecError::new(
                "VolumeAdjustmentSpec",
                "Volume adjustment out of valid range (-64 to +64 dB)".to_string(),
            )
            .into());
        }
        Ok(value)
    }
}

/// Volume peak specification (RVA2 frame)
#[derive(Debug, Clone)]
pub struct VolumePeakSpec {
    pub name: String,
}

impl VolumePeakSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for VolumePeakSpec {
    type Value = f32; // Peak level (0.0 to 1.0)

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.is_empty() {
            return Err(
                SpecError::new("VolumePeakSpec", "No data for volume peak".to_string()).into(),
            );
        }

        let bits = data[0];

        // A peak with 0 bits has no meaningful value
        if bits == 0 {
            return Ok((0.0, 1));
        }

        let vol_bytes = bits.div_ceil(8).min(4) as usize;

        if data.len() < 1 + vol_bytes {
            return Err(
                SpecError::new("VolumePeakSpec", "Not enough frame data".to_string()).into(),
            );
        }

        let shift = ((8 - (bits & 7)) & 7) + ((4 - vol_bytes) * 8) as u8;

        // Validate shift is within the valid range for a u32 left-shift.
        // The arithmetic above should always produce 0..=31, but we guard
        // against unexpected inputs to avoid undefined-behaviour-class panics.
        debug_assert!(shift <= 31, "shift out of range for u32: {shift}");

        let mut peak = 0u32;

        for &byte in data.iter().take(vol_bytes + 1).skip(1) {
            peak = (peak << 8) | byte as u32;
        }

        // Use checked_shl so an out-of-range shift saturates to zero
        // instead of panicking in debug or wrapping in release builds.
        peak = peak.checked_shl(shift.min(31) as u32).unwrap_or(0);
        let peak_float = peak as f32 / (2_u32.pow(31) - 1) as f32;

        Ok((peak_float, 1 + vol_bytes))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        // Validate range before casting to avoid silent truncation.
        if *value < 0.0 || *value > 1.0 {
            return Err(
                SpecError::new("VolumePeakSpec", "Peak volume out of range".to_string()).into(),
            );
        }

        let raw_value = (value * 32768.0).round() as u16;

        // Always write as 16 bits for consistency
        let mut result = vec![0x10]; // 16 bits indicator
        result.extend_from_slice(&raw_value.to_be_bytes());

        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        if !(0.0..=1.0).contains(&value) {
            return Err(SpecError::new(
                "VolumePeakSpec",
                "Peak volume must be between 0.0 and 1.0".to_string(),
            )
            .into());
        }
        Ok(value)
    }
}

/// RVA specification for legacy RVAD frames
#[derive(Debug, Clone)]
pub struct RVASpec {
    pub name: String,
    pub stereo_only: bool,
}

impl RVASpec {
    pub fn new(name: &str, stereo_only: bool) -> Self {
        Self {
            name: name.to_string(),
            stereo_only,
        }
    }
}

impl Spec for RVASpec {
    type Value = Vec<i32>; // Volume adjustments

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        if data.len() < 2 {
            return Err(SpecError::new("RVASpec", "Not enough data for RVA".to_string()).into());
        }

        let flags = data[0];
        let bits = data[1];

        if bits == 0 {
            return Err(SpecError::new("RVASpec", "Bits used must be > 0".to_string()).into());
        }

        let bytes_per_value = bits.div_ceil(8) as usize;
        let max_values = if self.stereo_only { 4 } else { 12 };

        let mut values = Vec::new();
        let mut offset = 2;

        while offset + bytes_per_value <= data.len() && values.len() < max_values {
            let bytes = &data[offset..offset + bytes_per_value];
            let mut value = 0i32;

            for &byte in bytes {
                value = (value << 8) | byte as i32;
            }

            values.push(value);
            offset += bytes_per_value;
        }

        if values.len() < 2 {
            return Err(
                SpecError::new("RVASpec", "First two values not optional".to_string()).into(),
            );
        }

        // Apply increment/decrement flags
        let flag_indices = [0, 1, 4, 5, 8, 10];
        for (bit, &index) in flag_indices.iter().enumerate() {
            if index < values.len() && (flags & (1 << bit)) == 0 {
                values[index] = -values[index];
            }
        }

        Ok((values, offset))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let max_values = if self.stereo_only { 4 } else { 12 };

        if value.len() < 2 || value.len() > max_values {
            return Err(SpecError::new(
                "RVASpec",
                format!(
                    "At least two volume change values required, max {}",
                    max_values
                ),
            )
            .into());
        }

        let mut result = Vec::new();
        let mut flags = 0u8;
        let mut abs_values = value.clone();

        // Calculate flags and absolute values
        let flag_indices = [0, 1, 4, 5, 8, 10];
        for (bit, &index) in flag_indices.iter().enumerate() {
            if index < abs_values.len() {
                if abs_values[index] < 0 {
                    abs_values[index] = -abs_values[index];
                } else {
                    flags |= 1 << bit;
                }
            }
        }

        result.push(flags);

        // Serialize values and find max byte length
        let mut byte_values = Vec::new();
        for &val in &abs_values {
            let bytes = BitPaddedInt::to_str(val as u32, Some(8), Some(true), Some(-1), Some(2))?;
            byte_values.push(bytes);
        }

        let max_bytes = byte_values.iter().map(|v| v.len()).max().unwrap_or(2);

        // Pad all values to same length
        for bytes in &mut byte_values {
            while bytes.len() < max_bytes {
                bytes.push(0);
            }
        }

        let bits = max_bytes * 8;
        result.push(bits as u8);

        for bytes in byte_values {
            result.extend_from_slice(&bytes);
        }

        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        let max_values = if self.stereo_only { 4 } else { 12 };

        if value.len() < 2 || value.len() > max_values {
            return Err(SpecError::new(
                "RVASpec",
                format!("Needs list of length 2..{}", max_values),
            )
            .into());
        }

        Ok(value)
    }
}

/// ASPI index specification for seek point indices
#[derive(Debug, Clone)]
pub struct ASPIIndexSpec {
    pub name: String,
}

impl ASPIIndexSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for ASPIIndexSpec {
    type Value = Vec<u16>; // Seek point indices

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let entry_size = 2; // 16-bit entries
        let num_entries = data.len() / entry_size;

        let mut indices = Vec::new();
        for i in 0..num_entries {
            let offset = i * entry_size;
            if offset + entry_size <= data.len() {
                let value = u16::from_be_bytes([data[offset], data[offset + 1]]);
                indices.push(value);
            }
        }

        Ok((indices, data.len()))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let mut result = Vec::new();

        // Assume 16-bit entries
        for &index in value {
            result.extend_from_slice(&index.to_be_bytes());
        }

        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Synchronized text specification (SYLT frame)
#[derive(Debug, Clone)]
pub struct SynchronizedTextSpec {
    pub name: String,
}

impl SynchronizedTextSpec {
    pub fn new(name: &str) -> Self {
        Self {
            name: name.to_string(),
        }
    }
}

impl Spec for SynchronizedTextSpec {
    type Value = Vec<(String, u32)>; // (text, timestamp) pairs

    fn read(
        &self,
        _header: &FrameHeader,
        _frame: &FrameData,
        data: &[u8],
    ) -> Result<(Self::Value, usize)> {
        let mut texts = Vec::new();
        let mut offset = 0;

        // Default to UTF-8 encoding
        let encoding = TextEncoding::Utf8;

        let terminator = match encoding {
            TextEncoding::Latin1 => vec![0x00],
            TextEncoding::Utf16 => vec![0x00, 0x00],
            TextEncoding::Utf16Be => vec![0x00, 0x00],
            TextEncoding::Utf8 => vec![0x00],
        };

        while offset < data.len() {
            // Find text terminator
            let text_end =
                find_terminator(&data[offset..], &terminator).unwrap_or(data.len() - offset);

            let text_bytes = &data[offset..offset + text_end];
            let text = match encoding {
                TextEncoding::Latin1 => text_bytes.iter().map(|&b| b as char).collect(),
                TextEncoding::Utf8 => String::from_utf8_lossy(text_bytes).into_owned(),
                TextEncoding::Utf16 | TextEncoding::Utf16Be => {
                    // Simplified UTF-16 handling
                    String::from_utf8_lossy(text_bytes).into_owned()
                }
            };

            offset += text_end + terminator.len();

            // Read timestamp (4 bytes)
            if offset + 4 > data.len() {
                return Err(SpecError::new(
                    "SynchronizedTextSpec",
                    "Not enough data for timestamp".to_string(),
                )
                .into());
            }

            let timestamp = u32::from_be_bytes([
                data[offset],
                data[offset + 1],
                data[offset + 2],
                data[offset + 3],
            ]);

            texts.push((text, timestamp));
            offset += 4;
        }

        Ok((texts, data.len()))
    }

    fn write(
        &self,
        _config: &FrameWriteConfig,
        _frame: &FrameData,
        value: &Self::Value,
    ) -> Result<Vec<u8>> {
        let mut result = Vec::new();

        // Default to UTF-8 encoding
        let encoding = TextEncoding::Utf8;

        let terminator = match encoding {
            TextEncoding::Latin1 => vec![0x00],
            TextEncoding::Utf16 => vec![0x00, 0x00],
            TextEncoding::Utf16Be => vec![0x00, 0x00],
            TextEncoding::Utf8 => vec![0x00],
        };

        for (text, timestamp) in value {
            // Encode text based on encoding
            let text_bytes = match encoding {
                TextEncoding::Latin1 => text.chars().map(|c| c as u8).collect::<Vec<u8>>(),
                TextEncoding::Utf8 => text.as_bytes().to_vec(),
                TextEncoding::Utf16 | TextEncoding::Utf16Be => {
                    // Simplified UTF-16 encoding
                    text.as_bytes().to_vec()
                }
            };

            result.extend_from_slice(&text_bytes);
            result.extend_from_slice(&terminator);
            result.extend_from_slice(&timestamp.to_be_bytes());
        }

        Ok(result)
    }

    fn validate(&self, _frame: &FrameData, value: Self::Value) -> Result<Self::Value> {
        Ok(value)
    }
}

/// Helper function to find string terminator in data.
/// For 2-byte terminators (UTF-16), searches at 2-byte-aligned offsets only.
fn find_terminator(data: &[u8], terminator: &[u8]) -> Option<usize> {
    if terminator.is_empty() {
        return Some(data.len());
    }

    if terminator.len() == 2 {
        let mut pos = 0;
        while pos + 1 < data.len() {
            if data[pos] == terminator[0] && data[pos + 1] == terminator[1] {
                return Some(pos);
            }
            pos += 2;
        }
        None
    } else {
        data.windows(terminator.len())
            .position(|window| window == terminator)
    }
}