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
//! # Ogg Vorbis Format Support
//!
//! This module provides comprehensive support for reading and writing Ogg Vorbis audio files.
//! Ogg Vorbis is a free, open-source lossy audio codec offering high quality compression,
//! widely used for music streaming and distribution.
//!
//! ## Overview
//!
//! Vorbis audio is wrapped in an Ogg container format, which provides:
//! - **Streaming support**: Efficient sequential reading
//! - **Multiple logical streams**: Can contain multiple audio tracks
//! - **Metadata support**: Vorbis Comments for tagging
//! - **Error resilience**: Graceful degradation with corrupted data
//!
//! This module uses the first Vorbis stream found in the Ogg bitstream.
//!
//! ## File Format
//!
//! Ogg Vorbis files typically use the `.ogg` extension and consist of:
//! 1. **Identification header**: Codec version, channels, sample rate
//! 2. **Comment header**: Vorbis Comments (tags/metadata)
//! 3. **Setup header**: Codec configuration
//! 4. **Audio packets**: Compressed audio data
//!
//! ## Audio Characteristics
//!
//! - **Lossy compression**: Smaller file sizes than lossless formats
//! - **Variable bitrate (VBR)**: Optimizes quality per audio complexity
//! - **Sample rates**: Typically 8kHz to 192kHz
//! - **Channels**: Mono, stereo, or multichannel (up to 255 channels)
//!
//! ## Tagging
//!
//! Ogg Vorbis uses Vorbis Comments for metadata, which support:
//! - Human-readable field names (TITLE, ARTIST, ALBUM, etc.)
//! - Multiple values per field
//! - Case-insensitive field names
//! - UTF-8 encoded values
//!
//! ## Examples
//!
//! ### Loading and reading file information
//!
//! ```no_run
//! use audex::oggvorbis::OggVorbis;
//! use audex::FileType;
//!
//! let vorbis = OggVorbis::load("song.ogg").unwrap();
//!
//! if let Some(ref info) = vorbis.info {
//!     println!("Sample rate: {} Hz", info.sample_rate);
//!     println!("Channels: {}", info.channels);
//!     println!("Duration: {:?}", info.length);
//!
//!     // Bitrate values are in bits per second, divide by 1000 for kbps
//!     if let Some(bitrate) = info.bitrate {
//!         println!("Bitrate: {} kbps", bitrate / 1000);
//!     }
//!
//!     if let Some(nominal) = info.nominal_bitrate {
//!         println!("Nominal bitrate: {} kbps", nominal / 1000);
//!     }
//! }
//! ```
//!
//! ### Reading and modifying tags
//!
//! ```no_run
//! use audex::oggvorbis::OggVorbis;
//! use audex::FileType;
//! use audex::Tags;
//!
//! let mut vorbis = OggVorbis::load("song.ogg").unwrap();
//!
//! if let Some(ref mut tags) = vorbis.tags {
//!     // Read existing tags
//!     if let Some(title) = tags.get("TITLE") {
//!         println!("Title: {}", title[0]);
//!     }
//!
//!     // Modify tags using set for Vorbis Comments
//!     tags.set("TITLE", vec!["New Title".to_string()]);
//!     tags.set("ARTIST", vec!["Artist Name".to_string()]);
//!     tags.set("ALBUM", vec!["Album Name".to_string()]);
//!     tags.set("DATE", vec!["2024".to_string()]);
//! }
//!
//! vorbis.save().unwrap();
//! ```
//!
//! ### Creating tags if they don't exist
//!
//! ```no_run
//! use audex::oggvorbis::OggVorbis;
//! use audex::FileType;
//!
//! let mut vorbis = OggVorbis::load("song.ogg").unwrap();
//!
//! if vorbis.tags.is_none() {
//!     vorbis.add_tags().unwrap();
//! }
//!
//! if let Some(ref mut tags) = vorbis.tags {
//!     tags.set("TITLE", vec!["Title".to_string()]);
//! }
//!
//! vorbis.save().unwrap();
//! ```
//!
//! ### Working with multiple values
//!
//! ```no_run
//! use audex::oggvorbis::OggVorbis;
//! use audex::FileType;
//! use audex::Tags;
//!
//! let mut vorbis = OggVorbis::load("song.ogg").unwrap();
//!
//! if let Some(ref mut tags) = vorbis.tags {
//!     // Add multiple artists using set
//!     tags.set("ARTIST", vec![
//!         "Artist One".to_string(),
//!         "Artist Two".to_string(),
//!     ]);
//!
//!     // Read all values
//!     if let Some(artists) = tags.get("ARTIST") {
//!         for artist in artists {
//!             println!("Artist: {}", artist);
//!         }
//!     }
//! }
//!
//! vorbis.save().unwrap();
//! ```
//!
//! ## References
//!
//! - Ogg Vorbis specification: <http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html>
//! - Vorbis website: <http://vorbis.com/>
//! - Ogg container format: <http://www.xiph.org/ogg/>

use crate::VERSION_STRING;
use crate::ogg::OggPage;
use crate::vorbis::VCommentDict;
use crate::{AudexError, FileType, Result, StreamInfo};
use byteorder::{LittleEndian, ReadBytesExt};
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Maximum number of OGG pages to read when searching for a specific packet.
/// Prevents OOM from malicious files with many small pages.
const MAX_PAGE_SEARCH: usize = 1024;

#[cfg(feature = "async")]
use tokio::fs::{File as TokioFile, OpenOptions as TokioOpenOptions};
#[cfg(feature = "async")]
use tokio::io::{AsyncSeekExt, BufReader as TokioBufReader};

/// Error type for Ogg Vorbis file operations.
///
/// This error occurs during parsing, reading, or writing Ogg Vorbis files.
/// It covers general Vorbis errors, I/O failures, and invalid file data.
///
/// # Variants
///
/// - **General**: General Ogg Vorbis processing errors
/// - **Io**: File system I/O errors during read/write operations
/// - **InvalidData**: Malformed or corrupted Vorbis data
///
/// # Common Causes
///
/// - Corrupted or incomplete Ogg Vorbis files
/// - Invalid header packets
/// - Malformed Vorbis Comment data
/// - I/O errors during file access
/// - Unsupported Vorbis encoder settings
///
/// # Examples
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// match OggVorbis::load("corrupted.ogg") {
///     Ok(vorbis) => println!("Loaded successfully"),
///     Err(e) => eprintln!("Failed to load: {}", e),
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum OggVorbisError {
    #[error("Ogg Vorbis error: {0}")]
    General(String),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Invalid data: {0}")]
    InvalidData(String),
}

/// Error type for Vorbis header parsing failures.
///
/// This error specifically occurs when parsing Vorbis stream headers
/// (identification, comment, or setup headers) that are malformed or invalid.
///
/// # Variants
///
/// - **InvalidHeader**: Header packet is malformed or doesn't meet specification
/// - **Io**: I/O error while reading header data
///
/// # Common Causes
///
/// - Invalid Vorbis identification header signature
/// - Sample rate set to zero (invalid per specification)
/// - Channel count set to zero
/// - Truncated header packets
/// - Incorrect packet type indicators
/// - File is not a valid Ogg Vorbis file
///
/// # Examples
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// match OggVorbis::load("invalid_header.ogg") {
///     Ok(vorbis) => println!("Valid Vorbis file"),
///     Err(e) => eprintln!("Header parsing failed: {}", e),
/// }
/// ```
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum OggVorbisHeaderError {
    #[error("Header error: {0}")]
    InvalidHeader(String),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

impl From<OggVorbisError> for AudexError {
    fn from(err: OggVorbisError) -> Self {
        match err {
            OggVorbisError::General(msg) => AudexError::InvalidData(msg),
            OggVorbisError::Io(e) => AudexError::Io(e),
            OggVorbisError::InvalidData(msg) => AudexError::InvalidData(msg),
        }
    }
}

impl From<OggVorbisHeaderError> for AudexError {
    fn from(err: OggVorbisHeaderError) -> Self {
        match err {
            OggVorbisHeaderError::InvalidHeader(msg) => AudexError::InvalidData(msg),
            OggVorbisHeaderError::Io(e) => AudexError::Io(e),
        }
    }
}

/// Vorbis comments embedded in an Ogg bitstream
#[derive(Debug, Clone)]
pub struct OggVCommentDict {
    inner: VCommentDict,
}

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

impl OggVCommentDict {
    /// Create new empty Vorbis comments
    pub fn new() -> Self {
        Self {
            inner: VCommentDict::new(),
        }
    }

    /// Create from file object and info
    pub fn from_fileobj<R: Read + Seek>(fileobj: &mut R, info: &OggVorbisInfo) -> Result<Self> {
        let mut pages = Vec::new();
        let mut complete = false;
        let mut pages_read = 0usize;
        let mut cumulative_bytes = 0u64;
        let limits = crate::limits::ParseLimits::default();

        while !complete {
            let page = OggPage::from_reader(fileobj)?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for comment packet".to_string(),
                ));
            }
            if page.serial == info.serial {
                OggPage::accumulate_page_bytes_with_limit(
                    limits,
                    &mut cumulative_bytes,
                    &page,
                    "OGG Vorbis comment packet",
                )?;
                pages.push(page.clone());
                complete = page.is_complete() || page.packets.len() > 1;
            }
        }

        let packets = OggPage::to_packets(&pages, false)?;
        if packets.is_empty() || packets[0].len() < 7 {
            return Err(AudexError::InvalidData(
                "Invalid Vorbis comment packet".to_string(),
            ));
        }

        // Verify the packet starts with the Vorbis comment header
        // (type byte 0x03 followed by "vorbis") before stripping it.
        if &packets[0][..7] != b"\x03vorbis" {
            return Err(AudexError::InvalidData(format!(
                "Expected Vorbis comment header (\\x03vorbis), got {:?}",
                &packets[0][..7]
            )));
        }
        let data = &packets[0][7..];

        let inner =
            VCommentDict::from_bytes_with_options(data, crate::vorbis::ErrorMode::Replace, true)?;

        // Store original data and calculate padding
        let _original_data = Some(data.to_vec());
        // Calculate size with framing bit to match actual written size
        let mut size_buffer = Vec::new();
        inner.write(&mut size_buffer, Some(true))?;
        let _vcomment_size = size_buffer.len();
        let _original_padding = data.len().saturating_sub(_vcomment_size);

        Ok(Self { inner })
    }

    /// Inject tags into the file
    pub fn inject<R: Read + Write + Seek + 'static>(
        &self,
        fileobj: &mut R,
        padding_func: Option<fn(&crate::tags::PaddingInfo) -> i64>,
    ) -> Result<()> {
        // Find the old pages in the file; we'll need to remove them,
        // plus grab any stray setup packet data out of them.
        fileobj.seek(SeekFrom::Start(0))?;

        // Read the first page to obtain the stream serial number.
        // In a multiplexed Ogg file multiple logical streams share the
        // container, so we must filter by serial when searching for the
        // Vorbis comment header to avoid matching a different stream.
        let first_page = OggPage::from_reader(fileobj)?;
        let stream_serial = first_page.serial;
        let mut page = first_page;
        let mut pages_read = 1usize;
        while page.packets.is_empty()
            || !page.packets[0].starts_with(b"\x03vorbis")
            || page.serial != stream_serial
        {
            page = OggPage::from_reader(fileobj)?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for comment header".to_string(),
                ));
            }
        }

        let mut old_pages = vec![page];
        // Collect all pages belonging to the comment packet
        loop {
            let last_page = old_pages.last().ok_or_else(|| {
                AudexError::InvalidData(
                    "expected non-empty page list while reading comments".into(),
                )
            })?;
            if last_page.is_complete() || last_page.packets.len() > 1 {
                break;
            }
            let page = OggPage::from_reader(fileobj)?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while reading comment pages".to_string(),
                ));
            }
            if page.serial == old_pages[0].serial {
                old_pages.push(page);
            }
        }

        let packets = OggPage::to_packets(&old_pages, false)?;
        if packets.is_empty() {
            return Err(AudexError::InvalidData("No packets found".to_string()));
        }

        // Calculate content size (approximate) - file size minus first packet
        // get_size preserves file position, so we must do the same
        let content_size = {
            let old_pos = fileobj.stream_position()?;
            let file_size = fileobj.seek(SeekFrom::End(0))?;
            fileobj.seek(SeekFrom::Start(old_pos))?; // Restore position
            // Use saturating subtraction to prevent overflow on large or crafted values
            i64::try_from(file_size)
                .unwrap_or(i64::MAX)
                .saturating_sub(i64::try_from(packets[0].len()).unwrap_or(0))
        };

        // Create Vorbis comment data
        let vcomment_data = {
            let mut data = b"\x03vorbis".to_vec();
            let mut vcomment_bytes = Vec::new();

            // Create a copy of our inner VCommentDict
            let mut comment_to_write = self.inner.clone();
            // Set vendor string based on whether there are actual tags to write
            if !comment_to_write.keys().is_empty() {
                comment_to_write.set_vendor(format!("Audex {}", VERSION_STRING));
            }

            // Use framing=true by default
            comment_to_write.write(&mut vcomment_bytes, Some(true))?;
            data.extend_from_slice(&vcomment_bytes);
            data
        };
        let padding_left = packets[0].len() as i64 - vcomment_data.len() as i64;

        // Use PaddingInfo: info = PaddingInfo(padding_left, content_size)
        let info = crate::tags::PaddingInfo::new(padding_left, content_size);
        // Calculate new_padding = info.get_padding_with(padding_func)
        let new_padding = info.get_padding_with(padding_func);

        // Set the new comment packet with proper padding - matches packets[0] = vcomment_data + b"\x00" * new_padding
        let mut new_packets = packets;
        new_packets[0] = vcomment_data;
        // Negative padding indicates the content exceeds the available space;
        // clamp to zero rather than silently discarding data.
        let padding_bytes = if new_padding < 0 {
            0usize
        } else {
            usize::try_from(new_padding).unwrap_or(0)
        };
        if padding_bytes > 0 {
            new_packets[0].extend_from_slice(&vec![0u8; padding_bytes]);
        }

        // Create new pages using _from_packets_try_preserve
        let new_pages = OggPage::from_packets_try_preserve(new_packets.clone(), &old_pages);

        let final_pages = if new_pages.is_empty() {
            // Fallback to regular from_packets - preserve original granule position
            let first_sequence = old_pages[0].sequence;

            let raw_position = old_pages
                .last()
                .ok_or_else(|| AudexError::InvalidData("no comment pages found".to_string()))?
                .position;
            // Per the Ogg spec, granule position -1 means "no granule position set"
            // for this page. Use 0 for comment header pages in this case.
            let original_granule = if raw_position < 0 {
                0u64
            } else {
                raw_position as u64
            };
            OggPage::from_packets_with_options(
                new_packets,
                first_sequence,
                4096,
                2048,
                original_granule,
            )?
        } else {
            new_pages
        };

        if final_pages.is_empty() {
            return Err(AudexError::InvalidData(
                "Failed to create new OGG pages".to_string(),
            ));
        }

        OggPage::replace(fileobj, &old_pages, final_pages)?;

        Ok(())
    }
}

// Delegate VCommentDict methods to inner
impl std::ops::Deref for OggVCommentDict {
    type Target = VCommentDict;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl std::ops::DerefMut for OggVCommentDict {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

// Implement Tags trait by delegating to inner VCommentDict
use crate::Tags;
impl Tags for OggVCommentDict {
    fn get(&self, key: &str) -> Option<&[String]> {
        self.inner.get(key)
    }

    fn set(&mut self, key: &str, values: Vec<String>) {
        self.inner.set(key, values)
    }

    fn remove(&mut self, key: &str) {
        self.inner.remove(key)
    }

    fn keys(&self) -> Vec<String> {
        self.inner.keys()
    }

    fn pprint(&self) -> String {
        self.inner.pprint()
    }
}

/// Main structure representing an Ogg Vorbis audio file.
///
/// This is the primary interface for reading and writing Ogg Vorbis files,
/// providing access to both audio stream information and Vorbis Comment metadata.
///
/// # Structure
///
/// - **`info`**: Optional audio stream information (sample rate, channels, bitrate, duration)
/// - **`tags`**: Optional Vorbis Comments for metadata (title, artist, album, etc.)
/// - **`filename`**: Internal file path used for save operations
///
/// # File Format
///
/// Ogg Vorbis uses the Ogg container format with Vorbis-encoded audio:
/// - **Extension**: `.ogg` (standard), `.oga` (audio-specific)
/// - **MIME type**: `audio/ogg`, `audio/vorbis`
/// - **Codec**: Vorbis (lossy compression with VBR support)
///
/// # Examples
///
/// ## Loading and reading file information
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// // Access stream information
/// if let Some(ref info) = vorbis.info {
///     println!("Sample rate: {} Hz", info.sample_rate);
///     println!("Channels: {}", info.channels);
///
///     if let Some(bitrate) = info.bitrate {
///         println!("Bitrate: {} kbps", bitrate / 1000);
///     }
/// }
/// ```
///
/// ## Reading and modifying tags
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::{FileType, Tags};
///
/// let mut vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// // Read existing tags using the Tags trait
/// if let Some(ref tags) = vorbis.tags {
///     if let Some(title) = tags.get("TITLE") {
///         println!("Current title: {}", title[0]);
///     }
/// }
///
/// // Modify tags using set for Vorbis Comments
/// if let Some(ref mut tags) = vorbis.tags {
///     tags.set("TITLE", vec!["New Title".to_string()]);
///     tags.set("ARTIST", vec!["Artist Name".to_string()]);
///     tags.set("ALBUM", vec!["Album Name".to_string()]);
///     tags.set("DATE", vec!["2024".to_string()]);
/// }
///
/// // Save changes back to file
/// vorbis.save().unwrap();
/// ```
///
/// ## Creating tags if they don't exist
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let mut vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// // Add tags if file has none
/// if vorbis.tags.is_none() {
///     vorbis.add_tags().unwrap();
///  }
///
/// if let Some(ref mut tags) = vorbis.tags {
///     tags.set("TITLE", vec!["New Song".to_string()]);
/// }
///
/// vorbis.save().unwrap();
/// ```
///
/// ## Working with multiple tag values
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let mut vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// if let Some(ref mut tags) = vorbis.tags {
///     // Add multiple artists (featured artists) using set
///     tags.set("ARTIST", vec![
///         "Primary Artist".to_string(),
///         "Featured Artist".to_string(),
///     ]);
///
///     // Add multiple genres
///     tags.set("GENRE", vec![
///         "Rock".to_string(),
///         "Alternative".to_string(),
///     ]);
/// }
///
/// vorbis.save().unwrap();
/// ```
///
/// ## Removing all metadata
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let mut vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// // Clear all tags
/// vorbis.clear().unwrap();
///
/// println!("All metadata removed");
/// ```
#[derive(Debug, Default)]
pub struct OggVorbis {
    /// Stream information
    pub info: Option<OggVorbisInfo>,
    /// Vorbis comments embedded in Ogg bitstream
    pub tags: Option<OggVCommentDict>,
    /// File path for saving operations
    filename: Option<PathBuf>,
}

/// Audio stream information for Ogg Vorbis files.
///
/// Contains technical details about the Vorbis audio stream extracted from
/// the identification header and calculated from the Ogg bitstream structure.
///
/// # Fields
///
/// - **`length`**: Total duration of the audio file
/// - **`bitrate`**: Average bitrate in bits per second (derived from identification header fields)
/// - **`sample_rate`**: Audio sample rate in Hz (typically 44100 or 48000)
/// - **`channels`**: Number of audio channels (1=mono, 2=stereo, etc.)
/// - **`serial`**: Ogg logical bitstream serial number (unique stream identifier)
/// - **`version`**: Vorbis encoder version (should be 0 for standard Vorbis)
/// - **`max_bitrate`**: Maximum instantaneous bitrate hint (optional, encoder-specific)
/// - **`nominal_bitrate`**: Target/nominal bitrate hint (optional, encoder-specific)
/// - **`min_bitrate`**: Minimum instantaneous bitrate hint (optional, encoder-specific)
///
/// # Bitrate Information
///
/// Vorbis uses variable bitrate (VBR) encoding by default. The bitrate fields provide hints:
/// - **bitrate**: Derived from the identification header's nominal/max/min bitrate fields
/// - **nominal_bitrate**: Encoder's target quality setting
/// - **max_bitrate/min_bitrate**: Bitrate constraints (often unused)
///
/// # Examples
///
/// ## Reading stream information
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// if let Some(ref info) = vorbis.info {
///     println!("Audio Format Information:");
///     println!("  Sample rate: {} Hz", info.sample_rate);
///     println!("  Channels: {}", info.channels);
///     println!("  Vorbis version: {}", info.version);
///
///     if let Some(length) = info.length {
///         let secs = length.as_secs();
///         println!("  Duration: {}:{:02}", secs / 60, secs % 60);
///     }
///
///     if let Some(bitrate) = info.bitrate {
///         println!("  Average bitrate: {} kbps", bitrate / 1000);
///     }
///
///     if let Some(nominal) = info.nominal_bitrate {
///         println!("  Nominal bitrate: {} kbps", nominal / 1000);
///     }
/// }
/// ```
///
/// ## Checking audio quality settings
///
/// ```no_run
/// use audex::oggvorbis::OggVorbis;
/// use audex::FileType;
///
/// let vorbis = OggVorbis::load("song.ogg").unwrap();
///
/// if let Some(ref info) = vorbis.info {
///     // Determine quality level based on bitrate
///     if let Some(nominal) = info.nominal_bitrate {
///         let quality = match nominal / 1000 {
///             0..=96 => "Low quality",
///             97..=128 => "Standard quality",
///             129..=192 => "High quality",
///             _ => "Very high quality",
///         };
///         println!("Encoding quality: {}", quality);
///     }
///
///     // Check if VBR constraints are set
///     if info.max_bitrate.is_some() || info.min_bitrate.is_some() {
///         println!("Bitrate constraints enabled");
///     }
/// }
/// ```
#[derive(Debug, Clone, Default)]
pub struct OggVorbisInfo {
    pub length: Option<Duration>,
    pub bitrate: Option<u32>,
    pub sample_rate: u32,
    pub channels: u16,
    pub serial: u32,
    pub version: u32,
    pub max_bitrate: Option<u32>,
    pub nominal_bitrate: Option<u32>,
    pub min_bitrate: Option<u32>,
}

impl StreamInfo for OggVorbisInfo {
    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 // Vorbis is lossy
    }
}

impl OggVorbisInfo {
    /// Parse Vorbis identification header
    pub fn from_identification_header(packet: &[u8]) -> Result<Self> {
        if packet.len() < 30 {
            return Err(AudexError::InvalidData(
                "Vorbis identification header too short".to_string(),
            ));
        }

        // Check packet type and signature
        if packet[0] != 1 || &packet[1..7] != b"vorbis" {
            return Err(AudexError::InvalidData(
                "Invalid Vorbis identification header".to_string(),
            ));
        }

        let mut cursor = Cursor::new(&packet[7..]);

        let version = cursor.read_u32::<LittleEndian>()?;
        let channels = cursor.read_u8()? as u16;
        let sample_rate = cursor.read_u32::<LittleEndian>()?;
        let max_bitrate = cursor.read_u32::<LittleEndian>()?;
        let nominal_bitrate = cursor.read_u32::<LittleEndian>()?;
        let min_bitrate = cursor.read_u32::<LittleEndian>()?;

        if sample_rate == 0 {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "sample rate can't be zero".to_string(),
            )
            .into());
        }

        if channels == 0 {
            return Err(AudexError::InvalidData(
                "Channel count cannot be zero".to_string(),
            ));
        }

        // Convert negative bitrates (0xFFFFFFFF in u32) to 0 per specification
        let max_bitrate = if max_bitrate == 0xFFFFFFFF {
            0
        } else {
            max_bitrate
        };
        let nominal_bitrate = if nominal_bitrate == 0xFFFFFFFF {
            0
        } else {
            nominal_bitrate
        };
        let min_bitrate = if min_bitrate == 0xFFFFFFFF {
            0
        } else {
            min_bitrate
        };

        // Calculate effective bitrate following standard logic.
        // Return None when no meaningful bitrate information is available
        // (all three fields are zero).
        let bitrate = if nominal_bitrate == 0 {
            // If nominal is 0, use average of max and min
            let avg = ((max_bitrate as u64 + min_bitrate as u64) / 2) as u32;
            if avg > 0 { Some(avg) } else { None }
        } else if max_bitrate > 0 && max_bitrate < nominal_bitrate {
            // If max bitrate exists and is less than nominal, use max
            Some(max_bitrate)
        } else if min_bitrate > nominal_bitrate {
            // If min bitrate is greater than nominal, use min
            Some(min_bitrate)
        } else {
            // Use nominal bitrate
            Some(nominal_bitrate)
        };

        // Convert for storage (back to Option<u32>)
        let max_bitrate_opt = if max_bitrate > 0 {
            Some(max_bitrate)
        } else {
            None
        };
        let nominal_bitrate_opt = if nominal_bitrate > 0 {
            Some(nominal_bitrate)
        } else {
            None
        };
        let min_bitrate_opt = if min_bitrate > 0 {
            Some(min_bitrate)
        } else {
            None
        };

        Ok(Self {
            length: None, // Will be calculated later
            bitrate,
            sample_rate,
            channels,
            serial: 0, // Will be set by caller
            version,
            max_bitrate: max_bitrate_opt,
            nominal_bitrate: nominal_bitrate_opt,
            min_bitrate: min_bitrate_opt,
        })
    }

    /// Calculate duration from position
    pub fn set_length(&mut self, position: i64) {
        if self.sample_rate > 0 && position > 0 {
            let duration_secs = position as f64 / self.sample_rate as f64;
            if duration_secs.is_finite() && duration_secs <= u64::MAX as f64 {
                self.length = Some(Duration::from_secs_f64(duration_secs));
            }
        } else {
            self.length = None;
        }
    }

    /// Pretty print format
    pub fn pprint(&self) -> String {
        let duration = self
            .length
            .map(|d| format!("{:.2}", d.as_secs_f64()))
            .unwrap_or_else(|| "0.00".to_string());
        let bitrate = self.bitrate.unwrap_or(0);

        format!("Ogg Vorbis, {} seconds, {} bps", duration, bitrate)
    }

    /// Calculate length from last page
    pub fn post_tags<R: Read + Seek>(&mut self, fileobj: &mut R) -> Result<()> {
        let last_page = OggPage::find_last(fileobj, self.serial, true)?
            .ok_or_else(|| AudexError::InvalidData("could not find last page".to_string()))?;
        if last_page.position > 0 {
            let length_secs = last_page.position as f64 / self.sample_rate as f64;
            if length_secs.is_finite() && length_secs >= 0.0 && length_secs <= u64::MAX as f64 {
                self.length = Some(Duration::from_secs_f64(length_secs));
            }
        }
        Ok(())
    }
}

impl FileType for OggVorbis {
    type Tags = OggVCommentDict;
    type Info = OggVorbisInfo;

    fn format_id() -> &'static str {
        "OggVorbis"
    }

    fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        use std::fs::File;
        use std::io::BufReader;

        debug_event!("parsing OGG Vorbis file");
        let path_buf = path.as_ref().to_path_buf();
        let file = File::open(&path_buf)?;
        let mut reader = BufReader::new(file);

        // Parse Ogg file following standard initialization process
        reader.seek(std::io::SeekFrom::Start(0))?;

        // Find first page with packets
        let mut page = OggPage::from_reader(&mut reader)?;
        if page.packets.is_empty() {
            return Err(
                OggVorbisHeaderError::InvalidHeader("page has not packets".to_string()).into(),
            );
        }

        // Find Vorbis identification header
        let mut pages_read = 1usize;
        while page.packets.is_empty() || !page.packets[0].starts_with(b"\x01vorbis") {
            page = OggPage::from_reader(&mut reader)?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for identification header".to_string(),
                ));
            }
        }

        if !page.is_first() {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page has ID header, but doesn't start a stream".to_string(),
            )
            .into());
        }

        if page.packets[0].len() < 28 {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page contains a packet too short to be valid".to_string(),
            )
            .into());
        }

        let mut info = OggVorbisInfo::from_identification_header(&page.packets[0])?;
        info.serial = page.serial;

        // Parse comment header using OggVCommentDict
        // Note: Don't seek back to start - continue from current position after identification header
        let tags = OggVCommentDict::from_fileobj(&mut reader, &info)?;
        debug_event!(tag_count = tags.keys().len(), "OGG Vorbis tags loaded");

        // Calculate length using post_tags method
        info.post_tags(&mut reader)?;

        Ok(Self {
            info: Some(info),
            tags: Some(tags),
            filename: Some(path_buf),
        })
    }

    fn load_from_reader(reader: &mut dyn crate::ReadSeek) -> Result<Self> {
        debug_event!("parsing OGG Vorbis file from reader");
        let mut reader = reader;
        reader.seek(std::io::SeekFrom::Start(0))?;

        // Find first page with packets
        let mut page = OggPage::from_reader(&mut reader)?;
        if page.packets.is_empty() {
            return Err(
                OggVorbisHeaderError::InvalidHeader("page has not packets".to_string()).into(),
            );
        }

        // Find Vorbis identification header
        let mut pages_read = 1usize;
        while page.packets.is_empty() || !page.packets[0].starts_with(b"\x01vorbis") {
            page = OggPage::from_reader(&mut reader)?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for identification header".to_string(),
                ));
            }
        }

        if !page.is_first() {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page has ID header, but doesn't start a stream".to_string(),
            )
            .into());
        }

        if page.packets[0].len() < 28 {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page contains a packet too short to be valid".to_string(),
            )
            .into());
        }

        let mut info = OggVorbisInfo::from_identification_header(&page.packets[0])?;
        info.serial = page.serial;

        // Parse comment header using OggVCommentDict
        let tags = OggVCommentDict::from_fileobj(&mut reader, &info)?;

        // Calculate length using post_tags method
        info.post_tags(&mut reader)?;

        Ok(Self {
            info: Some(info),
            tags: Some(tags),
            filename: None,
        })
    }

    fn save(&mut self) -> Result<()> {
        debug_event!("saving OGG Vorbis metadata");
        if let Some(path) = self.filename.clone() {
            self.save_with_options(Some(path), None)
        } else {
            warn_event!("no filename available for OGG Vorbis save");
            Err(AudexError::InvalidData(
                "No filename available for saving".to_string(),
            ))
        }
    }

    fn clear(&mut self) -> Result<()> {
        // Preserve the previous tags so we can restore them if the save fails.
        // Without this, a failed save would leave in-memory tags wiped while the
        // file remains unchanged, putting the object in an inconsistent state.
        let prev_tags = self.tags.take();

        let mut empty = OggVCommentDict::new();
        empty.set_vendor(String::new());
        self.tags = Some(empty);

        if let Err(e) = self.save() {
            self.tags = prev_tags;
            return Err(e);
        }
        Ok(())
    }

    fn save_to_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        let tags = self
            .tags
            .as_ref()
            .ok_or_else(|| AudexError::InvalidData("No tags available for saving".to_string()))?;

        // Read all data into memory so we can work with a Cursor that is
        // Sized + 'static, which the inject method requires.
        let buf = crate::util::read_all_from_writer_limited(writer, "in-memory Ogg Vorbis save")?;
        let mut cursor = Cursor::new(buf);
        tags.inject(&mut cursor, None)?;

        // Write the modified data back to the original writer
        let result = cursor.into_inner();
        writer.seek(std::io::SeekFrom::Start(0))?;
        writer.write_all(&result)?;
        crate::util::truncate_writer_dyn(writer, result.len() as u64)?;

        Ok(())
    }

    fn clear_writer(&mut self, writer: &mut dyn crate::ReadWriteSeek) -> Result<()> {
        let mut empty = OggVCommentDict::new();
        empty.set_vendor(String::new());
        self.tags = Some(empty);
        self.save_to_writer(writer)
    }

    fn save_to_path(&mut self, path: &Path) -> Result<()> {
        self.save_with_options(Some(path), None)
    }

    /// Adds empty Vorbis comment block to the file.
    ///
    /// Creates a new empty tag structure if none exists. If tags already exist,
    /// returns an error.
    ///
    /// # Errors
    ///
    /// Returns `AudexError::InvalidOperation` if tags already exist.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use audex::oggvorbis::OggVorbis;
    /// use audex::FileType;
    ///
    /// let mut vorbis = OggVorbis::load("song.ogg")?;
    /// if vorbis.tags.is_none() {
    ///     vorbis.add_tags()?;
    /// }
    /// vorbis.set("title", vec!["My Song".to_string()])?;
    /// vorbis.save()?;
    /// # Ok::<(), audex::AudexError>(())
    /// ```
    fn add_tags(&mut self) -> Result<()> {
        if self.tags.is_some() {
            return Err(AudexError::InvalidOperation(
                "Tags already exist".to_string(),
            ));
        }
        self.tags = Some(OggVCommentDict::new());
        Ok(())
    }

    fn tags(&self) -> Option<&Self::Tags> {
        self.tags.as_ref()
    }

    fn tags_mut(&mut self) -> Option<&mut Self::Tags> {
        self.tags.as_mut()
    }

    fn info(&self) -> &Self::Info {
        // Return a static default when info is absent, rather than panicking.
        // This can happen if the struct was default-constructed or after a
        // partial parse that did not populate the info field.
        static DEFAULT_INFO: OggVorbisInfo = OggVorbisInfo {
            length: None,
            bitrate: None,
            sample_rate: 0,
            channels: 0,
            serial: 0,
            version: 0,
            max_bitrate: None,
            nominal_bitrate: None,
            min_bitrate: None,
        };
        self.info.as_ref().unwrap_or(&DEFAULT_INFO)
    }

    fn score(_filename: &str, header: &[u8]) -> i32 {
        // Return 1 if both conditions are met, 0 otherwise
        let has_ogg_signature = header.len() >= 4 && &header[0..4] == b"OggS";
        let has_vorbis_marker =
            header.len() >= 7 && header.windows(7).any(|window| window == b"\x01vorbis");

        if has_ogg_signature && has_vorbis_marker {
            1
        } else {
            0
        }
    }

    fn mime_types() -> &'static [&'static str] {
        &[
            "audio/ogg",
            "audio/vorbis",
            "audio/x-vorbis",
            "application/ogg",
        ]
    }
}

impl OggVorbis {
    /// Create new OggVorbis instance from a file
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
        Self::load(path)
    }

    /// Save with advanced options
    pub fn save_with_options<P>(
        &mut self,
        path: Option<P>,
        padding_func: Option<fn(&crate::tags::PaddingInfo) -> i64>,
    ) -> Result<()>
    where
        P: AsRef<Path>,
    {
        use std::fs::OpenOptions;

        let (file_path, is_new_path) = match &path {
            Some(p) => (p.as_ref().to_path_buf(), true),
            None => (
                self.filename.clone().ok_or_else(|| {
                    AudexError::InvalidData("No filename available for saving".to_string())
                })?,
                false,
            ),
        };

        // Get tags or return error if no tags to save
        let tags = self
            .tags
            .as_ref()
            .ok_or_else(|| AudexError::InvalidData("No tags available for saving".to_string()))?;

        let mut file = OpenOptions::new().read(true).write(true).open(&file_path)?;
        tags.inject(&mut file, padding_func)?;

        // Update stored filename if a new path was provided
        if is_new_path {
            self.filename = Some(file_path);
        }

        Ok(())
    }

    /// Error type for OggVorbis
    pub const ERROR: &'static str = "OggVorbisHeaderError";

    /// MIME types supported
    pub const MIMES: &'static [&'static str] = &["audio/vorbis", "audio/x-vorbis"];

    /// Static method to score file format match
    pub fn score_static(_filename: &str, _fileobj: &mut dyn Read, header: &[u8]) -> i32 {
        // Return 1 if both conditions are met, 0 otherwise
        let has_ogg_signature = header.len() >= 4 && &header[0..4] == b"OggS";
        let has_vorbis_marker =
            header.len() >= 7 && header.windows(7).any(|window| window == b"\x01vorbis");

        if has_ogg_signature && has_vorbis_marker {
            1
        } else {
            0
        }
    }

    /// Add tags if none exist
    pub fn add_tags(&mut self) -> Result<()> {
        if self.tags.is_some() {
            return Err(AudexError::InvalidOperation(
                "Tags already exist".to_string(),
            ));
        }
        self.tags = Some(OggVCommentDict::new());
        Ok(())
    }
}

/// Standalone function for deleting tags from a file
///
/// # Arguments
/// * `path` - Path to the file
///
/// # Errors
/// Returns `Err` if the file cannot be read, parsed, or written to.
///
/// # Example
/// ```no_run
/// use audex::oggvorbis;
/// oggvorbis::clear("/path/to/file.ogg").expect("Failed to clear tags");
/// ```
pub fn clear<P: AsRef<Path>>(path: P) -> Result<()> {
    let mut vorbis = OggVorbis::load(path)?;
    vorbis.clear()
}

/// Helper method for creating OggVCommentDict from VCommentDict
impl OggVCommentDict {
    /// Create from inner VCommentDict
    ///
    /// This method allows creating an OggVCommentDict from a raw VCommentDict,
    /// which is useful for async operations that parse tags separately.
    pub fn from_inner(inner: VCommentDict) -> Self {
        let mut result = Self::new();
        // Copy all tags from inner
        for key in inner.keys() {
            if let Some(values) = inner.get(&key) {
                result.set(&key, values.to_vec());
            }
        }
        result.set_vendor(inner.vendor().to_string());
        result
    }
}

#[cfg(feature = "async")]
impl OggVorbis {
    /// Load Ogg Vorbis file asynchronously
    ///
    /// Reads and parses an Ogg Vorbis file from disk asynchronously.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the Ogg Vorbis file
    ///
    /// # Returns
    ///
    /// A new `OggVorbis` instance with parsed info and tags
    pub async fn load_async<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path_buf = path.as_ref().to_path_buf();
        let file = TokioFile::open(&path_buf).await?;
        let mut reader = TokioBufReader::new(file);

        // Parse Ogg file following standard initialization process
        reader.seek(SeekFrom::Start(0)).await?;

        // Find first page with packets
        let mut page = OggPage::from_reader_async(&mut reader).await?;
        if page.packets.is_empty() {
            return Err(
                OggVorbisHeaderError::InvalidHeader("page has no packets".to_string()).into(),
            );
        }

        // Find Vorbis identification header
        let mut pages_read = 1usize;
        while page.packets.is_empty() || !page.packets[0].starts_with(b"\x01vorbis") {
            page = OggPage::from_reader_async(&mut reader).await?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for identification header".to_string(),
                ));
            }
        }

        if !page.is_first() {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page has ID header, but doesn't start a stream".to_string(),
            )
            .into());
        }

        if page.packets[0].len() < 28 {
            return Err(OggVorbisHeaderError::InvalidHeader(
                "page contains a packet too short to be valid".to_string(),
            )
            .into());
        }

        let mut info = OggVorbisInfo::from_identification_header(&page.packets[0])?;
        info.serial = page.serial;

        // Parse comment header using async method
        let tags = Self::parse_tags_async(&mut reader, &info).await?;

        // Calculate length using async post_tags method
        Self::post_tags_async(&mut reader, &mut info).await?;

        Ok(Self {
            info: Some(info),
            tags: Some(tags),
            filename: Some(path_buf),
        })
    }

    /// Parse tags from async reader
    async fn parse_tags_async<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
        reader: &mut R,
        info: &OggVorbisInfo,
    ) -> Result<OggVCommentDict> {
        let mut pages = Vec::new();
        let mut complete = false;
        let mut cumulative_bytes = 0u64;
        let limits = crate::limits::ParseLimits::default();

        // Read pages until we have complete Vorbis comment packet
        let mut pages_read = 0usize;
        while !complete {
            let page = OggPage::from_reader_async(reader).await?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for comment packet".to_string(),
                ));
            }
            if page.serial == info.serial {
                OggPage::accumulate_page_bytes_with_limit(
                    limits,
                    &mut cumulative_bytes,
                    &page,
                    "OGG Vorbis comment packet",
                )?;
                pages.push(page.clone());
                complete = page.is_complete() || page.packets.len() > 1;
            }
        }

        // Extract packets from pages
        let packets = OggPage::to_packets(&pages, false)?;
        if packets.is_empty() || packets[0].len() < 7 {
            return Err(AudexError::InvalidData(
                "Invalid Vorbis comment packet".to_string(),
            ));
        }

        // Strip off "\x03vorbis" header to get raw comment data
        let data = &packets[0][7..];

        // Parse with Replace mode to handle encoding issues gracefully
        let inner =
            VCommentDict::from_bytes_with_options(data, crate::vorbis::ErrorMode::Replace, true)?;

        Ok(OggVCommentDict::from_inner(inner))
    }

    /// Calculate length from last page (async version)
    async fn post_tags_async<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
        reader: &mut R,
        info: &mut OggVorbisInfo,
    ) -> Result<()> {
        let last_page = OggPage::find_last_async(reader, info.serial, true)
            .await?
            .ok_or_else(|| AudexError::InvalidData("could not find last page".to_string()))?;
        if last_page.position > 0 {
            let length_secs = last_page.position as f64 / info.sample_rate as f64;
            if length_secs.is_finite() && length_secs >= 0.0 && length_secs <= u64::MAX as f64 {
                info.length = Some(Duration::from_secs_f64(length_secs));
            }
        }
        Ok(())
    }

    /// Save Ogg Vorbis file asynchronously
    ///
    /// Saves the current tags back to the file.
    pub async fn save_async(&mut self) -> Result<()> {
        if let Some(path) = self.filename.clone() {
            self.save_with_options_async(Some(path), None).await
        } else {
            Err(AudexError::InvalidData(
                "No filename available for saving".to_string(),
            ))
        }
    }

    /// Save with advanced options asynchronously
    ///
    /// # Arguments
    ///
    /// * `path` - Optional path to save to (uses stored filename if None)
    /// * `padding_func` - Optional function to calculate padding size
    pub async fn save_with_options_async<P>(
        &mut self,
        path: Option<P>,
        padding_func: Option<fn(&crate::tags::PaddingInfo) -> i64>,
    ) -> Result<()>
    where
        P: AsRef<Path>,
    {
        let (file_path, is_new_path) = match &path {
            Some(p) => (p.as_ref().to_path_buf(), true),
            None => (
                self.filename.clone().ok_or_else(|| {
                    AudexError::InvalidData("No filename available for saving".to_string())
                })?,
                false,
            ),
        };

        // Get tags or return error if no tags to save
        let tags = self
            .tags
            .as_ref()
            .ok_or_else(|| AudexError::InvalidData("No tags available for saving".to_string()))?;

        // Open the file for reading and writing
        let mut file = TokioOpenOptions::new()
            .read(true)
            .write(true)
            .open(&file_path)
            .await?;

        // Use inject method to write tags
        Self::inject_tags_async(&mut file, tags, padding_func).await?;

        // Update stored filename if a new path was provided
        if is_new_path {
            self.filename = Some(file_path);
        }

        Ok(())
    }

    /// Inject tags into the file asynchronously
    async fn inject_tags_async(
        fileobj: &mut TokioFile,
        tags: &OggVCommentDict,
        padding_func: Option<fn(&crate::tags::PaddingInfo) -> i64>,
    ) -> Result<()> {
        // Find the old Vorbis comment pages in the file
        fileobj.seek(SeekFrom::Start(0)).await?;

        // Find the page containing the Vorbis comment header
        let mut page = OggPage::from_reader_async(fileobj).await?;
        let mut pages_read = 1usize;
        while page.packets.is_empty() || !page.packets[0].starts_with(b"\x03vorbis") {
            page = OggPage::from_reader_async(fileobj).await?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while searching for comment header".to_string(),
                ));
            }
        }

        let mut old_pages = vec![page];

        // Collect all pages belonging to the comment packet
        loop {
            let last_page = old_pages.last().ok_or_else(|| {
                AudexError::InvalidData(
                    "expected non-empty page list while reading comments".into(),
                )
            })?;
            if last_page.is_complete() || last_page.packets.len() > 1 {
                break;
            }
            let page = OggPage::from_reader_async(fileobj).await?;
            pages_read += 1;
            if pages_read > MAX_PAGE_SEARCH {
                return Err(AudexError::InvalidData(
                    "Too many OGG pages while reading comment pages".to_string(),
                ));
            }
            if page.serial == old_pages[0].serial {
                old_pages.push(page);
            }
        }

        let packets = OggPage::to_packets(&old_pages, false)?;
        if packets.is_empty() {
            return Err(AudexError::InvalidData("No packets found".to_string()));
        }

        // Calculate content size for padding calculation
        let content_size = {
            let old_pos = fileobj.stream_position().await?;
            let file_size = fileobj.seek(SeekFrom::End(0)).await?;
            fileobj.seek(SeekFrom::Start(old_pos)).await?;
            // Use saturating subtraction to prevent overflow on large or crafted values
            i64::try_from(file_size)
                .unwrap_or(i64::MAX)
                .saturating_sub(i64::try_from(packets[0].len()).unwrap_or(0))
        };

        // Create new Vorbis comment data
        let vcomment_data = {
            let mut data = b"\x03vorbis".to_vec();
            let mut vcomment_bytes = Vec::new();

            let mut comment_to_write = VCommentDict::clone(&**tags);
            if !comment_to_write.keys().is_empty() {
                comment_to_write.set_vendor(format!("Audex {}", VERSION_STRING));
            }

            comment_to_write.write(&mut vcomment_bytes, Some(true))?;
            data.extend_from_slice(&vcomment_bytes);
            data
        };
        let padding_left = packets[0].len() as i64 - vcomment_data.len() as i64;

        // Calculate optimal padding
        let info = crate::tags::PaddingInfo::new(padding_left, content_size);
        let new_padding = info.get_padding_with(padding_func);

        // Build new packet with padding
        let mut new_packets = packets;
        new_packets[0] = vcomment_data;
        // Negative padding indicates the content exceeds the available space;
        // clamp to zero rather than silently discarding data.
        let padding_bytes = if new_padding < 0 {
            0usize
        } else {
            usize::try_from(new_padding).unwrap_or(0)
        };
        if padding_bytes > 0 {
            new_packets[0].extend_from_slice(&vec![0u8; padding_bytes]);
        }

        // Create new Ogg pages, preserving layout if possible
        let new_pages = OggPage::from_packets_try_preserve(new_packets.clone(), &old_pages);

        let final_pages = if new_pages.is_empty() {
            let first_sequence = old_pages[0].sequence;
            let raw_position = old_pages
                .last()
                .ok_or_else(|| AudexError::InvalidData("no comment pages found".to_string()))?
                .position;
            // Per the Ogg spec, granule position -1 means "no granule position set"
            // for this page. Use 0 for comment header pages in this case.
            let original_granule = if raw_position < 0 {
                0u64
            } else {
                raw_position as u64
            };
            OggPage::from_packets_with_options(
                new_packets,
                first_sequence,
                4096,
                2048,
                original_granule,
            )?
        } else {
            new_pages
        };

        if final_pages.is_empty() {
            return Err(AudexError::InvalidData(
                "Failed to create new OGG pages".to_string(),
            ));
        }

        // Replace old pages with new ones
        OggPage::replace_async(fileobj, &old_pages, final_pages).await?;

        Ok(())
    }

    /// Clear tags asynchronously
    ///
    /// Removes all tags from the file and saves the changes.
    pub async fn clear_async(&mut self) -> Result<()> {
        let mut empty = OggVCommentDict::new();
        empty.set_vendor(String::new());
        self.tags = Some(empty);
        self.save_async().await
    }

    /// Delete file tags asynchronously (standalone operation)
    ///
    /// Loads, clears, and saves tags in one operation.
    pub async fn delete_async<P: AsRef<Path>>(path: P) -> Result<()> {
        let mut vorbis = Self::load_async(path).await?;
        vorbis.clear_async().await
    }
}

/// Standalone async function for clearing tags from a file
///
/// # Arguments
/// * `path` - Path to the file
///
/// # Example
/// ```no_run
/// use audex::oggvorbis;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     oggvorbis::clear_async("/path/to/file.ogg").await?;
///     Ok(())
/// }
/// ```
#[cfg(feature = "async")]
pub async fn clear_async<P: AsRef<Path>>(path: P) -> Result<()> {
    OggVorbis::delete_async(path).await
}