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
//! Ogg container format support
//!
//! This module provides comprehensive support for the Ogg multimedia container format,
//! which is a free, open container format for digital multimedia. Ogg is designed for
//! efficient streaming and manipulation of high-quality digital multimedia.
//!
//! # Container Structure
//!
//! The Ogg container format is based on a page-oriented structure:
//!
//! - **Pages**: The fundamental unit of an Ogg file. Each page contains a header
//!   and one or more packet segments.
//! - **Packets**: Logical data units that may span multiple pages. Packets contain
//!   codec-specific data (audio samples, video frames, metadata, etc.).
//! - **Streams**: Logical bitstreams identified by serial numbers. Multiple streams
//!   can be multiplexed within a single Ogg file.
//!
//! ## Page Structure
//!
//! Each Ogg page consists of:
//! - **Header** (27 bytes): Contains capture pattern "OggS", version, flags,
//!   granule position, serial number, sequence number, and CRC checksum
//! - **Segment Table**: Array of segment sizes (lacing values)
//! - **Packet Data**: The actual payload data divided into segments
//!
//! ## Header Flags
//!
//! - **Continued (0x01)**: First packet continues from previous page
//! - **Beginning of Stream (0x02)**: First page of logical bitstream
//! - **End of Stream (0x04)**: Last page of logical bitstream
//!
//! # Multiplexing
//!
//! Ogg supports multiplexing multiple independent streams within a single file.
//! Each stream has a unique serial number, and pages from different streams can
//! be interleaved. This allows for:
//! - Multiple audio tracks
//! - Combined audio and video (Ogg Theora)
//! - Metadata and subtitle streams
//! - Low-latency streaming with proper interleaving
//!
//! # Supported Codecs
//!
//! Common codecs used within Ogg containers:
//! - **Vorbis**: Lossy audio compression
//! - **Opus**: Low-latency audio codec
//! - **FLAC**: Lossless audio compression
//! - **Speex**: Speech codec
//! - **Theora**: Video codec
//!
//! # Examples
//!
//! ## Loading an Ogg file
//!
//! ```no_run
//! use audex::ogg::OggFile;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Load an Ogg file and parse all streams
//! let ogg = OggFile::load("/path/to/audio.ogg")?;
//!
//! // Access streams
//! println!("Found {} streams", ogg.streams.len());
//! for (serial, stream) in &ogg.streams {
//!     println!("Stream {}: codec = {}", serial, stream.codec);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Parsing individual pages
//!
//! ```rust
//! use audex::ogg::OggPage;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Build a valid Ogg page with proper CRC, then parse it back
//! let mut page = OggPage::new();
//! page.serial = 42;
//! page.sequence = 1;
//! let ogg_data = page.write()?;
//!
//! let parsed = OggPage::from_bytes(&ogg_data)?;
//! println!("Page version: {}", parsed.version);
//! println!("Serial: {}", parsed.serial);
//! assert_eq!(parsed.serial, 42);
//! # Ok(())
//! # }
//! ```
//!
//! ## Working with multi-stream files
//!
//! ```no_run
//! use audex::ogg::OggFile;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let ogg = OggFile::load("/path/to/multistream.ogg")?;
//!
//! // Get pages for a specific stream
//! if let Some(stream) = ogg.streams.get(&1234) {
//!     let pages = ogg.get_pages_for_stream(1234);
//!     println!("Stream 1234 has {} pages", pages.len());
//!
//!     // Extract packets from pages
//!     let packets = ogg.get_packets(1234)?;
//!     println!("Stream 1234 has {} packets", packets.len());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Converting packets to pages
//!
//! ```rust
//! use audex::ogg::OggPage;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create some example packets
//! let packets = vec![
//!     vec![1, 2, 3, 4, 5],
//!     vec![6, 7, 8, 9, 10],
//! ];
//!
//! // Convert to Ogg pages (sequence starts at 0, target page size 4096, wiggle room 2048)
//! let pages = OggPage::from_packets(packets, 0, 4096, 2048);
//! println!("Created {} pages", pages.len());
//! # Ok(())
//! # }
//! ```
//!
//! # References
//!
//! - [RFC 3533 - The Ogg Encapsulation Format Version 0](https://www.rfc-editor.org/rfc/rfc3533.html)
//! - [Xiph.Org Ogg Documentation](https://xiph.org/ogg/)

use crate::limits::ParseLimits;
use crate::util::{insert_bytes, resize_bytes};
use crate::{AudexError, Result, StreamInfo};
use std::collections::HashMap;
use std::fs::File;
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::time::Duration;

#[cfg(feature = "async")]
use crate::util::{insert_bytes_async, resize_bytes_async};
#[cfg(feature = "async")]
use tokio::fs::File as TokioFile;
#[cfg(feature = "async")]
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, AsyncWrite, AsyncWriteExt};

/// Maximum number of Ogg pages to scan before bailing out.
/// Prevents excessive CPU usage on malformed files with many
/// tiny pages (e.g., zero-segment pages that are only 27 bytes each).
/// A typical 2-hour stereo Vorbis file at 128kbps has ~150,000 pages,
/// so 500,000 provides generous headroom for legitimate files.
const MAX_OGG_PAGES: u32 = 500_000;

/// Ogg CRC-32 lookup table for polynomial 0x04C11DB7 (unreflected).
/// Generated at compile time — each entry is CRC(i) for a single byte i.
const OGG_CRC_TABLE: [u32; 256] = {
    let mut table = [0u32; 256];
    let mut i = 0u32;
    while i < 256 {
        let mut crc = i << 24;
        let mut j = 0;
        while j < 8 {
            if crc & 0x80000000 != 0 {
                crc = (crc << 1) ^ 0x04C11DB7;
            } else {
                crc <<= 1;
            }
            j += 1;
        }
        table[i as usize] = crc;
        i += 1;
    }
    table
};

/// Compute the Ogg CRC-32 checksum over a byte slice.
///
/// Uses the Ogg-specific polynomial 0x04C11DB7, MSB-first with no
/// input/output reflection, initial value 0, and no final XOR.
fn ogg_crc32(data: &[u8]) -> u32 {
    let mut crc: u32 = 0;
    for &byte in data {
        let index = ((crc >> 24) as u8) ^ byte;
        crc = (crc << 8) ^ OGG_CRC_TABLE[index as usize];
    }
    crc
}

/// File manipulation utilities
mod file_utils {
    use crate::Result;
    use std::io::{Read, Seek, SeekFrom};

    /// Seek backwards from end, ensuring we don't go before start
    pub fn seek_end<F: Read + Seek>(fileobj: &mut F, offset: u64) -> Result<()> {
        fileobj.seek(SeekFrom::End(0))?;
        let filesize = fileobj.stream_position()?;
        let seek_pos = filesize.saturating_sub(offset);
        fileobj.seek(SeekFrom::Start(seek_pos))?;
        Ok(())
    }
}

/// Ogg page header and data
///
/// Represents a single Ogg page, which is the fundamental unit of the Ogg container format.
/// Each page contains header information and packet data for a specific logical stream.
///
/// # Structure
///
/// An Ogg page consists of:
/// - A 27-byte header containing metadata
/// - A segment table that describes packet boundaries
/// - One or more packet fragments (segments)
///
/// # Page Header Fields
///
/// - **version**: Stream structure version (always 0 for current Ogg specification)
/// - **header_type**: Bitfield containing flags (continued, BOS, EOS)
/// - **position**: Granule position (codec-specific time/position marker, -1 for incomplete)
/// - **serial**: Unique serial number identifying the logical stream
/// - **sequence**: Page sequence number within the stream
/// - **checksum**: CRC32 checksum of the entire page
/// - **segments**: Segment table (lacing values) describing packet fragment sizes
/// - **packets**: Actual packet data fragments
/// - **offset**: Optional file offset where this page was found
/// - **complete**: Whether all packets on this page are complete (not spanning to next page)
///
/// # Examples
///
/// ## Creating an empty page
///
/// ```rust
/// use audex::ogg::OggPage;
///
/// let page = OggPage::new();
/// assert_eq!(page.version, 0);
/// assert_eq!(page.serial, 0);
/// ```
///
/// ## Parsing a page from data
///
/// ```rust
/// use audex::ogg::OggPage;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Build a valid page, serialize it, then parse it back
/// let mut page = OggPage::new();
/// page.serial = 1;
/// page.sequence = 5;
/// let data = page.write()?;
///
/// let parsed = OggPage::from_bytes(&data)?;
/// assert_eq!(parsed.serial, 1);
/// assert_eq!(parsed.sequence, 5);
/// # Ok(())
/// # }
/// ```
///
/// ## Checking page flags
///
/// ```rust
/// use audex::ogg::OggPage;
///
/// let mut page = OggPage::new();
/// page.set_first(true);
/// page.set_last(true);
///
/// assert!(page.is_first());
/// assert!(page.is_last());
/// assert!(page.first()); // Alternative method name
/// assert!(page.last());  // Alternative method name
/// ```
#[derive(Debug, Clone)]
pub struct OggPage {
    /// Stream structure version (always 0 for current specification)
    pub version: u8,

    /// Header type flags bitfield
    /// - Bit 0 (0x01): Continued packet flag
    /// - Bit 1 (0x02): Beginning of stream (BOS) flag
    /// - Bit 2 (0x04): End of stream (EOS) flag
    pub header_type: u8,

    /// Granule position - codec-specific time/position marker.
    /// A value of -1 means "no granule position set" per the Ogg spec.
    /// When parsing from a file the raw header value is used as-is.
    pub position: i64,

    /// Bitstream serial number - unique identifier for this logical stream
    pub serial: u32,

    /// Page sequence number - increments for each page in the stream.
    /// The Ogg specification treats this as an unsigned 32-bit integer.
    pub sequence: u32,

    /// CRC32 checksum calculated over the entire page
    pub checksum: u32,

    /// Segment table (lacing values) - array of segment sizes
    /// Each value indicates the size of a segment (0-255 bytes)
    pub segments: Vec<u8>,

    /// Packet data - actual payload divided into packets
    /// Packets may span multiple pages
    pub packets: Vec<Vec<u8>>,

    /// File offset where this page was found (if parsed from a file)
    pub offset: Option<i64>,

    /// Whether all packets on this page are complete
    /// False if the last packet continues on the next page
    pub complete: bool,
}

impl OggPage {
    // Property-style methods for flag access

    /// Check if this is the first page of a stream
    pub fn first(&self) -> bool {
        (self.header_type & 0x02) != 0
    }

    /// Check if this is the last page of a stream  
    pub fn last(&self) -> bool {
        (self.header_type & 0x04) != 0
    }

    /// Check if first packet continues from previous page
    pub fn continued(&self) -> bool {
        (self.header_type & 0x01) != 0
    }
    /// Create new empty Ogg page
    pub fn new() -> Self {
        Self {
            version: 0,
            header_type: 0,
            position: 0,
            serial: 0,
            sequence: 0,
            checksum: 0,
            segments: Vec::new(),
            packets: Vec::new(),
            offset: None,
            complete: true,
        }
    }

    /// Constructor that accepts optional reader
    ///
    /// # Arguments
    ///
    /// * `fileobj` - Optional reader to parse page from. If None, creates empty page
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Create empty page
    /// let empty_page = OggPage::new_from_fileobj(None::<&mut std::io::Cursor<Vec<u8>>>)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ```rust
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Build a valid page, serialize it, then parse via reader
    /// let ogg_data = OggPage::new().write()?;
    /// let mut reader = std::io::Cursor::new(ogg_data);
    /// let page = OggPage::new_from_fileobj(Some(&mut reader))?;
    /// assert_eq!(page.version, 0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn new_from_fileobj<R: Read + Seek>(fileobj: Option<&mut R>) -> Result<Self> {
        match fileobj {
            Some(reader) => Self::from_reader(reader),
            None => Ok(Self::new()),
        }
    }

    /// Creates an OggPage by immediately reading and parsing from the provided reader
    ///
    /// # Arguments
    ///
    /// * `fileobj` - Reader to parse page from
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Build a valid page, serialize it, then parse via from_fileobj
    /// let ogg_data = OggPage::new().write()?;
    /// let mut reader = std::io::Cursor::new(ogg_data);
    /// let page = OggPage::from_fileobj(&mut reader)?;
    /// assert_eq!(page.version, 0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_fileobj<R: Read + Seek>(fileobj: &mut R) -> Result<Self> {
        Self::from_reader(fileobj)
    }

    /// Convenience constructor for file paths
    /// Opens the file and reads the first Ogg page
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the Ogg file
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let page = OggPage::from_file("/path/to/file.ogg")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
        let mut file = File::open(path.as_ref()).map_err(|e| {
            AudexError::Io(std::io::Error::new(
                e.kind(),
                format!("Failed to open file '{}': {}", path.as_ref().display(), e),
            ))
        })?;
        Self::from_reader(&mut file)
    }

    /// Constructor for byte slice data
    /// Creates an OggPage from byte data
    ///
    /// # Arguments
    ///
    /// * `data` - Byte slice containing Ogg page data
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Build a valid page with proper CRC, then parse from bytes
    /// let ogg_page_data = OggPage::new().write()?;
    /// let page = OggPage::from_bytes(&ogg_page_data)?;
    /// assert_eq!(page.version, 0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        Self::from_reader(&mut cursor)
    }

    /// Constructor from `Vec<u8>` (consumes the vector)
    /// Creates an OggPage from owned byte data
    ///
    /// # Arguments
    ///
    /// * `data` - Vector containing Ogg page data
    ///
    /// # Examples
    ///
    /// ```rust
    /// use audex::ogg::OggPage;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // Build a valid page with proper CRC, then parse from owned bytes
    /// let ogg_page_data = OggPage::new().write()?;
    /// let page = OggPage::from_vec(ogg_page_data)?;
    /// assert_eq!(page.version, 0);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_vec(data: Vec<u8>) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        Self::from_reader(&mut cursor)
    }

    /// Parse Ogg page from reader
    pub fn from_reader<R: Read + Seek>(reader: &mut R) -> Result<Self> {
        let offset = reader.stream_position().ok().map(|o| o as i64);

        // Read header (27 bytes)
        let mut header = [0u8; 27];
        match reader.read_exact(&mut header) {
            Ok(()) => {}
            Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                return Err(AudexError::from(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    "End of file",
                )));
            }
            Err(e) => return Err(AudexError::from(e)),
        }

        // Check OggS signature
        if &header[0..4] != b"OggS" {
            error_event!("invalid OGG page signature");
            return Err(AudexError::InvalidData(format!(
                "Invalid OGG signature: {:?}",
                &header[0..4]
            )));
        }

        let version = header[4];
        if version != 0 {
            warn_event!(version = version, "unsupported OGG version");
            return Err(AudexError::UnsupportedFormat(format!(
                "Unsupported OGG version: {}",
                version
            )));
        }

        let header_type = header[5];
        let position_u64 = u64::from_le_bytes([
            header[6], header[7], header[8], header[9], header[10], header[11], header[12],
            header[13],
        ]);
        // Interpret as signed i64 via two's complement — the Ogg spec
        // treats granule position as signed, where -1 (all bits set)
        // means "no position". The `as i64` cast is a bit reinterpretation
        // and handles all values correctly, including u64::MAX → -1.
        let position = position_u64 as i64;

        let serial = u32::from_le_bytes([header[14], header[15], header[16], header[17]]);

        let sequence = u32::from_le_bytes([header[18], header[19], header[20], header[21]]);

        let checksum = u32::from_le_bytes([header[22], header[23], header[24], header[25]]);
        let segment_count = header[26];

        // Read segment table
        let mut segments = vec![0u8; segment_count as usize];
        reader.read_exact(&mut segments)?;

        // Calculate packet boundaries and read data
        let mut packets = Vec::new();
        let mut current_packet = Vec::new();

        for &segment_size in &segments {
            let mut segment_data = vec![0u8; segment_size as usize];
            reader.read_exact(&mut segment_data)?;
            current_packet.extend_from_slice(&segment_data);

            // If segment size < 255, packet is complete
            if segment_size < 255 {
                packets.push(current_packet);
                current_packet = Vec::new();
            }
        }

        // If we have remaining data in current_packet, it's an incomplete packet
        let complete = if !current_packet.is_empty() {
            packets.push(current_packet);
            false // Last packet is incomplete
        } else {
            // Check if last segment is < 255 (complete packet)
            segments.last().is_none_or(|&s| s < 255)
        };

        let page = Self {
            version,
            header_type,
            position,
            serial,
            sequence,
            checksum,
            segments,
            packets,
            offset,
            complete,
        };

        // Verify the CRC32 checksum against the actual page contents.
        // The spec requires readers to validate this to detect corruption.
        //
        // The CRC check happens after packet assembly because the Ogg CRC
        // covers the entire serialized page (header + segment table + payload).
        // We must read the payload to compute the checksum, so there is no way
        // to reject a bad CRC before the data is in memory. However, per-page
        // payload size is bounded by the segment table (max 255 segments of
        // 255 bytes each = ~64 KB), so the allocation cost before rejection is
        // inherently limited regardless of total file size.
        let computed_crc = page.compute_read_crc()?;
        if computed_crc != checksum {
            return Err(AudexError::InvalidData(format!(
                "OGG page CRC32 mismatch: stored={:#010X}, computed={:#010X}",
                checksum, computed_crc
            )));
        }

        Ok(page)
    }

    /// Reconstruct the raw page bytes using the stored segment table
    /// and compute CRC32 with the checksum field zeroed. This gives
    /// the same CRC that the original writer computed, allowing us
    /// to detect any corruption that occurred after the page was written.
    ///
    /// NOTE: This uses the segment table and packet data as stored in the struct.
    /// It is only valid on freshly-parsed pages. After modifying packets, the
    /// segment table may be stale, producing an incorrect CRC. Use write_to()
    /// for serialization — it recomputes the CRC from current data.
    fn compute_read_crc(&self) -> Result<u32> {
        let mut raw = Vec::new();

        // Header (27 bytes)
        raw.extend_from_slice(b"OggS");
        raw.push(self.version);
        raw.push(self.header_type);
        let position_u64 = self.position as u64;
        raw.extend_from_slice(&position_u64.to_le_bytes());
        raw.extend_from_slice(&self.serial.to_le_bytes());
        raw.extend_from_slice(&self.sequence.to_le_bytes());
        // Checksum field must be zeroed for CRC computation
        raw.extend_from_slice(&[0u8; 4]);
        // The Ogg spec limits each page to 255 segments. Reject pages that
        // violate this, since truncating the count would produce a wrong CRC.
        if self.segments.len() > 255 {
            return Err(AudexError::InvalidData(format!(
                "Ogg page has {} segments, exceeding the 255 segment limit",
                self.segments.len()
            )));
        }
        raw.push(self.segments.len() as u8);

        // Segment table — use the original lacing values, not rebuilt ones
        raw.extend_from_slice(&self.segments);

        // Packet payload data
        for packet in &self.packets {
            raw.extend_from_slice(packet);
        }

        Ok(self.calculate_ogg_crc32(&raw))
    }

    /// Write Ogg page to writer
    pub fn write_to<W: Write>(&self, writer: &mut W) -> Result<Vec<u8>> {
        let mut data = Vec::new();

        // Write header
        data.extend_from_slice(b"OggS");
        data.push(self.version);
        data.push(self.header_type);

        // The granule position is stored as a raw 64-bit field. The i64 value
        // is reinterpreted as u64 for serialization (-1 becomes 0xFFFFFFFFFFFFFFFF,
        // which is the standard "no position" sentinel in the Ogg spec).
        data.extend_from_slice(&(self.position as u64).to_le_bytes());

        data.extend_from_slice(&self.serial.to_le_bytes());
        data.extend_from_slice(&self.sequence.to_le_bytes());
        data.extend_from_slice(&[0, 0, 0, 0]); // Placeholder for checksum

        // Build segment table from packets
        let mut lacing_data = Vec::new();
        for packet in &self.packets {
            let packet_len = packet.len();
            let (full_segments, remainder) = (packet_len / 255, packet_len % 255);

            // Add full segments (255 bytes each)
            lacing_data.extend(std::iter::repeat_n(255u8, full_segments));

            // Add remainder segment
            lacing_data.push(remainder as u8);
        }

        // Handle incomplete pages - strip trailing zero segment
        if !self.complete && lacing_data.last() == Some(&0) {
            lacing_data.pop();
        }

        // The Ogg spec limits each page to 255 segments. If the lacing table
        // exceeds this, the page is too large and must be split by the caller.
        if lacing_data.len() > 255 {
            return Err(AudexError::InvalidData(format!(
                "Ogg page has {} lacing segments, exceeding the 255 segment limit",
                lacing_data.len()
            )));
        }

        data.push(lacing_data.len() as u8);
        data.extend_from_slice(&lacing_data);

        // Write packet data
        for packet in &self.packets {
            data.extend_from_slice(packet);
        }

        // Calculate CRC32 using bit-swapped algorithm to match Ogg specification
        let crc = self.calculate_ogg_crc32(&data);

        // Update checksum in data
        data[22..26].copy_from_slice(&crc.to_le_bytes());

        writer.write_all(&data)?;
        Ok(data)
    }

    /// Calculate Ogg-specific CRC-32 using the standard Ogg polynomial.
    ///
    /// The Ogg format uses polynomial 0x04C11DB7 (MSB-first, no reflection,
    /// init=0, final XOR=0). This is computed directly via a 256-entry
    /// lookup table rather than adapting a different CRC-32 variant.
    fn calculate_ogg_crc32(&self, data: &[u8]) -> u32 {
        ogg_crc32(data)
    }

    /// Write page as bytes without writing to a writer
    pub fn write(&self) -> Result<Vec<u8>> {
        let mut buffer = Vec::new();
        self.write_to(&mut buffer)?;
        Ok(buffer)
    }

    /// Calculate CRC32 checksum for this page.
    /// Returns an error if the page cannot be serialized (e.g., too many segments).
    pub fn calculate_crc(&self) -> Result<u32> {
        let mut data = Vec::new();
        self.write_to(&mut data)?;

        // Clear checksum field for calculation
        if data.len() >= 26 {
            data[22..26].copy_from_slice(&[0, 0, 0, 0]);
        }

        Ok(self.calculate_ogg_crc32(&data))
    }

    /// Get total page size in bytes
    pub fn size(&self) -> usize {
        let mut size = 27; // Header size

        // Add segment table size
        let mut segment_count = 0;
        for packet in &self.packets {
            let packet_len = packet.len();
            let full_segments = packet_len / 255;
            segment_count += full_segments + 1; // +1 for remainder segment
        }

        // If last packet ends at segment boundary and page is incomplete,
        // we don't need the final zero byte (it gets removed by write_to)
        if !self.packets.is_empty() && !self.complete {
            // Safe: we just verified packets is non-empty above.
            let last_packet_len = self
                .packets
                .last()
                .expect("packets confirmed non-empty")
                .len();
            // Strip the trailing zero segment to match write_to(). This
            // covers both segment-boundary packets and empty packets.
            if last_packet_len % 255 == 0 && segment_count > 0 {
                segment_count -= 1;
            }
        }

        size += segment_count; // Segment table
        size += self.packets.iter().map(|p| p.len()).sum::<usize>(); // Packet data
        size
    }

    /// Check if page starts with "OggS"
    pub fn validate_sync(data: &[u8]) -> bool {
        data.len() >= 4 && &data[0..4] == b"OggS"
    }

    /// Check if this is the first page of a stream
    pub fn is_first(&self) -> bool {
        (self.header_type & 0x02) != 0
    }

    /// Check if this is the last page of a stream
    pub fn is_last(&self) -> bool {
        (self.header_type & 0x04) != 0
    }

    /// Check if first packet continues from previous page
    pub fn is_continued(&self) -> bool {
        (self.header_type & 0x01) != 0
    }

    /// Set first page flag
    pub fn set_first(&mut self, first: bool) {
        if first {
            self.header_type |= 0x02;
        } else {
            self.header_type &= !0x02;
        }
    }

    /// Set last page flag
    pub fn set_last(&mut self, last: bool) {
        if last {
            self.header_type |= 0x04;
        } else {
            self.header_type &= !0x04;
        }
    }

    /// Set continued packet flag
    pub fn set_continued(&mut self, continued: bool) {
        if continued {
            self.header_type |= 0x01;
        } else {
            self.header_type &= !0x01;
        }
    }

    /// Check if this page completes all packets
    pub fn is_complete(&self) -> bool {
        self.complete
    }

    /// Set complete flag
    pub fn set_complete(&mut self, complete: bool) {
        self.complete = complete;
    }

    /// Find the last page with given serial number
    pub fn find_last<R: Read + Seek>(
        reader: &mut R,
        serial: u32,
        finishing: bool,
    ) -> Result<Option<Self>> {
        Self::find_last_with_finishing(reader, serial, finishing)
    }

    #[doc(hidden)]
    pub fn accumulate_page_bytes_with_limit(
        limits: ParseLimits,
        cumulative_bytes: &mut u64,
        page: &OggPage,
        context: &str,
    ) -> Result<()> {
        let page_bytes: u64 = page.packets.iter().map(|pkt| pkt.len() as u64).sum();
        *cumulative_bytes = cumulative_bytes.saturating_add(page_bytes);
        limits.check_tag_size(*cumulative_bytes, context)
    }

    /// Find the last page with given serial number (legacy version for backward compatibility)
    pub fn find_last_u32<R: Read + Seek>(reader: &mut R, serial: u32) -> Result<Option<Self>> {
        Self::find_last(reader, serial, false)
    }

    /// Find the last page with the given serial number, optionally requiring
    /// a valid finishing position.
    ///
    /// The method first attempts a **fast path**: it reads the trailing 64 KiB
    /// of the file and searches backwards for the last `OggS` signature. If
    /// the target serial is found there with an EOS flag, it returns
    /// immediately.
    ///
    /// When the fast path fails (e.g. multiplexed streams, or missing EOS),
    /// a **slow path** scans from the beginning of the file page-by-page.
    /// This is bounded by `MAX_OGG_PAGES` and the cumulative byte budget
    /// from `ParseLimits`, but it can still be significantly slower and will
    /// hold the most-recently-matched page in memory until the scan completes.
    pub fn find_last_with_finishing<R: Read + Seek>(
        reader: &mut R,
        serial: u32,
        finishing: bool,
    ) -> Result<Option<Self>> {
        // For non-multiplexed streams, check the last page first (fast path)
        file_utils::seek_end(reader, 256 * 256)?;

        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer)?;

        // Find last OggS signature
        if let Some(index) = buffer.windows(4).rposition(|w| w == b"OggS") {
            let mut cursor = Cursor::new(&buffer[index..]);
            if let Ok(page) = Self::from_reader(&mut cursor) {
                if page.serial == serial {
                    let is_valid = !finishing || page.position != -1;
                    if is_valid && page.last() {
                        return Ok(Some(page));
                    }
                    // Continue searching for EOS page, but keep this as backup
                }
            }
        }

        // Stream is multiplexed or we need to find EOS page - use slow method.
        // Track cumulative packet data to prevent memory exhaustion on crafted
        // files with many large pages (matching the budget in OggFile::load).
        // Use a reduced page cap for this fallback scan to limit CPU time;
        // the fast path above handles well-formed files, so the slow path
        // only needs enough headroom for multiplexed or truncated streams.
        const MAX_SLOW_PATH_PAGES: u32 = 50_000;
        reader.seek(SeekFrom::Start(0))?;
        let mut best_page = None;
        let mut pages_scanned = 0u32;
        let mut cumulative_bytes: u64 = 0;
        let limits = ParseLimits::default();

        loop {
            // Cap the number of pages to prevent excessive scanning on
            // malformed files with many tiny (e.g., zero-segment) pages
            if pages_scanned >= MAX_SLOW_PATH_PAGES {
                break;
            }

            match Self::from_reader(reader) {
                Ok(page) => {
                    pages_scanned += 1;

                    Self::accumulate_page_bytes_with_limit(
                        limits,
                        &mut cumulative_bytes,
                        &page,
                        "OGG cumulative page data",
                    )?;

                    if page.serial == serial {
                        let is_valid = !finishing || page.position != -1;
                        if is_valid {
                            best_page = Some(page.clone());
                        }
                        if page.last() {
                            break;
                        }
                    }
                }
                Err(e) => {
                    if let AudexError::Io(io_err) = &e {
                        if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
                            break;
                        }
                    }
                    return Ok(best_page);
                }
            }
        }

        Ok(best_page)
    }

    /// Convert pages to packets for a specific serial
    pub fn to_packets(pages: &[OggPage], strict: bool) -> Result<Vec<Vec<u8>>> {
        Self::to_packets_strict(pages, strict)
    }

    /// Convert pages to packets with strict validation
    /// If strict is true, first page must start new packet and last page must end packet
    pub fn to_packets_strict(pages: &[OggPage], strict: bool) -> Result<Vec<Vec<u8>>> {
        // Safety cap: reject reconstructed packet data exceeding 256 MB total.
        // This prevents runaway memory usage from malformed or adversarial streams.
        const MAX_TOTAL_PACKET_BYTES: usize = 256 * 1024 * 1024;

        if pages.is_empty() {
            return Ok(Vec::new());
        }

        let serial = pages[0].serial;
        let mut packets: Vec<Vec<u8>> = Vec::new();
        let mut total_bytes: usize = 0;

        // Strict mode validation
        if strict {
            if pages[0].continued() {
                return Err(AudexError::InvalidData(
                    "first packet is continued".to_string(),
                ));
            }
            // Safe: early return above guarantees pages is non-empty.
            if !pages
                .last()
                .expect("pages confirmed non-empty")
                .is_complete()
            {
                return Err(AudexError::InvalidData(
                    "last packet does not complete".to_string(),
                ));
            }
        } else if !pages.is_empty() && pages[0].continued() {
            // Non-strict mode with continued first packet - start with empty packet
            packets.push(vec![]);
        }

        // Sequences must be consecutive
        // Start with the first page's sequence number, not necessarily 0
        let mut expected_sequence = pages[0].sequence;

        for page in pages.iter() {
            if page.serial != serial {
                return Err(AudexError::InvalidData(format!(
                    "invalid serial number in page: expected {}, got {}",
                    serial, page.serial
                )));
            }

            // Check sequence numbers - must be consecutive
            if page.sequence != expected_sequence {
                warn_event!(
                    expected = expected_sequence,
                    actual = page.sequence,
                    "OGG page sequence number mismatch"
                );
                return Err(AudexError::InvalidData(format!(
                    "bad sequence number in page: expected {}, got {}",
                    expected_sequence, page.sequence
                )));
            }

            // Increment for next page (wrapping to handle max sequence)
            expected_sequence = expected_sequence.wrapping_add(1);

            if !page.packets.is_empty() {
                if page.continued() {
                    // Continue the last packet
                    if let Some(last_packet) = packets.last_mut() {
                        total_bytes += page.packets[0].len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        last_packet.extend_from_slice(&page.packets[0]);
                    } else {
                        // Should not happen in valid streams
                        return Err(AudexError::InvalidData(
                            "Continued packet with no previous packet".to_string(),
                        ));
                    }

                    // Add remaining complete packets
                    for packet in &page.packets[1..] {
                        total_bytes += packet.len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        packets.push(packet.clone());
                    }
                } else {
                    // All packets on this page are complete
                    for packet in &page.packets {
                        total_bytes += packet.len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        packets.push(packet.clone());
                    }
                }
            }
        }

        Ok(packets)
    }

    /// Create pages from packet data with default parameters
    pub fn from_packets_simple(packets: Vec<Vec<u8>>) -> Vec<OggPage> {
        Self::from_packets(packets, 0, 4096, 2048)
    }

    /// Create pages from packet data with custom sequence
    pub fn from_packets_sequence(packets: Vec<Vec<u8>>, sequence: u32) -> Vec<OggPage> {
        Self::from_packets(packets, sequence, 4096, 2048)
    }

    /// Internal method to extract packets without sequence validation
    /// Used by from_packets_try_preserve for size comparison
    fn to_packets_no_sequence_validation(pages: &[OggPage]) -> Result<Vec<Vec<u8>>> {
        // Same cumulative byte budget as to_packets_strict to prevent
        // memory exhaustion from adversarial or malformed page sequences.
        const MAX_TOTAL_PACKET_BYTES: usize = 256 * 1024 * 1024;

        if pages.is_empty() {
            return Ok(Vec::new());
        }

        let serial = pages[0].serial;
        let mut packets: Vec<Vec<u8>> = Vec::new();
        let mut total_bytes: usize = 0;

        // No sequence validation - just reconstruct packets
        for page in pages.iter() {
            if page.serial != serial {
                return Err(AudexError::InvalidData(format!(
                    "invalid serial number in page: expected {}, got {}",
                    serial, page.serial
                )));
            }

            if !page.packets.is_empty() {
                if page.continued() {
                    // Continue the last packet
                    if let Some(last_packet) = packets.last_mut() {
                        total_bytes += page.packets[0].len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        last_packet.extend_from_slice(&page.packets[0]);
                    } else {
                        // Continued flag set but no preceding packet to append to.
                        // This indicates a truncated or out-of-order stream. The
                        // first segment is dropped because there is no context to
                        // reconstruct the full packet.
                        warn_event!(
                            serial = serial,
                            sequence = page.sequence,
                            dropped_bytes = page.packets[0].len(),
                            "OGG continued packet has no predecessor; dropping first segment"
                        );
                    }
                    // Add remaining packets from this page
                    for packet in &page.packets[1..] {
                        total_bytes += packet.len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        packets.push(packet.clone());
                    }
                } else {
                    // Add all packets from this page
                    for packet in &page.packets {
                        total_bytes += packet.len();
                        if total_bytes > MAX_TOTAL_PACKET_BYTES {
                            return Err(AudexError::InvalidData(
                                "cumulative packet data exceeds 256 MB limit".to_string(),
                            ));
                        }
                        packets.push(packet.clone());
                    }
                }
            }
        }

        Ok(packets)
    }

    /// Try to preserve original page layout when packet sizes match
    /// Falls back to regular from_packets if sizes don't match
    /// EXACTLY matches the _from_packets_try_preserve behavior
    pub fn from_packets_try_preserve(packets: Vec<Vec<u8>>, old_pages: &[OggPage]) -> Vec<OggPage> {
        if old_pages.is_empty() {
            return Vec::new();
        }

        // Extract old packets and compare sizes - use non-validating version
        // This allows comparison even when sequences don't start at 0
        let old_packets = match Self::to_packets_no_sequence_validation(old_pages) {
            Ok(packets) => packets,
            Err(_) => return Self::from_packets(packets, old_pages[0].sequence, 4096, 2048),
        };

        // Check if packet sizes match
        let new_sizes: Vec<usize> = packets.iter().map(|p| p.len()).collect();
        let old_sizes: Vec<usize> = old_packets.iter().map(|p| p.len()).collect();

        if new_sizes != old_sizes {
            // Sizes don't match, fall back to regular from_packets
            return Self::from_packets(packets, old_pages[0].sequence, 4096, 2048);
        }

        // Sizes match - preserve page layout exactly.
        // Keep a clone for fallback in case the drain loop hits an inconsistency.
        let packets_backup = packets.clone();
        let mut new_data: Vec<u8> = packets.into_iter().flatten().collect();
        let mut new_pages = Vec::new();
        let sequence = old_pages[0].sequence;

        for old_page in old_pages {
            let mut new_page = OggPage::new();
            new_page.sequence = old_page.sequence;
            new_page.serial = old_page.serial;
            new_page.complete = old_page.complete;
            new_page.set_continued(old_page.continued());
            new_page.position = old_page.position;

            for old_packet in &old_page.packets {
                let packet_len = old_packet.len();
                if new_data.len() >= packet_len {
                    let packet_data = new_data.drain(..packet_len).collect::<Vec<u8>>();
                    new_page.packets.push(packet_data);
                } else {
                    // Page layout inconsistency — fall back to standard page generation
                    return Self::from_packets(packets_backup, sequence, 4096, 2048);
                }
            }

            new_pages.push(new_page);
        }

        // All data should be consumed after distributing across pages
        if !new_data.is_empty() {
            // Leftover data means the page layout didn't account for all bytes.
            // Fall back to standard page generation to avoid data loss.
            return Self::from_packets(packets_backup, sequence, 4096, 2048);
        }

        new_pages
    }

    /// Create pages from packet data
    ///
    /// Returns an empty vector if an internal error occurs during page
    /// construction. In practice this cannot happen because the granule
    /// position is fixed at 0 (always fits in i64) and the internal page
    /// list is always non-empty after a push, but we avoid panicking to
    /// keep the API robust.
    pub fn from_packets(
        packets: Vec<Vec<u8>>,
        sequence: u32,
        default_size: usize,
        wiggle_room: usize,
    ) -> Vec<OggPage> {
        Self::from_packets_with_options(packets, sequence, default_size, wiggle_room, 0)
            .unwrap_or_default()
    }

    /// Create pages from packet data with position setting
    /// EXACT port of the from_packets algorithm
    pub fn from_packets_with_options(
        packets: Vec<Vec<u8>>,
        sequence: u32,
        default_size: usize,
        wiggle_room: usize,
        granule_position: u64,
    ) -> Result<Vec<OggPage>> {
        // Cap chunk size so a single chunk never exceeds 255 lacing segments.
        // A chunk of N bytes needs (N / 255) + 1 segments, so the maximum
        // single-chunk payload for 255 segments is 254 * 255 = 64770 bytes.
        const MAX_CHUNK: usize = 254 * 255;
        let chunk_size = ((default_size / 255) * 255).min(MAX_CHUNK);
        let mut pages = Vec::new();
        let mut page = OggPage::new();
        page.sequence = sequence;

        // Track lacing segments per page — Ogg limits each page to 255.
        // Each packet contributes (byte_len / 255) + 1 segments.
        let mut page_segment_count: usize = 0;

        for packet in packets {
            page.packets.push(Vec::new());
            let mut remaining_packet = packet;

            while !remaining_packet.is_empty() {
                let data_len = chunk_size.min(remaining_packet.len());
                let data = remaining_packet.drain(..data_len).collect::<Vec<u8>>();
                let packet_len = remaining_packet.len();

                // Compute how many lacing segments the page would need
                // if we added this data to the current last packet.
                let last_pkt_len = page.packets.last().map_or(0, |p| p.len());
                let new_pkt_len = last_pkt_len + data.len();
                let segments_before = (last_pkt_len / 255) + if last_pkt_len > 0 { 1 } else { 0 };
                let segments_after = (new_pkt_len / 255) + 1;
                let extra_segments = segments_after.saturating_sub(segments_before);

                if page.size() < default_size && page_segment_count + extra_segments <= 255 {
                    // Add data to the last packet (the one we just created for this iteration)
                    if let Some(last_packet) = page.packets.last_mut() {
                        last_packet.extend(data);
                    }
                    page_segment_count += extra_segments;
                } else {
                    // logic for page overflow
                    if let Some(last_packet) = page.packets.last() {
                        if !last_packet.is_empty() {
                            // If we've put any packet data into this page yet,
                            // we need to mark it incomplete.
                            page.complete = false;
                            if page.packets.len() == 1 {
                                // Set position to -1 for incomplete page
                                page.position = -1;
                            }
                        } else {
                            // However, we can also have just started this packet on an already
                            // full page, in which case, just start the new page with this packet.
                            page.packets.pop();
                        }
                    }

                    pages.push(page);
                    page = OggPage::new();
                    // Safe: pages is non-empty after the push above
                    let prev = pages.last().ok_or_else(|| {
                        AudexError::InternalError("page list empty after push".to_string())
                    })?;
                    page.set_continued(!prev.complete);
                    page.sequence = prev.sequence.checked_add(1).ok_or_else(|| {
                        AudexError::InvalidData(
                            "Ogg page sequence counter overflow while building pages".to_string(),
                        )
                    })?;
                    // Reset segment counter for the new page
                    let new_pkt_segments = (data.len() / 255) + 1;
                    page_segment_count = new_pkt_segments;
                    page.packets.push(data);
                }

                // wiggle room logic — absorb the remainder into the
                // current packet if it's small enough and won't exceed
                // the 255 segment limit per Ogg page
                if packet_len < wiggle_room {
                    if let Some(last_packet) = page.packets.last_mut() {
                        let before = (last_packet.len() / 255) + 1;
                        let projected_len = last_packet.len() + remaining_packet.len();
                        let after = (projected_len / 255) + 1;
                        let extra = after.saturating_sub(before);

                        // Only absorb if the total segment count stays within
                        // the Ogg page maximum of 255 segments
                        if page_segment_count + extra <= 255 {
                            last_packet.extend_from_slice(&remaining_packet);
                            page_segment_count += extra;
                            remaining_packet.clear();
                        }
                    }
                }
            }
        }

        if !page.packets.is_empty() {
            pages.push(page);
        }

        // Validate that the granule position fits in i64 to prevent
        // silent truncation for extremely long streams.
        let signed_position = i64::try_from(granule_position).map_err(|_| {
            AudexError::InvalidData(format!(
                "Granule position {} exceeds i64::MAX and cannot be represented",
                granule_position
            ))
        })?;

        // Set the final granule position on the last page
        if let Some(last_page) = pages.last_mut() {
            last_page.position = signed_position;
        }

        Ok(pages)
    }

    /// Renumber pages for a given serial starting at sequence number
    pub fn renumber<R: Read + Write + Seek>(
        reader: &mut R,
        serial: u32,
        start_sequence: u32,
    ) -> Result<()> {
        let mut sequence = start_sequence;
        let mut pages_scanned: u32 = 0;

        loop {
            // Enforce the same page limit as OggFile::load() to prevent
            // unbounded iteration from crafted files with many tiny pages
            if pages_scanned >= MAX_OGG_PAGES {
                return Err(AudexError::ParseError(format!(
                    "Ogg renumber exceeded maximum page count ({})",
                    MAX_OGG_PAGES
                )));
            }

            let page_offset = reader.stream_position()?;

            match OggPage::from_reader(reader) {
                Ok(mut page) => {
                    if page.serial == serial {
                        // Update sequence number
                        page.sequence = sequence;

                        // Changing the sequence number cannot change the page size,
                        // so we can safely seek back and overwrite
                        reader.seek(SeekFrom::Start(page_offset))?;

                        // Write updated page with new sequence and recalculated CRC
                        let page_data = page.write()?;
                        reader.write_all(&page_data)?;

                        // Seek to end of this page to continue
                        reader.seek(SeekFrom::Start(page_offset + page_data.len() as u64))?;

                        sequence = sequence.checked_add(1).ok_or_else(|| {
                            AudexError::InvalidData(
                                "Ogg page sequence counter overflow".to_string(),
                            )
                        })?;
                    }
                    pages_scanned += 1;
                }
                Err(e) => {
                    if let AudexError::Io(io_err) = &e {
                        if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
                            break; // Normal end of file
                        }
                    }
                    // For any other error, we might have hit invalid data
                    // Return the error to indicate the issue
                    return Err(e);
                }
            }
        }

        Ok(())
    }

    /// Replace old pages with new pages in a file
    pub fn replace<R: Read + Write + Seek + 'static>(
        reader: &mut R,
        old_pages: &[OggPage],
        new_pages: Vec<OggPage>,
    ) -> Result<()> {
        if old_pages.is_empty() || new_pages.is_empty() {
            return Err(AudexError::InvalidData(
                "empty pages list not allowed".to_string(),
            ));
        }

        let mut updated_pages = new_pages;

        // Number the new pages starting from the first old page
        let first_sequence = old_pages[0].sequence;
        for (i, page) in updated_pages.iter_mut().enumerate() {
            page.sequence = first_sequence.checked_add(i as u32).ok_or_else(|| {
                AudexError::ParseError("Ogg page sequence number overflow".to_string())
            })?;
            page.serial = old_pages[0].serial;
        }

        // Copy flags from old pages
        updated_pages[0].set_first(old_pages[0].first());
        updated_pages[0].set_last(old_pages[0].last());
        updated_pages[0].set_continued(old_pages[0].continued());

        // Only copy BOS to the last page when it IS the first page
        // (i.e. single-page replacement).  When comments expand across
        // multiple pages, BOS must never appear on a non-initial page.
        let old_last = old_pages
            .last()
            .ok_or_else(|| AudexError::InternalError("old_pages empty in replace".to_string()))?;
        let old_last_first = old_last.first();
        let old_last_last = old_last.last();
        let old_last_complete = old_last.is_complete();

        if updated_pages.len() == 1 {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .set_first(old_last_first);
        } else {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .set_first(false);
        }
        updated_pages
            .last_mut()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
            .set_last(old_last_last);
        updated_pages
            .last_mut()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
            .set_complete(old_last_complete);

        // Handle incomplete single-packet pages
        let last_page = updated_pages
            .last()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?;
        if !last_page.is_complete() && last_page.packets.len() == 1 {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .position = -1;
        }

        // Write new page data
        let new_data: Result<Vec<Vec<u8>>> = updated_pages.iter().map(|p| p.write()).collect();
        let new_data = new_data?;

        // Add dummy data or merge remaining data for different page counts
        let mut final_data = new_data;
        let pages_diff = old_pages.len() as i64 - final_data.len() as i64;

        if pages_diff > 0 {
            // More old pages than new - add empty data
            for _ in 0..pages_diff {
                final_data.push(Vec::new());
            }
        } else if pages_diff < 0 {
            // When new pages exceed old pages, merge excess page data into the last
            // old-page slot so the file replacement sees exactly old_pages.len() entries.
            let split_idx = old_pages.len().saturating_sub(1);
            if split_idx < final_data.len() {
                let merged: Vec<u8> = final_data.drain(split_idx..).flatten().collect();
                final_data.push(merged);
            }
        }

        // Replace pages - handle case where we have more new pages than old pages
        #[allow(unused_assignments)]
        let mut offset_adjust: i64 = 0;
        let mut new_data_end: Option<u64> = None;
        let min_pages = old_pages.len().min(final_data.len());

        // First, replace existing old pages with corresponding new data
        for i in 0..min_pages {
            let old_page = &old_pages[i];
            let data = &final_data[i];

            if let Some(offset) = old_page.offset {
                let adjusted = offset.checked_add(offset_adjust).ok_or_else(|| {
                    AudexError::InvalidData("Page offset arithmetic overflow".to_string())
                })?;
                if adjusted < 0 {
                    return Err(AudexError::InvalidData(format!(
                        "Adjusted page offset is negative: {}",
                        adjusted
                    )));
                }
                let adjusted_offset = adjusted as u64;
                let data_size = data.len() as u64;
                let old_size = old_page.size() as u64;

                // Resize the area in the file
                resize_bytes(reader, old_size, data_size, adjusted_offset)?;

                // Write the new data
                reader.seek(SeekFrom::Start(adjusted_offset))?;
                reader.write_all(data)?;

                new_data_end = Some(adjusted_offset + data_size);
                // Use checked conversion to avoid wrapping on extremely large sizes
                let data_i64 = i64::try_from(data_size).map_err(|_| {
                    AudexError::InvalidData("Page data size exceeds i64 range".to_string())
                })?;
                let old_i64 = i64::try_from(old_size).map_err(|_| {
                    AudexError::InvalidData("Old page size exceeds i64 range".to_string())
                })?;
                // Accumulate offset using checked arithmetic to prevent overflow
                let delta = data_i64.checked_sub(old_i64).ok_or_else(|| {
                    AudexError::InvalidData(
                        "page size delta overflow during offset calculation".to_string(),
                    )
                })?;
                offset_adjust = offset_adjust.checked_add(delta).ok_or_else(|| {
                    AudexError::InvalidData(
                        "cumulative page offset adjustment overflow".to_string(),
                    )
                })?;
            }
        }

        // Now handle additional new pages (if any)
        if final_data.len() > old_pages.len() {
            if let Some(insert_offset) = new_data_end {
                for data in &final_data[min_pages..] {
                    let data_size = data.len() as u64;

                    if !data.is_empty() {
                        // Insert space for this page
                        insert_bytes(reader, data_size, insert_offset, None)?;

                        // Write the data
                        reader.seek(SeekFrom::Start(insert_offset))?;
                        reader.write_all(data)?;

                        new_data_end = Some(insert_offset + data_size);
                    }
                }
            }
        }

        // Renumber remaining pages if page count changed
        if old_pages.len() != updated_pages.len() {
            if let Some(end_offset) = new_data_end {
                reader.seek(SeekFrom::Start(end_offset))?;
                let last_updated = updated_pages.last().ok_or_else(|| {
                    AudexError::InternalError("updated_pages empty during renumber".to_string())
                })?;
                let serial = last_updated.serial;
                let next_sequence = last_updated.sequence.checked_add(1).ok_or_else(|| {
                    AudexError::InvalidData(
                        "Ogg page sequence counter overflow before renumber".to_string(),
                    )
                })?;
                Self::renumber(reader, serial, next_sequence)?;
            }
        }

        Ok(())
    }
}

/// Base Ogg file implementation
///
/// Represents a complete Ogg file with all its pages and logical streams.
/// This type provides high-level access to the structure of an Ogg container,
/// allowing you to work with multiple multiplexed streams.
///
/// # Structure
///
/// - **pages**: All Ogg pages in the file, in order
/// - **streams**: Logical streams indexed by serial number, with packets extracted
///
/// # Examples
///
/// ```no_run
/// use audex::ogg::OggFile;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Load an Ogg file
/// let ogg = OggFile::load("/path/to/audio.ogg")?;
///
/// // Iterate through streams
/// for (serial, stream) in &ogg.streams {
///     println!("Stream {}: {} codec, {} packets",
///              serial, stream.codec, stream.packets.len());
/// }
///
/// // Find Vorbis stream
/// if let Some(vorbis) = ogg.get_stream_by_codec("vorbis") {
///     println!("Found Vorbis stream with {} packets", vorbis.packets.len());
/// }
///
/// // Get pages for a specific stream
/// let stream_pages = ogg.get_pages_for_stream(12345);
/// println!("Found {} pages for stream 12345", stream_pages.len());
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct OggFile {
    /// All pages in the file, in the order they appear
    pub pages: Vec<OggPage>,

    /// Logical streams indexed by serial number
    /// Each stream contains extracted packets and codec information
    pub streams: HashMap<u32, OggStream>,
}

impl OggFile {
    /// Create a new empty OggFile with no pages or streams.
    pub fn new() -> Self {
        Self {
            pages: Vec::new(),
            streams: HashMap::new(),
        }
    }

    /// Load Ogg file from path
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        use std::fs::File;
        use std::io::BufReader;

        let file = File::open(path)?;
        let mut reader = BufReader::new(file);

        let mut ogg_file = Self::new();
        let mut pages_scanned = 0u32;
        let limits = ParseLimits::default();
        let mut cumulative_bytes: u64 = 0;

        loop {
            // Cap the number of pages to prevent excessive scanning on
            // malformed files with many tiny (e.g., zero-segment) pages
            if pages_scanned >= MAX_OGG_PAGES {
                break;
            }

            match OggPage::from_reader(&mut reader) {
                Ok(page) => {
                    pages_scanned += 1;

                    // Track cumulative allocation across all pages to prevent
                    // a crafted file from exhausting memory. Each page's segment
                    // and packet data counts toward the global tag-size budget.
                    let page_bytes: u64 = page.packets.iter().map(|pkt| pkt.len() as u64).sum();
                    cumulative_bytes = cumulative_bytes.saturating_add(page_bytes);

                    if cumulative_bytes > limits.max_tag_size {
                        return Err(AudexError::InvalidData(format!(
                            "OGG cumulative page data ({} bytes) exceeds configured limit ({} bytes)",
                            cumulative_bytes, limits.max_tag_size
                        )));
                    }

                    let serial = page.serial;

                    // Add to streams
                    let stream = ogg_file
                        .streams
                        .entry(serial)
                        .or_insert_with(|| OggStream::new(serial));

                    // Copy packets into the stream for sequential access (codec
                    // detection, identification_packet(), comment_packet(), etc.).
                    // Pages also retain their own packet data for round-trip
                    // serialization fidelity, so both copies are required.
                    for packet in &page.packets {
                        stream.packets.push(packet.clone());
                    }

                    ogg_file.pages.push(page);
                }
                Err(e) => {
                    if let AudexError::Io(io_err) = &e {
                        if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
                            break; // Normal end of file
                        }
                    }
                    return Err(e);
                }
            }
        }

        // Detect codecs for all streams
        for stream in ogg_file.streams.values_mut() {
            stream.detect_codec();
        }

        Ok(ogg_file)
    }

    /// Get first stream with specific codec
    pub fn get_stream_by_codec(&self, codec: &str) -> Option<&OggStream> {
        self.streams.values().find(|stream| stream.codec == codec)
    }

    /// Get pages for a specific stream
    pub fn get_pages_for_stream(&self, serial: u32) -> Vec<&OggPage> {
        self.pages
            .iter()
            .filter(|page| page.serial == serial)
            .collect()
    }

    /// Reconstruct packets from pages for a specific stream
    pub fn get_packets(&self, serial: u32) -> Result<Vec<Vec<u8>>> {
        let pages: Vec<OggPage> = self
            .pages
            .iter()
            .filter(|page| page.serial == serial)
            .cloned()
            .collect();

        OggPage::to_packets(&pages, false)
    }
}

/// Ogg logical stream
///
/// Represents a single logical bitstream within an Ogg container file.
/// Each stream is identified by a unique serial number and contains packets
/// for a specific codec (Vorbis, Opus, FLAC, Theora, etc.).
///
/// # Structure
///
/// - **serial_number**: Unique identifier for this stream
/// - **codec**: Detected codec name (e.g., "vorbis", "opus", "flac", "theora")
/// - **packets**: All packets belonging to this stream
///
/// The codec is automatically detected by examining the first packet's header.
///
/// # Packet Types
///
/// Most Ogg codecs use a three-packet header structure:
/// 1. **Identification packet**: Codec signature and basic parameters
/// 2. **Comment packet**: Metadata and tags (Vorbis Comments)
/// 3. **Setup packet**: Codec-specific configuration (if needed)
///
/// # Examples
///
/// ```no_run
/// use audex::ogg::OggFile;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let ogg = OggFile::load("/path/to/audio.ogg")?;
///
/// for (serial, stream) in &ogg.streams {
///     println!("Stream {}:", serial);
///     println!("  Codec: {}", stream.codec);
///     println!("  Packets: {}", stream.packets.len());
///
///     // Access identification packet
///     if let Some(id_packet) = stream.identification_packet() {
///         println!("  ID packet size: {} bytes", id_packet.len());
///     }
///
///     // Access comment packet (metadata)
///     if let Some(comment_packet) = stream.comment_packet() {
///         println!("  Comment packet size: {} bytes", comment_packet.len());
///     }
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct OggStream {
    /// Unique serial number for this logical stream
    pub serial_number: u32,

    /// Codec name detected from the identification packet
    /// Common values: "vorbis", "opus", "flac", "theora", "speex"
    pub codec: String,

    /// All packets belonging to this stream, extracted from pages
    pub packets: Vec<Vec<u8>>,
}

impl OggStream {
    /// Create new stream
    pub fn new(serial: u32) -> Self {
        Self {
            serial_number: serial,
            codec: String::new(),
            packets: Vec::new(),
        }
    }

    /// Detect codec from first packet
    pub fn detect_codec(&mut self) {
        if let Some(first_packet) = self.packets.first() {
            if first_packet.len() >= 8 && first_packet.starts_with(b"\x01vorbis") {
                self.codec = "vorbis".to_string();
            } else if first_packet.len() >= 8 && first_packet.starts_with(b"OpusHead") {
                self.codec = "opus".to_string();
            } else if first_packet.len() >= 8 && first_packet.starts_with(b"\x80theora") {
                self.codec = "theora".to_string();
            } else if first_packet.len() >= 5 && first_packet.starts_with(b"\x7FFLAC") {
                self.codec = "flac".to_string();
            }
        }
    }

    /// Get first packet (usually contains codec identification)
    pub fn identification_packet(&self) -> Option<&Vec<u8>> {
        self.packets.first()
    }

    /// Get comment packet (usually second packet)
    pub fn comment_packet(&self) -> Option<&Vec<u8>> {
        self.packets.get(1)
    }

    /// Get setup packet (usually third packet for some codecs)
    pub fn setup_packet(&self) -> Option<&Vec<u8>> {
        self.packets.get(2)
    }
}

/// Base Ogg stream information
///
/// Contains audio stream information extracted from an Ogg logical bitstream.
/// This struct implements the `StreamInfo` trait, providing a standardized
/// interface for accessing audio properties.
///
/// # Fields
///
/// - **length**: Total duration of the audio stream
/// - **bitrate**: Average bitrate in bits per second (may be VBR)
/// - **sample_rate**: Audio sample rate in Hz (e.g., 44100, 48000)
/// - **channels**: Number of audio channels (1 = mono, 2 = stereo, etc.)
/// - **serial**: Serial number of the logical stream
///
/// # Examples
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
/// use audex::StreamInfo;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let vorbis = OggVorbis::load("/path/to/audio.ogg")?;
///
/// // Access stream information (info is Option<OggVorbisInfo>)
/// if let Some(ref info) = vorbis.info {
///     if let Some(duration) = info.length() {
///         println!("Duration: {:?}", duration);
///     }
///
///     if let Some(bitrate) = info.bitrate() {
///         println!("Bitrate: {} kbps", bitrate / 1000);
///     }
///
///     if let Some(sample_rate) = info.sample_rate() {
///         println!("Sample rate: {} Hz", sample_rate);
///     }
///
///     if let Some(channels) = info.channels() {
///         println!("Channels: {}", channels);
///     }
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Default)]
pub struct OggStreamInfo {
    /// Duration of the audio stream
    pub length: Option<Duration>,

    /// Average bitrate in bits per second
    pub bitrate: Option<u32>,

    /// Sample rate in Hz
    pub sample_rate: u32,

    /// Number of audio channels
    pub channels: u16,

    /// Logical stream serial number
    pub serial: u32,
}

impl StreamInfo for OggStreamInfo {
    fn length(&self) -> Option<Duration> {
        self.length
    }

    fn bitrate(&self) -> Option<u32> {
        self.bitrate
    }

    fn sample_rate(&self) -> Option<u32> {
        if self.sample_rate > 0 {
            Some(self.sample_rate)
        } else {
            None
        }
    }

    fn channels(&self) -> Option<u16> {
        if self.channels > 0 {
            Some(self.channels)
        } else {
            None
        }
    }

    fn bits_per_sample(&self) -> Option<u16> {
        None // Varies by codec
    }
}

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

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

impl PartialEq for OggPage {
    /// Two Ogg pages are equal if they write the same data
    fn eq(&self, other: &Self) -> bool {
        match (self.write(), other.write()) {
            (Ok(a), Ok(b)) => a == b,
            _ => false,
        }
    }
}

/// Seek backwards from end position in an async reader
///
/// Seeks to an offset from the end of the file, ensuring we don't seek
/// before the start of the file.
#[cfg(feature = "async")]
pub async fn seek_end_async<F: AsyncRead + AsyncSeek + Unpin>(
    fileobj: &mut F,
    offset: u64,
) -> Result<()> {
    fileobj.seek(SeekFrom::End(0)).await?;
    let filesize = fileobj.stream_position().await?;
    let seek_pos = filesize.saturating_sub(offset);
    fileobj.seek(SeekFrom::Start(seek_pos)).await?;
    Ok(())
}

#[cfg(feature = "async")]
impl OggPage {
    /// Parse Ogg page from async reader
    ///
    /// Reads and parses an Ogg page from the current position in an async reader.
    /// This is the async equivalent of `OggPage::from_reader`.
    ///
    /// # Arguments
    ///
    /// * `reader` - Async reader positioned at the start of an Ogg page
    ///
    /// # Returns
    ///
    /// The parsed `OggPage` or an error if the data is invalid
    pub async fn from_reader_async<R: AsyncRead + AsyncSeek + Unpin>(
        reader: &mut R,
    ) -> Result<Self> {
        let offset = reader.stream_position().await.ok().map(|o| o as i64);

        // Read header (27 bytes) - standard Ogg page header size
        let mut header = [0u8; 27];
        match reader.read_exact(&mut header).await {
            Ok(_) => {}
            Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
                return Err(AudexError::from(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    "End of file",
                )));
            }
            Err(e) => return Err(AudexError::from(e)),
        }

        // Validate OggS magic signature
        if &header[0..4] != b"OggS" {
            return Err(AudexError::InvalidData(format!(
                "Invalid OGG signature: {:?}",
                &header[0..4]
            )));
        }

        let version = header[4];
        if version != 0 {
            return Err(AudexError::UnsupportedFormat(format!(
                "Unsupported OGG version: {}",
                version
            )));
        }

        // Parse header fields
        let header_type = header[5];
        let position_u64 = u64::from_le_bytes([
            header[6], header[7], header[8], header[9], header[10], header[11], header[12],
            header[13],
        ]);
        // Interpret as signed i64 via two's complement — the Ogg spec
        // treats granule position as signed, where -1 (all bits set)
        // means "no position". The `as i64` cast is a bit reinterpretation
        // and handles all values correctly, including u64::MAX → -1.
        let position = position_u64 as i64;

        let serial = u32::from_le_bytes([header[14], header[15], header[16], header[17]]);

        let sequence = u32::from_le_bytes([header[18], header[19], header[20], header[21]]);

        let checksum = u32::from_le_bytes([header[22], header[23], header[24], header[25]]);
        let segment_count = header[26];

        // Read segment table
        let mut segments = vec![0u8; segment_count as usize];
        reader.read_exact(&mut segments).await?;

        // Calculate packet boundaries and read data
        let mut packets = Vec::new();
        let mut current_packet = Vec::new();

        for &segment_size in &segments {
            let mut segment_data = vec![0u8; segment_size as usize];
            reader.read_exact(&mut segment_data).await?;
            current_packet.extend_from_slice(&segment_data);

            // Segment size < 255 indicates packet boundary
            if segment_size < 255 {
                packets.push(current_packet);
                current_packet = Vec::new();
            }
        }

        // Handle incomplete packets
        let complete = if !current_packet.is_empty() {
            packets.push(current_packet);
            false // Last packet is incomplete
        } else {
            segments.last().is_none_or(|&s| s < 255)
        };

        let page = Self {
            version,
            header_type,
            position,
            serial,
            sequence,
            checksum,
            segments,
            packets,
            offset,
            complete,
        };

        // Verify CRC32 checksum to detect corruption (same as sync path)
        let computed_crc = page.compute_read_crc()?;
        if computed_crc != checksum {
            return Err(AudexError::InvalidData(format!(
                "OGG page CRC32 mismatch: stored={:#010X}, computed={:#010X}",
                checksum, computed_crc
            )));
        }

        Ok(page)
    }

    /// Write Ogg page to async writer
    ///
    /// Serializes this page and writes it to the provided async writer.
    /// This is the async equivalent of `OggPage::write_to`.
    ///
    /// # Arguments
    ///
    /// * `writer` - Async writer to write the page data to
    ///
    /// # Returns
    ///
    /// The serialized page data as a byte vector
    pub async fn write_to_async<W: AsyncWrite + Unpin>(&self, writer: &mut W) -> Result<Vec<u8>> {
        // Use the sync write method which calculates CRC properly
        let data = self.write()?;
        writer.write_all(&data).await?;
        Ok(data)
    }

    /// Find the last page with given serial number (async version)
    ///
    /// Searches the file for the last Ogg page belonging to the specified stream.
    /// This is the async equivalent of `OggPage::find_last`.
    ///
    /// # Arguments
    ///
    /// * `reader` - Async reader to search
    /// * `serial` - Stream serial number to match
    /// * `finishing` - If true, only return pages with valid granule positions
    ///
    /// # Returns
    ///
    /// The last matching page, or None if not found
    pub async fn find_last_async<R: AsyncRead + AsyncSeek + Unpin>(
        reader: &mut R,
        serial: u32,
        finishing: bool,
    ) -> Result<Option<Self>> {
        // Fast path: check the last page first for non-multiplexed streams
        seek_end_async(reader, 256 * 256).await?;

        let mut buffer = Vec::new();
        reader.read_to_end(&mut buffer).await?;

        // Find last OggS signature in buffer
        if let Some(index) = buffer.windows(4).rposition(|w| w == b"OggS") {
            let mut cursor = std::io::Cursor::new(&buffer[index..]);
            if let Ok(page) = Self::from_reader(&mut cursor) {
                if page.serial == serial {
                    let is_valid = !finishing || page.position != -1;
                    if is_valid && page.last() {
                        return Ok(Some(page));
                    }
                }
            }
        }

        // Slow path: scan entire file for multiplexed streams.
        // Cap the number of pages to prevent excessive scanning on
        // malformed files with many tiny (e.g., zero-segment) pages.
        // This mirrors the sync find_last behavior.
        reader.seek(SeekFrom::Start(0)).await?;
        let mut best_page = None;
        let mut pages_scanned = 0u32;
        let mut cumulative_bytes: u64 = 0;
        let limits = ParseLimits::default();

        loop {
            if pages_scanned >= MAX_OGG_PAGES {
                break;
            }

            match Self::from_reader_async(reader).await {
                Ok(page) => {
                    pages_scanned += 1;
                    Self::accumulate_page_bytes_with_limit(
                        limits,
                        &mut cumulative_bytes,
                        &page,
                        "OGG cumulative page data",
                    )?;
                    if page.serial == serial {
                        let is_valid = !finishing || page.position != -1;
                        if is_valid {
                            best_page = Some(page.clone());
                        }
                        if page.last() {
                            break;
                        }
                    }
                }
                Err(e) => {
                    if let AudexError::Io(io_err) = &e {
                        if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
                            break;
                        }
                    }
                    return Ok(best_page);
                }
            }
        }

        Ok(best_page)
    }

    /// Renumber pages for a given serial starting at sequence number (async version)
    ///
    /// Updates page sequence numbers in place for pages belonging to the specified stream.
    /// This is the async equivalent of `OggPage::renumber`.
    ///
    /// # Arguments
    ///
    /// * `reader` - Async file handle with read/write access
    /// * `serial` - Stream serial number to renumber
    /// * `start_sequence` - Starting sequence number
    pub async fn renumber_async(
        reader: &mut TokioFile,
        serial: u32,
        start_sequence: u32,
    ) -> Result<()> {
        let mut sequence = start_sequence;

        loop {
            let page_offset = reader.stream_position().await?;

            match OggPage::from_reader_async(reader).await {
                Ok(mut page) => {
                    if page.serial == serial {
                        // Update sequence number
                        page.sequence = sequence;

                        // Seek back and overwrite
                        reader.seek(SeekFrom::Start(page_offset)).await?;

                        // Write updated page with recalculated CRC
                        let page_data = page.write()?;
                        reader.write_all(&page_data).await?;

                        // Seek to end of this page to continue
                        reader
                            .seek(SeekFrom::Start(page_offset + page_data.len() as u64))
                            .await?;

                        sequence = sequence.checked_add(1).ok_or_else(|| {
                            AudexError::InvalidData(
                                "Ogg page sequence counter overflow".to_string(),
                            )
                        })?;
                    }
                }
                Err(e) => {
                    if let AudexError::Io(io_err) = &e {
                        if io_err.kind() == std::io::ErrorKind::UnexpectedEof {
                            break; // Normal end of file
                        }
                    }
                    return Err(e);
                }
            }
        }

        Ok(())
    }

    /// Replace old pages with new pages in a file (async version)
    ///
    /// Replaces a sequence of Ogg pages with new pages, adjusting file size as needed.
    /// This is the async equivalent of `OggPage::replace`.
    ///
    /// # Arguments
    ///
    /// * `reader` - Async file handle with read/write access
    /// * `old_pages` - Pages to replace
    /// * `new_pages` - Replacement pages
    pub async fn replace_async(
        reader: &mut TokioFile,
        old_pages: &[OggPage],
        new_pages: Vec<OggPage>,
    ) -> Result<()> {
        if old_pages.is_empty() || new_pages.is_empty() {
            return Err(AudexError::InvalidData(
                "empty pages list not allowed".to_string(),
            ));
        }

        let mut updated_pages = new_pages;

        // Number the new pages starting from the first old page
        let first_sequence = old_pages[0].sequence;
        for (i, page) in updated_pages.iter_mut().enumerate() {
            page.sequence = first_sequence.checked_add(i as u32).ok_or_else(|| {
                AudexError::ParseError("Ogg page sequence number overflow".to_string())
            })?;
            page.serial = old_pages[0].serial;
        }

        // Copy flags from old pages to preserve stream structure
        updated_pages[0].set_first(old_pages[0].first());
        updated_pages[0].set_last(old_pages[0].last());
        updated_pages[0].set_continued(old_pages[0].continued());

        let old_last = old_pages.last().ok_or_else(|| {
            AudexError::InternalError("old_pages empty in replace_async".to_string())
        })?;
        let old_last_first = old_last.first();
        let old_last_last = old_last.last();
        let old_last_complete = old_last.is_complete();

        // Only copy BOS to the last page when it IS the first page
        // (i.e. single-page replacement).  When comments expand across
        // multiple pages, BOS must never appear on a non-initial page.
        if updated_pages.len() == 1 {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .set_first(old_last_first);
        } else {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .set_first(false);
        }
        updated_pages
            .last_mut()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
            .set_last(old_last_last);
        updated_pages
            .last_mut()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
            .set_complete(old_last_complete);

        // Handle incomplete single-packet pages
        let last_page = updated_pages
            .last()
            .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?;
        if !last_page.is_complete() && last_page.packets.len() == 1 {
            updated_pages
                .last_mut()
                .ok_or_else(|| AudexError::InternalError("updated_pages empty".to_string()))?
                .position = -1;
        }

        // Serialize new page data
        let new_data: Result<Vec<Vec<u8>>> = updated_pages.iter().map(|p| p.write()).collect();
        let new_data = new_data?;

        // Adjust for page count differences
        let mut final_data = new_data;
        let pages_diff = old_pages.len() as i64 - final_data.len() as i64;

        if pages_diff > 0 {
            // More old pages than new - add empty data
            for _ in 0..pages_diff {
                final_data.push(Vec::new());
            }
        } else if pages_diff < 0 {
            // When new pages exceed old pages, merge excess page data into the last
            // old-page slot so the file replacement sees exactly old_pages.len() entries.
            let split_idx = old_pages.len().saturating_sub(1);
            if split_idx < final_data.len() {
                let merged: Vec<u8> = final_data.drain(split_idx..).flatten().collect();
                final_data.push(merged);
            }
        }

        // Replace pages in file
        let mut offset_adjust: i64 = 0;
        let mut new_data_end: Option<u64> = None;
        let min_pages = old_pages.len().min(final_data.len());

        // Replace existing old pages with corresponding new data
        for i in 0..min_pages {
            let old_page = &old_pages[i];
            let data = &final_data[i];

            if let Some(offset) = old_page.offset {
                let adjusted = offset.checked_add(offset_adjust).ok_or_else(|| {
                    AudexError::InvalidData("Page offset arithmetic overflow".to_string())
                })?;
                if adjusted < 0 {
                    return Err(AudexError::InvalidData(format!(
                        "Adjusted page offset is negative: {}",
                        adjusted
                    )));
                }
                let adjusted_offset = adjusted as u64;
                let data_size = data.len() as u64;
                let old_size = old_page.size() as u64;

                // Resize the file region
                resize_bytes_async(reader, old_size, data_size, adjusted_offset).await?;

                // Write the new data
                reader.seek(SeekFrom::Start(adjusted_offset)).await?;
                reader.write_all(data).await?;

                new_data_end = Some(adjusted_offset + data_size);
                // Use checked conversion to avoid wrapping on extremely large sizes
                let data_i64 = i64::try_from(data_size).map_err(|_| {
                    AudexError::InvalidData("Page data size exceeds i64 range".to_string())
                })?;
                let old_i64 = i64::try_from(old_size).map_err(|_| {
                    AudexError::InvalidData("Old page size exceeds i64 range".to_string())
                })?;
                // Accumulate offset using checked arithmetic to prevent overflow
                let delta = data_i64.checked_sub(old_i64).ok_or_else(|| {
                    AudexError::InvalidData(
                        "page size delta overflow during offset calculation".to_string(),
                    )
                })?;
                offset_adjust = offset_adjust.checked_add(delta).ok_or_else(|| {
                    AudexError::InvalidData(
                        "cumulative page offset adjustment overflow".to_string(),
                    )
                })?;
            }
        }

        // Handle additional new pages (if any)
        if final_data.len() > old_pages.len() {
            if let Some(insert_offset) = new_data_end {
                for data in &final_data[min_pages..] {
                    let data_size = data.len() as u64;

                    if !data.is_empty() {
                        // Insert space for this page
                        insert_bytes_async(reader, data_size, insert_offset, None).await?;

                        // Write the data
                        reader.seek(SeekFrom::Start(insert_offset)).await?;
                        reader.write_all(data).await?;

                        new_data_end = Some(insert_offset + data_size);
                    }
                }
            }
        }

        // Renumber remaining pages if page count changed
        if old_pages.len() != updated_pages.len() {
            if let Some(end_offset) = new_data_end {
                reader.seek(SeekFrom::Start(end_offset)).await?;
                let last_updated = updated_pages.last().ok_or_else(|| {
                    AudexError::InternalError("updated_pages empty during renumber".to_string())
                })?;
                let serial = last_updated.serial;
                let next_sequence = last_updated.sequence.checked_add(1).ok_or_else(|| {
                    AudexError::InvalidData(
                        "Ogg page sequence counter overflow before renumber".to_string(),
                    )
                })?;
                Self::renumber_async(reader, serial, next_sequence).await?;
            }
        }

        reader.flush().await?;
        Ok(())
    }
}