imferno-core 2.1.0

SMPTE ST 2067 IMF parser and validator
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
//! SMPTE ST 2067-2 Core Constraints — AssetMap, PKL, and foundational IMF types.
//!
//! This module covers:
//! - Foundational primitives: [`ImfUuid`], [`SmpteUl`], [`ImfTypeError`]
//! - PKL types: [`AssetHash`], [`HashAlgorithm`], [`MimeType`]
//! - Namespace detection: [`AssetMapNamespace`], [`PklNamespace`], [`CoreConstraintsNamespace`]
//! - Document parsers: [`parse_assetmap`], [`parse_pkl`], [`parse_opl`]
//! - Re-exports from [`volindex`]: [`VolumeIndex`], [`parse_volindex`]

pub mod codes;
pub mod volindex;
pub mod volindex_codes;

// Re-export VOLINDEX types
pub use volindex::{parse_volindex, VolindexError, VolumeIndex};

use base64::Engine;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

// ─── Error ────────────────────────────────────────────────────────────────────

#[derive(Debug, Error, PartialEq)]
pub enum ImfTypeError {
    #[error("Invalid UUID '{0}': expected urn:uuid:<uuid> or bare UUID")]
    InvalidUuid(String),
    #[error("Invalid edit rate '{0}': expected 'numerator denominator'")]
    InvalidEditRate(String),
    #[error("Invalid hash: {0}")]
    InvalidHash(String),
    #[error("Invalid language tag '{0}': must be non-empty")]
    InvalidLanguageTag(String),
    #[error("Invalid SMPTE UL '{0}': expected 16 hex bytes in dotted groups")]
    InvalidUl(String),
}

// ─── SmpteUl ─────────────────────────────────────────────────────────────────

/// A SMPTE Universal Label — 16-byte identifier per ST 336M.
///
/// Byte layout:
/// ```text
/// Bytes 1-4:  Object Identifier (always 06.0E.2B.34)
/// Byte  5:    Category designator
/// Byte  6:    Registry designator
/// Byte  7:    Structure designator
/// Byte  8:    Version number  ← MUST BE IGNORED for comparison (ST 298M)
/// Bytes 9-16: Item-specific identification
/// ```
///
/// Per ST 298M, byte 8 (the registry version number) is masked when comparing
/// ULs for semantic identity. Two ULs that differ only in byte 8 are the same item.
#[derive(Debug, Clone, Copy)]
pub struct SmpteUl(pub [u8; 16]);

impl SmpteUl {
    /// Parse a UL from string form.
    ///
    /// Accepted formats:
    /// - `urn:smpte:ul:060e2b34.04010106.04010101.03030000` (4 groups of 4 bytes)
    /// - `060e2b34.04010106.04010101.03030000` (bare 4-group form)
    /// - `060e2b34.0401.0106.04010101.03030000` (5-group variant from some test data)
    pub fn parse(s: &str) -> Result<Self, ImfTypeError> {
        let hex_part = s.strip_prefix("urn:smpte:ul:").unwrap_or(s).trim();

        // Remove dots to get a contiguous hex string
        let hex_str: String = hex_part.chars().filter(|c| *c != '.').collect();

        if hex_str.len() != 32 {
            return Err(ImfTypeError::InvalidUl(s.to_string()));
        }

        let mut bytes = [0u8; 16];
        for i in 0..16 {
            bytes[i] = u8::from_str_radix(&hex_str[i * 2..i * 2 + 2], 16)
                .map_err(|_| ImfTypeError::InvalidUl(s.to_string()))?;
        }

        Ok(SmpteUl(bytes))
    }

    /// Compare two ULs ignoring byte 8 (index 7) — the registry version number.
    ///
    /// Per ST 298M, the version byte must be masked for semantic comparison.
    pub fn matches_ignoring_version(&self, other: &SmpteUl) -> bool {
        for i in 0..16 {
            if i == 7 {
                continue; // skip version byte
            }
            if self.0[i] != other.0[i] {
                return false;
            }
        }
        true
    }

    /// Return the UL with byte 8 zeroed for use as a canonical match key.
    pub fn normalized(&self) -> Self {
        let mut bytes = self.0;
        bytes[7] = 0;
        SmpteUl(bytes)
    }

    /// The discriminating bytes (bytes 9-16) that identify the specific item.
    pub fn item_bytes(&self) -> &[u8] {
        &self.0[8..]
    }
}

impl PartialEq for SmpteUl {
    /// Equality comparison ignores byte 8 (version), per ST 298M.
    fn eq(&self, other: &Self) -> bool {
        self.matches_ignoring_version(other)
    }
}

impl Eq for SmpteUl {}

impl std::hash::Hash for SmpteUl {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // Hash with byte 8 zeroed so equal items hash identically
        let norm = self.normalized();
        norm.0.hash(state);
    }
}

impl std::fmt::Display for SmpteUl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "urn:smpte:ul:{:02x}{:02x}{:02x}{:02x}.{:02x}{:02x}{:02x}{:02x}.{:02x}{:02x}{:02x}{:02x}.{:02x}{:02x}{:02x}{:02x}",
            self.0[0], self.0[1], self.0[2], self.0[3],
            self.0[4], self.0[5], self.0[6], self.0[7],
            self.0[8], self.0[9], self.0[10], self.0[11],
            self.0[12], self.0[13], self.0[14], self.0[15],
        )
    }
}

// ─── ImfUuid ──────────────────────────────────────────────────────────────────

/// A SMPTE IMF UUID.
///
/// In XML documents UUIDs appear as `urn:uuid:<uuid>`. In JSON/WASM output
/// they serialise as bare UUID strings (`"0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85"`).
/// Deserialization accepts both forms.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(type = "string"))]
pub struct ImfUuid(pub Uuid);

impl ImfUuid {
    /// Parse from `urn:uuid:...` or a bare UUID string.
    pub fn parse(s: &str) -> Result<Self, ImfTypeError> {
        let bare = s.strip_prefix("urn:uuid:").unwrap_or(s);
        Uuid::parse_str(bare)
            .map(ImfUuid)
            .map_err(|_| ImfTypeError::InvalidUuid(s.to_string()))
    }

    /// Return the URN form used in XML: `urn:uuid:<uuid>`.
    pub fn to_urn(&self) -> String {
        format!("urn:uuid:{}", self.0)
    }
}

impl std::fmt::Display for ImfUuid {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl Serialize for ImfUuid {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.0.to_string())
    }
}

impl<'de> Deserialize<'de> for ImfUuid {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = String::deserialize(d)?;
        ImfUuid::parse(&s).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "jsonschema")]
impl schemars::JsonSchema for ImfUuid {
    fn schema_name() -> String {
        "ImfUuid".to_owned()
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let mut schema = gen.subschema_for::<String>().into_object();
        schema.metadata().description = Some(
            "A SMPTE IMF UUID, serialised as a bare UUID string (e.g. \"0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85\")".to_owned()
        );
        schema.format = Some("uuid".to_owned());
        schema.into()
    }
}

// ─── AssetHash ────────────────────────────────────────────────────────────────

/// A decoded asset hash from a Packing List, per SMPTE ST 2067-2 §9.
///
/// PKL files carry base64-encoded SHA-1 or SHA-256 digests for each tracked asset.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AssetHash {
    algorithm: HashAlgorithm,
    bytes: Vec<u8>,
}

/// The hash algorithm used for a PKL asset digest.
///
/// Per SMPTE ST 2067-2:2020 §9, SHA-1 is the default algorithm.
/// SHA-256 is supported via the `<HashAlgorithm>` element using
/// XML Digital Signature algorithm URIs.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HashAlgorithm {
    /// SHA-1 (default per ST 2067-2 §9).
    /// URI: `http://www.w3.org/2000/09/xmldsig#sha1`
    Sha1,
    /// SHA-256.
    /// URI: `http://www.w3.org/2001/04/xmlenc#sha256`
    Sha256,
}

impl HashAlgorithm {
    /// Parse a hash algorithm from an XML Digital Signature algorithm URI.
    ///
    /// Per ST 2067-2:2020 §9, the `<HashAlgorithm>` element uses
    /// `ds:DigestMethodType` which carries an `Algorithm` attribute URI.
    pub fn from_uri(uri: &str) -> Option<Self> {
        match uri.trim() {
            "http://www.w3.org/2000/09/xmldsig#sha1" => Some(Self::Sha1),
            "http://www.w3.org/2001/04/xmlenc#sha256" => Some(Self::Sha256),
            _ => None,
        }
    }

    /// Expected digest length in bytes for this algorithm.
    pub fn digest_len(&self) -> usize {
        match self {
            Self::Sha1 => 20,
            Self::Sha256 => 32,
        }
    }
}

impl std::fmt::Display for HashAlgorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Sha1 => write!(f, "SHA-1"),
            Self::Sha256 => write!(f, "SHA-256"),
        }
    }
}

impl AssetHash {
    /// Return the hash algorithm (SHA-1 or SHA-256).
    pub fn algorithm(&self) -> HashAlgorithm {
        self.algorithm
    }

    /// Return the raw digest bytes.
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Decode a base64-encoded SHA-1 digest as found in PKL `<Hash>` elements.
    ///
    /// Per SMPTE ST 2067-2 §9, SHA-1 produces a 20-byte digest.
    pub fn from_base64_sha1(b64: &str) -> Result<Self, ImfTypeError> {
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(b64)
            .map_err(|e| ImfTypeError::InvalidHash(e.to_string()))?;
        if bytes.len() != 20 {
            return Err(ImfTypeError::InvalidHash(format!(
                "SHA-1 digest must be 20 bytes, got {}",
                bytes.len()
            )));
        }
        Ok(Self {
            algorithm: HashAlgorithm::Sha1,
            bytes,
        })
    }

    /// Decode a base64-encoded SHA-256 digest.
    ///
    /// SHA-256 produces a 32-byte digest.
    pub fn from_base64_sha256(b64: &str) -> Result<Self, ImfTypeError> {
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(b64)
            .map_err(|e| ImfTypeError::InvalidHash(e.to_string()))?;
        if bytes.len() != 32 {
            return Err(ImfTypeError::InvalidHash(format!(
                "SHA-256 digest must be 32 bytes, got {}",
                bytes.len()
            )));
        }
        Ok(Self {
            algorithm: HashAlgorithm::Sha256,
            bytes,
        })
    }

    /// Decode a base64-encoded digest for the given algorithm.
    pub fn from_base64(b64: &str, algorithm: HashAlgorithm) -> Result<Self, ImfTypeError> {
        match algorithm {
            HashAlgorithm::Sha1 => Self::from_base64_sha1(b64),
            HashAlgorithm::Sha256 => Self::from_base64_sha256(b64),
        }
    }

    /// Encode the hash bytes as base64, as used in PKL XML.
    pub fn to_base64(&self) -> String {
        base64::engine::general_purpose::STANDARD.encode(&self.bytes)
    }
}

// ─── MimeType ─────────────────────────────────────────────────────────────────

/// MIME type as used in `<Type>` elements in PKL assets (SMPTE ST 2067-2 §9).
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MimeType {
    /// `text/xml` — CPL and other XML documents
    TextXml,
    /// `application/xml` — alternative XML MIME type
    ApplicationXml,
    /// `application/mxf` — MXF essence files
    ApplicationMxf,
    /// Unrecognised; the original string is preserved.
    Other(String),
}

impl MimeType {
    pub fn parse(s: &str) -> Self {
        match s.trim() {
            "text/xml" => Self::TextXml,
            "application/xml" => Self::ApplicationXml,
            "application/mxf" => Self::ApplicationMxf,
            other => Self::Other(other.to_string()),
        }
    }

    pub fn is_xml(&self) -> bool {
        matches!(self, Self::TextXml | Self::ApplicationXml)
    }

    pub fn is_mxf(&self) -> bool {
        matches!(self, Self::ApplicationMxf)
    }
}

impl std::fmt::Display for MimeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::TextXml => write!(f, "text/xml"),
            Self::ApplicationXml => write!(f, "application/xml"),
            Self::ApplicationMxf => write!(f, "application/mxf"),
            Self::Other(s) => write!(f, "{}", s),
        }
    }
}

// ─── AssetMapNamespace ────────────────────────────────────────────────────────

/// The detected SMPTE spec version of an AssetMap document, derived from its root xmlns.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AssetMapNamespace {
    /// DCI era — `http://www.smpte-ra.org/schemas/429-9/2007/AM`
    #[default]
    Dci429_9,
    /// SMPTE ST 2067-9:2016 — `http://www.smpte-ra.org/schemas/2067-9/2016`
    Smpte2067_9_2016,
    /// SMPTE ST 2067-9:2020 — `http://www.smpte-ra.org/ns/2067-9/2020`
    Smpte2067_9_2020,
    /// Unrecognised namespace; the original URI is preserved.
    Unknown(String),
}

impl AssetMapNamespace {
    /// Detect AssetMap spec version from a namespace URI.
    pub fn from_uri(uri: &str) -> Self {
        match uri.trim() {
            "http://www.smpte-ra.org/schemas/429-9/2007/AM" => Self::Dci429_9,
            "http://www.smpte-ra.org/schemas/2067-9/2016" => Self::Smpte2067_9_2016,
            "http://www.smpte-ra.org/ns/2067-9/2020" => Self::Smpte2067_9_2020,
            other => Self::Unknown(other.to_string()),
        }
    }

    /// Returns the normative spec document identifier.
    pub fn spec_id(&self) -> &str {
        match self {
            Self::Dci429_9 => "ST 429-9:2007",
            Self::Smpte2067_9_2016 => "ST 2067-9:2016",
            Self::Smpte2067_9_2020 => "ST 2067-9:2020",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl std::fmt::Display for AssetMapNamespace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Dci429_9 => write!(f, "http://www.smpte-ra.org/schemas/429-9/2007/AM"),
            Self::Smpte2067_9_2016 => write!(f, "http://www.smpte-ra.org/schemas/2067-9/2016"),
            Self::Smpte2067_9_2020 => write!(f, "http://www.smpte-ra.org/ns/2067-9/2020"),
            Self::Unknown(s) => write!(f, "{}", s),
        }
    }
}

// ─── PklNamespace ─────────────────────────────────────────────────────────────

/// The detected SMPTE spec version of a PKL document, derived from its root xmlns.
///
/// PKL schema evolved across three eras: DCI 429-8, IMF 2067-2 (2013-2016), and 2020.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PklNamespace {
    /// DCI era — `http://www.smpte-ra.org/schemas/429-8/2007/PKL`
    #[default]
    Dci429_8,
    /// SMPTE ST 2067-2:2013 — `http://www.smpte-ra.org/schemas/2067-2/2013`
    Smpte2067_2_2013,
    /// SMPTE ST 2067-2:2016 — `http://www.smpte-ra.org/schemas/2067-2/2016`
    Smpte2067_2_2016,
    /// SMPTE ST 2067-2:2016 (PKL variant) — `http://www.smpte-ra.org/schemas/2067-2/2016/PKL`
    Smpte2067_2_2016Pkl,
    /// SMPTE ST 2067-2:2020 — `http://www.smpte-ra.org/ns/2067-2/2020`
    Smpte2067_2_2020,
    /// Unrecognised namespace; the original URI is preserved.
    Unknown(String),
}

impl PklNamespace {
    /// Detect PKL spec version from a namespace URI.
    pub fn from_uri(uri: &str) -> Self {
        match uri.trim() {
            "http://www.smpte-ra.org/schemas/429-8/2007/PKL" => Self::Dci429_8,
            "http://www.smpte-ra.org/schemas/2067-2/2013" => Self::Smpte2067_2_2013,
            "http://www.smpte-ra.org/schemas/2067-2/2016" => Self::Smpte2067_2_2016,
            "http://www.smpte-ra.org/schemas/2067-2/2016/PKL" => Self::Smpte2067_2_2016Pkl,
            "http://www.smpte-ra.org/ns/2067-2/2020" => Self::Smpte2067_2_2020,
            other => Self::Unknown(other.to_string()),
        }
    }

    /// Returns the normative spec document identifier.
    pub fn spec_id(&self) -> &str {
        match self {
            Self::Dci429_8 => "ST 429-8:2007",
            Self::Smpte2067_2_2013 => "ST 2067-2:2013",
            Self::Smpte2067_2_2016 | Self::Smpte2067_2_2016Pkl => "ST 2067-2:2016",
            Self::Smpte2067_2_2020 => "ST 2067-2:2020",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl std::fmt::Display for PklNamespace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Dci429_8 => write!(f, "http://www.smpte-ra.org/schemas/429-8/2007/PKL"),
            Self::Smpte2067_2_2013 => write!(f, "http://www.smpte-ra.org/schemas/2067-2/2013"),
            Self::Smpte2067_2_2016 => write!(f, "http://www.smpte-ra.org/schemas/2067-2/2016"),
            Self::Smpte2067_2_2016Pkl => {
                write!(f, "http://www.smpte-ra.org/schemas/2067-2/2016/PKL")
            }
            Self::Smpte2067_2_2020 => write!(f, "http://www.smpte-ra.org/ns/2067-2/2020"),
            Self::Unknown(s) => write!(f, "{}", s),
        }
    }
}

// ─── CoreConstraintsNamespace ─────────────────────────────────────────────────

/// The detected SMPTE core constraints spec version, from inner xmlns declarations in CPLs.
///
/// CPL documents reference core constraints namespaces for elements defined in ST 2067-2.
/// This is distinct from the CPL namespace (ST 2067-3) and determines which core constraint
/// rules apply.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum CoreConstraintsNamespace {
    /// SMPTE ST 2067-2:2013 — `http://www.smpte-ra.org/schemas/2067-2/2013`
    Smpte2067_2_2013,
    /// SMPTE ST 2067-2:2016 — `http://www.smpte-ra.org/schemas/2067-2/2016`
    #[default]
    Smpte2067_2_2016,
    /// SMPTE ST 2067-2:2020 — `http://www.smpte-ra.org/ns/2067-2/2020`
    Smpte2067_2_2020,
    /// Unrecognised namespace; the original URI is preserved.
    Unknown(String),
}

impl CoreConstraintsNamespace {
    /// Detect core constraints spec version from a namespace URI.
    pub fn from_uri(uri: &str) -> Self {
        match uri.trim() {
            "http://www.smpte-ra.org/schemas/2067-2/2013" => Self::Smpte2067_2_2013,
            "http://www.smpte-ra.org/schemas/2067-2/2016" => Self::Smpte2067_2_2016,
            "http://www.smpte-ra.org/ns/2067-2/2020" => Self::Smpte2067_2_2020,
            other => Self::Unknown(other.to_string()),
        }
    }

    /// Returns the normative spec document identifier.
    pub fn spec_id(&self) -> &str {
        match self {
            Self::Smpte2067_2_2013 => "ST 2067-2:2013",
            Self::Smpte2067_2_2016 => "ST 2067-2:2016",
            Self::Smpte2067_2_2020 => "ST 2067-2:2020",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl std::fmt::Display for CoreConstraintsNamespace {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Smpte2067_2_2013 => write!(f, "http://www.smpte-ra.org/schemas/2067-2/2013"),
            Self::Smpte2067_2_2016 => write!(f, "http://www.smpte-ra.org/schemas/2067-2/2016"),
            Self::Smpte2067_2_2020 => write!(f, "http://www.smpte-ra.org/ns/2067-2/2020"),
            Self::Unknown(s) => write!(f, "{}", s),
        }
    }
}

// ─── detect_root_namespace ────────────────────────────────────────────────────

/// Extract the default namespace URI from an XML document's root element.
///
/// Searches for the first `xmlns="..."` (non-prefixed) declaration. This is used
/// by parsers to detect which spec version a document conforms to.
pub fn detect_root_namespace(xml: &str) -> Option<String> {
    use std::sync::LazyLock;
    // Match xmlns="..." but NOT xmlns:prefix="..."
    // We look for xmlns= preceded by whitespace (not by a colon)
    static RE_XMLNS: LazyLock<regex::Regex> =
        LazyLock::new(|| regex::Regex::new(r#"(?:^|[\s<])xmlns="([^"]*)""#).unwrap());
    RE_XMLNS.captures(xml).map(|cap| cap[1].to_string())
}

// ─── Parse error ──────────────────────────────────────────────────────────────

/// Errors that can occur when parsing an AssetMap, PKL, OPL, or VOLINDEX.
#[derive(Debug, Error)]
pub enum AssetMapParseError {
    /// The XML is structurally invalid or missing required elements.
    #[error("XML parse error: {0}")]
    Xml(#[from] quick_xml::DeError),
    /// A required field contains an invalid value (bad UUID, bad hash, etc.).
    #[error("Invalid field '{field}': {source}")]
    Field {
        field: &'static str,
        #[source]
        source: ImfTypeError,
    },
}

// ─── Private raw deserialization layer ────────────────────────────────────────

mod raw {
    use serde::Deserialize;

    fn default_volume_index() -> u32 {
        1
    }

    #[derive(Deserialize)]
    pub struct AssetMap {
        #[serde(rename = "Id")]
        pub id: String,
        #[serde(rename = "AnnotationText", default)]
        pub annotation_text: Option<String>,
        #[serde(rename = "Creator", default)]
        pub creator: Option<String>,
        #[serde(rename = "VolumeCount")]
        pub volume_count: u32,
        #[serde(rename = "IssueDate")]
        pub issue_date: String,
        #[serde(rename = "Issuer", default)]
        pub issuer: Option<String>,
        #[serde(rename = "AssetList")]
        pub asset_list: AssetList,
    }

    #[derive(Deserialize)]
    pub struct AssetList {
        #[serde(rename = "Asset")]
        pub assets: Vec<Asset>,
    }

    #[derive(Deserialize)]
    pub struct Asset {
        #[serde(rename = "Id")]
        pub id: String,
        #[serde(rename = "PackingList", default)]
        pub packing_list: Option<bool>,
        #[serde(rename = "ChunkList")]
        pub chunk_list: ChunkList,
    }

    #[derive(Deserialize)]
    pub struct ChunkList {
        #[serde(rename = "Chunk")]
        pub chunks: Vec<Chunk>,
    }

    #[derive(Deserialize)]
    pub struct Chunk {
        #[serde(rename = "Path")]
        pub path: String,
        #[serde(rename = "VolumeIndex", default = "default_volume_index")]
        pub volume_index: u32,
    }

    // ── OPL (ST 2067-100) ──────────────────────────────────────────────────

    #[derive(Deserialize)]
    pub struct OutputProfileList {
        #[serde(rename = "Id")]
        pub id: String,
        #[serde(rename = "Annotation", default)]
        pub annotation: Option<String>,
        #[serde(rename = "IssueDate")]
        pub issue_date: String,
        #[serde(rename = "Issuer", default)]
        pub issuer: Option<String>,
        #[serde(rename = "Creator", default)]
        pub creator: Option<String>,
        #[serde(rename = "CompositionPlaylistId")]
        pub composition_playlist_id: String,
    }

    // ── PKL ─────────────────────────────────────────────────────────────────

    #[derive(Deserialize)]
    pub struct PackingList {
        #[serde(rename = "Id")]
        pub id: String,
        #[serde(rename = "AnnotationText", default)]
        pub annotation_text: Option<String>,
        #[serde(rename = "IssueDate")]
        pub issue_date: String,
        #[serde(rename = "Issuer", default)]
        pub issuer: Option<String>,
        #[serde(rename = "Creator", default)]
        pub creator: Option<String>,
        /// SMPTE ST 2067-2 §9: Optional group identifier for partial deliveries.
        #[serde(rename = "GroupId", default)]
        pub group_id: Option<String>,
        #[serde(rename = "AssetList")]
        pub asset_list: PklAssetList,
    }

    #[derive(Deserialize)]
    pub struct PklAssetList {
        #[serde(rename = "Asset")]
        pub assets: Vec<PklAsset>,
    }

    /// `ds:DigestMethodType` — carries an `Algorithm` attribute URI.
    /// Used in `<HashAlgorithm Algorithm="..."/>` per SMPTE ST 2067-2 §9.
    #[derive(Deserialize)]
    pub struct DigestMethod {
        #[serde(rename = "@Algorithm")]
        pub algorithm: String,
    }

    #[derive(Deserialize)]
    pub struct PklAsset {
        #[serde(rename = "Id")]
        pub id: String,
        #[serde(rename = "AnnotationText", default)]
        pub annotation_text: Option<String>,
        #[serde(rename = "Hash")]
        pub hash: String,
        #[serde(rename = "Size")]
        pub size: u64,
        #[serde(rename = "Type")]
        pub mime_type: String,
        #[serde(rename = "OriginalFileName", default)]
        pub original_file_name: Option<String>,
        /// SMPTE ST 2067-2 §9: Optional hash algorithm override.
        /// When absent, SHA-1 is assumed (default per spec).
        #[serde(rename = "HashAlgorithm", default)]
        pub hash_algorithm: Option<DigestMethod>,
    }
}

// ─── Public domain types ───────────────────────────────────────────────────────

// VolumeIndex lives in the volindex submodule and is re-exported at the top of this file.

/// ASSETMAP.xml — maps UUIDs to physical file paths (ST 429-9 §6).
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, rename_all = "camelCase"))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct AssetMap {
    /// The SMPTE spec version detected from the root xmlns.
    #[serde(skip)]
    pub namespace: AssetMapNamespace,
    /// Unique identifier for this AssetMap (ST 429-9 §6.2).
    pub id: ImfUuid,
    pub annotation_text: Option<String>,
    pub creator: Option<String>,
    pub volume_count: u32,
    /// ISO 8601 issue date (e.g. `"2016-10-06T08:35:02-00:00"`).
    pub issue_date: String,
    pub issuer: Option<String>,
    pub asset_list: AssetList,
}

impl AssetMap {
    fn from_raw(
        raw: raw::AssetMap,
        namespace: AssetMapNamespace,
    ) -> Result<Self, AssetMapParseError> {
        Ok(Self {
            namespace,
            id: ImfUuid::parse(&raw.id).map_err(|source| AssetMapParseError::Field {
                field: "Id",
                source,
            })?,
            annotation_text: raw.annotation_text,
            creator: raw.creator,
            volume_count: raw.volume_count,
            issue_date: raw.issue_date,
            issuer: raw.issuer,
            asset_list: AssetList::from_raw(raw.asset_list)?,
        })
    }
}

/// The `<AssetList>` element in ASSETMAP.xml.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, rename_all = "camelCase"))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct AssetList {
    pub assets: Vec<Asset>,
}

impl AssetList {
    fn from_raw(raw: raw::AssetList) -> Result<Self, AssetMapParseError> {
        let assets = raw
            .assets
            .into_iter()
            .map(Asset::from_raw)
            .collect::<Result<Vec<_>, _>>()?;
        Ok(Self { assets })
    }
}

/// A single asset entry in ASSETMAP.xml.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, rename_all = "camelCase"))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Asset {
    /// UUID identifying this asset.
    pub id: ImfUuid,
    /// Present and `true` when this entry refers to the Packing List file.
    pub packing_list: Option<bool>,
    pub chunk_list: ChunkList,
}

impl Asset {
    fn from_raw(raw: raw::Asset) -> Result<Self, AssetMapParseError> {
        Ok(Self {
            id: ImfUuid::parse(&raw.id).map_err(|source| AssetMapParseError::Field {
                field: "Id",
                source,
            })?,
            packing_list: raw.packing_list,
            chunk_list: ChunkList::from_raw(raw.chunk_list),
        })
    }
}

/// A list of file chunks for a single asset.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, rename_all = "camelCase"))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct ChunkList {
    pub chunks: Vec<Chunk>,
}

impl ChunkList {
    fn from_raw(raw: raw::ChunkList) -> Self {
        Self {
            chunks: raw
                .chunks
                .into_iter()
                .map(|c| Chunk {
                    path: c.path,
                    volume_index: c.volume_index,
                })
                .collect(),
        }
    }
}

/// A single file path entry in a ChunkList.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
#[cfg_attr(feature = "typescript", ts(export, rename_all = "camelCase"))]
#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub struct Chunk {
    /// File path relative to the IMP root directory.
    pub path: String,
    pub volume_index: u32,
}

/// OPL XML — Output Profile List (SMPTE ST 2067-100).
///
/// Defines output processing instructions for a composition: image scaling,
/// cropping, pixel encoding, and audio routing/mixing macros. The macro list
/// is not deserialized (it uses `xsi:type` polymorphism with vendor-specific
/// extension types).
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct OutputProfileList {
    pub id: ImfUuid,
    pub annotation: Option<String>,
    /// ISO 8601 issue date.
    pub issue_date: String,
    pub issuer: Option<String>,
    pub creator: Option<String>,
    /// The CPL that this OPL targets.
    pub composition_playlist_id: ImfUuid,
}

impl OutputProfileList {
    fn from_raw(raw: raw::OutputProfileList) -> Result<Self, AssetMapParseError> {
        Ok(Self {
            id: ImfUuid::parse(&raw.id).map_err(|source| AssetMapParseError::Field {
                field: "Id",
                source,
            })?,
            annotation: raw.annotation,
            issue_date: raw.issue_date,
            issuer: raw.issuer,
            creator: raw.creator,
            composition_playlist_id: ImfUuid::parse(&raw.composition_playlist_id).map_err(
                |source| AssetMapParseError::Field {
                    field: "CompositionPlaylistId",
                    source,
                },
            )?,
        })
    }
}

/// PKL XML — Packing List (SMPTE ST 2067-2 §9).
///
/// Assets carry SHA-1 (default) or SHA-256 checksums. The algorithm is
/// determined by the optional `<HashAlgorithm>` element on each asset.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PackingList {
    /// The SMPTE spec version detected from the root xmlns.
    #[serde(skip)]
    pub namespace: PklNamespace,
    pub id: ImfUuid,
    pub annotation_text: Option<String>,
    /// ISO 8601 issue date.
    pub issue_date: String,
    pub issuer: Option<String>,
    pub creator: Option<String>,
    /// Optional group identifier for partial deliveries (SMPTE ST 2067-2 §9).
    pub group_id: Option<ImfUuid>,
    pub asset_list: PklAssetList,
}

impl PackingList {
    fn from_raw(
        raw: raw::PackingList,
        namespace: PklNamespace,
    ) -> Result<Self, AssetMapParseError> {
        let group_id = raw
            .group_id
            .map(|s| ImfUuid::parse(&s))
            .transpose()
            .map_err(|source| AssetMapParseError::Field {
                field: "GroupId",
                source,
            })?;

        Ok(Self {
            namespace,
            id: ImfUuid::parse(&raw.id).map_err(|source| AssetMapParseError::Field {
                field: "Id",
                source,
            })?,
            annotation_text: raw.annotation_text,
            issue_date: raw.issue_date,
            issuer: raw.issuer,
            creator: raw.creator,
            group_id,
            asset_list: PklAssetList::from_raw(raw.asset_list)?,
        })
    }
}

/// The `<AssetList>` element in a PKL.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PklAssetList {
    pub assets: Vec<PklAsset>,
}

impl PklAssetList {
    fn from_raw(raw: raw::PklAssetList) -> Result<Self, AssetMapParseError> {
        let assets = raw
            .assets
            .into_iter()
            .map(PklAsset::from_raw)
            .collect::<Result<Vec<_>, _>>()?;
        Ok(Self { assets })
    }
}

/// A single asset entry in a PKL.
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PklAsset {
    pub id: ImfUuid,
    pub annotation_text: Option<String>,
    /// SHA-1 digest decoded from the base64 `<Hash>` element (SMPTE ST 2067-2 §9.3).
    pub hash: AssetHash,
    /// File size in bytes.
    pub size: u64,
    /// MIME type of the asset file (SMPTE ST 2067-2 §9.4).
    pub mime_type: MimeType,
    pub original_file_name: Option<String>,
}

impl PklAsset {
    fn from_raw(raw: raw::PklAsset) -> Result<Self, AssetMapParseError> {
        // Determine hash algorithm from <HashAlgorithm Algorithm="..."/> element.
        // Per ST 2067-2 §9, SHA-1 is the default when the element is absent.
        let algorithm = match &raw.hash_algorithm {
            Some(dm) => {
                HashAlgorithm::from_uri(&dm.algorithm).ok_or_else(|| AssetMapParseError::Field {
                    field: "HashAlgorithm",
                    source: ImfTypeError::InvalidHash(format!(
                        "unsupported hash algorithm URI: {}",
                        dm.algorithm
                    )),
                })?
            }
            None => HashAlgorithm::Sha1,
        };

        Ok(Self {
            id: ImfUuid::parse(&raw.id).map_err(|source| AssetMapParseError::Field {
                field: "Id",
                source,
            })?,
            annotation_text: raw.annotation_text,
            hash: AssetHash::from_base64(&raw.hash, algorithm).map_err(|source| {
                AssetMapParseError::Field {
                    field: "Hash",
                    source,
                }
            })?,
            size: raw.size,
            mime_type: MimeType::parse(&raw.mime_type),
            original_file_name: raw.original_file_name,
        })
    }
}

// ─── Parse functions ──────────────────────────────────────────────────────────

// parse_volindex is re-exported from the volindex module at the top of this file.

/// Parse ASSETMAP.xml (ST 429-9 §6).
///
/// Detects the SMPTE spec version from the root `xmlns` attribute and stores it
/// on the returned `AssetMap.namespace` field.
pub fn parse_assetmap(xml_content: &str) -> Result<AssetMap, AssetMapParseError> {
    let namespace = detect_root_namespace(xml_content)
        .map(|uri| AssetMapNamespace::from_uri(&uri))
        .unwrap_or_default();
    let raw: raw::AssetMap = quick_xml::de::from_str(xml_content)?;
    AssetMap::from_raw(raw, namespace)
}

/// Parse PKL XML (SMPTE ST 2067-2 §9).
///
/// Detects the SMPTE spec version from the root `xmlns` attribute and stores it
/// on the returned `PackingList.namespace` field.
pub fn parse_pkl(xml_content: &str) -> Result<PackingList, AssetMapParseError> {
    let namespace = detect_root_namespace(xml_content)
        .map(|uri| PklNamespace::from_uri(&uri))
        .unwrap_or_default();
    let raw: raw::PackingList = quick_xml::de::from_str(xml_content)?;
    PackingList::from_raw(raw, namespace)
}

/// Parse OPL XML (SMPTE ST 2067-100).
///
/// Extracts the core metadata (Id, Annotation, IssueDate, Issuer, Creator,
/// CompositionPlaylistId). The MacroList is not deserialized because it uses
/// `xsi:type` polymorphism with vendor-specific extension namespaces.
pub fn parse_opl(xml_content: &str) -> Result<OutputProfileList, AssetMapParseError> {
    let raw: raw::OutputProfileList = quick_xml::de::from_str(xml_content)?;
    OutputProfileList::from_raw(raw)
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use std::path::PathBuf;

    fn test_data(name: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../../test-data")
            .join(name)
    }

    // ── ImfUuid ──────────────────────────────────────────────────────────────

    /// SMPTE ST 2067-2 §7: UUIDs are serialized as urn:uuid: URNs in XML.
    #[test]
    fn uuid_parse_urn_form() {
        let id = ImfUuid::parse("urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85").unwrap();
        assert_eq!(id.to_string(), "0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85");
        assert_eq!(id.to_urn(), "urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85");
    }

    #[test]
    fn uuid_parse_bare_form() {
        let id = ImfUuid::parse("0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85").unwrap();
        assert_eq!(id.to_string(), "0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85");
    }

    #[test]
    fn uuid_parse_invalid() {
        assert!(ImfUuid::parse("not-a-uuid").is_err());
        assert!(ImfUuid::parse("").is_err());
        assert!(ImfUuid::parse("urn:uuid:not-valid").is_err());
    }

    #[test]
    fn uuid_roundtrip_serde() {
        let id = ImfUuid::parse("urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85").unwrap();
        let json = serde_json::to_string(&id).unwrap();
        // Serializes as bare UUID (no urn: prefix) for JSON
        assert_eq!(json, r#""0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85""#);
        let back: ImfUuid = serde_json::from_str(&json).unwrap();
        assert_eq!(id, back);
    }

    #[test]
    fn uuid_deserialize_urn_from_json() {
        // Deserializer must accept urn: form too
        let back: ImfUuid =
            serde_json::from_str(r#""urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85""#).unwrap();
        assert_eq!(back.to_string(), "0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85");
    }

    // ── SmpteUl ──────────────────────────────────────────────────────────────

    /// ST 298M: Byte 8 (registry version) must be ignored for semantic identity.
    #[test]
    fn smpte_ul_parse_4group() {
        let ul = SmpteUl::parse("060e2b34.04010106.04010101.03030000").unwrap();
        assert_eq!(ul.0[0], 0x06);
        assert_eq!(ul.0[7], 0x06); // byte 8 = version
        assert_eq!(ul.0[12], 0x03);
    }

    #[test]
    fn smpte_ul_parse_urn_form() {
        let ul = SmpteUl::parse("urn:smpte:ul:060e2b34.04010106.04010101.03030000").unwrap();
        assert_eq!(ul.0[0], 0x06);
    }

    #[test]
    fn smpte_ul_parse_5group_variant() {
        // Variant format from some test data
        let ul = SmpteUl::parse("urn:smpte:ul:060e2b34.0401.0101.04010101.01020000").unwrap();
        assert_eq!(ul.0[4], 0x04);
        assert_eq!(ul.0[5], 0x01);
    }

    #[test]
    fn smpte_ul_version_agnostic_equality() {
        // Same UL at different registry versions
        let v1 = SmpteUl::parse("060e2b34.04010101.04010101.03030000").unwrap();
        let v6 = SmpteUl::parse("060e2b34.04010106.04010101.03030000").unwrap();
        let vd = SmpteUl::parse("060e2b34.0401010d.04010101.03030000").unwrap();
        assert_eq!(v1, v6, "version 01 == version 06");
        assert_eq!(v6, vd, "version 06 == version 0d");
    }

    #[test]
    fn smpte_ul_different_items_not_equal() {
        let a = SmpteUl::parse("060e2b34.04010106.04010101.03030000").unwrap();
        let b = SmpteUl::parse("060e2b34.04010106.04010101.03040000").unwrap();
        assert_ne!(a, b);
    }

    #[test]
    fn smpte_ul_display_roundtrip() {
        let ul = SmpteUl::parse("urn:smpte:ul:060e2b34.04010106.04010101.03030000").unwrap();
        let s = ul.to_string();
        assert!(s.starts_with("urn:smpte:ul:"));
        let ul2 = SmpteUl::parse(&s).unwrap();
        assert_eq!(ul, ul2);
    }

    #[test]
    fn smpte_ul_parse_invalid() {
        assert!(SmpteUl::parse("not-a-ul").is_err());
        assert!(SmpteUl::parse("060e2b34.04010106").is_err()); // too short
    }

    // ── AssetHash ────────────────────────────────────────────────────────────

    /// SMPTE ST 2067-2 §9: PKL assets carry base64-encoded SHA-1 digests.
    #[test]
    fn asset_hash_sha1_roundtrip() {
        // SHA-1 of empty bytes
        let b64 = "2jmj7l5rSw0yVb/vlWAYkK/YBwk=";
        let h = AssetHash::from_base64_sha1(b64).unwrap();
        assert_eq!(h.algorithm, HashAlgorithm::Sha1);
        assert_eq!(h.bytes.len(), 20);
        assert_eq!(h.to_base64(), b64);
    }

    /// SMPTE ST 2067-2 §9: SHA-1 digest must be exactly 20 bytes.
    #[test]
    fn asset_hash_sha1_wrong_length_rejected() {
        let err = AssetHash::from_base64_sha1("AAAA").unwrap_err();
        assert!(err.to_string().contains("20 bytes"));
    }

    #[test]
    fn asset_hash_sha256_roundtrip() {
        let b64 = "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=";
        let h = AssetHash::from_base64_sha256(b64).unwrap();
        assert_eq!(h.algorithm, HashAlgorithm::Sha256);
        assert_eq!(h.bytes.len(), 32);
        assert_eq!(h.to_base64(), b64);
    }

    #[test]
    fn asset_hash_sha256_wrong_length_rejected() {
        let err = AssetHash::from_base64_sha256("2jmj7l5rSw0yVb/vlWAYkK/YBwk=").unwrap_err();
        assert!(err.to_string().contains("32 bytes"));
    }

    #[test]
    fn asset_hash_from_base64_routes_correctly() {
        let sha1_b64 = "2jmj7l5rSw0yVb/vlWAYkK/YBwk=";
        let sha256_b64 = "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=";

        let h1 = AssetHash::from_base64(sha1_b64, HashAlgorithm::Sha1).unwrap();
        assert_eq!(h1.algorithm, HashAlgorithm::Sha1);

        let h2 = AssetHash::from_base64(sha256_b64, HashAlgorithm::Sha256).unwrap();
        assert_eq!(h2.algorithm, HashAlgorithm::Sha256);
    }

    #[test]
    fn asset_hash_invalid_base64() {
        assert!(AssetHash::from_base64_sha1("not-valid-base64!!!").is_err());
    }

    // ── HashAlgorithm ─────────────────────────────────────────────────────────

    /// SMPTE ST 2067-2 §9: HashAlgorithm URI parsing.
    #[test]
    fn hash_algorithm_from_uri() {
        assert_eq!(
            HashAlgorithm::from_uri("http://www.w3.org/2000/09/xmldsig#sha1"),
            Some(HashAlgorithm::Sha1)
        );
        assert_eq!(
            HashAlgorithm::from_uri("http://www.w3.org/2001/04/xmlenc#sha256"),
            Some(HashAlgorithm::Sha256)
        );
        assert_eq!(HashAlgorithm::from_uri("http://example.com/unknown"), None);
    }

    #[test]
    fn hash_algorithm_digest_len() {
        assert_eq!(HashAlgorithm::Sha1.digest_len(), 20);
        assert_eq!(HashAlgorithm::Sha256.digest_len(), 32);
    }

    #[test]
    fn hash_algorithm_display() {
        assert_eq!(HashAlgorithm::Sha1.to_string(), "SHA-1");
        assert_eq!(HashAlgorithm::Sha256.to_string(), "SHA-256");
    }

    // ── VOLINDEX ──────────────────────────────────────────────────────────────

    /// ST 429-9 §5: VOLINDEX.xml contains a single <Index> element.
    #[test]
    fn volindex_parses_index_element() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<VolumeIndex xmlns="http://www.smpte-ra.org/schemas/429-9/2007/AM">
    <Index>1</Index>
</VolumeIndex>"#;
        let result = parse_volindex(xml).unwrap();
        assert_eq!(result.index, 1);
    }

    // ── ASSETMAP ──────────────────────────────────────────────────────────────

    /// ST 429-9 §6.2: AssetMap Id must be a valid UUID URN.
    #[test]
    fn assetmap_id_is_imf_uuid() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<AssetMap xmlns="http://www.smpte-ra.org/schemas/429-9/2007/AM">
    <Id>urn:uuid:75864667-c65e-4aae-a5b2-fa5ea5fe31b7</Id>
    <AnnotationText>MERIDIAN</AnnotationText>
    <Creator>Clipster 6.1.0.0 Beta (build 111500)</Creator>
    <VolumeCount>1</VolumeCount>
    <IssueDate>2016-10-06T08:35:02-00:00</IssueDate>
    <Issuer>R&amp;S</Issuer>
    <AssetList>
        <Asset>
            <Id>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</Id>
            <ChunkList>
                <Chunk>
                    <Path>CPL_0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85.xml</Path>
                    <VolumeIndex>1</VolumeIndex>
                </Chunk>
            </ChunkList>
        </Asset>
        <Asset>
            <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
            <PackingList>true</PackingList>
            <ChunkList>
                <Chunk>
                    <Path>PKL_f5e93462-aed2-44ad-a4ba-2adb65823e7c.xml</Path>
                    <VolumeIndex>1</VolumeIndex>
                </Chunk>
            </ChunkList>
        </Asset>
    </AssetList>
</AssetMap>"#;

        let result = parse_assetmap(xml).unwrap();
        assert_eq!(
            result.id,
            ImfUuid::parse("urn:uuid:75864667-c65e-4aae-a5b2-fa5ea5fe31b7").unwrap()
        );
        assert_eq!(result.annotation_text, Some("MERIDIAN".to_string()));
        assert_eq!(result.volume_count, 1);
        assert_eq!(result.asset_list.assets.len(), 2);

        // ST 429-9 §6.3: Asset entries carry UUID references to package files.
        let cpl_asset = &result.asset_list.assets[0];
        assert_eq!(
            cpl_asset.id,
            ImfUuid::parse("urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85").unwrap()
        );
        assert_eq!(cpl_asset.packing_list, None);
        assert_eq!(
            cpl_asset.chunk_list.chunks[0].path,
            "CPL_0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85.xml"
        );

        // ST 429-9 §6.3: PackingList flag marks the PKL asset.
        let pkl_asset = &result.asset_list.assets[1];
        assert_eq!(pkl_asset.packing_list, Some(true));
        assert_eq!(
            pkl_asset.chunk_list.chunks[0].path,
            "PKL_f5e93462-aed2-44ad-a4ba-2adb65823e7c.xml"
        );
    }

    /// ST 429-9 §6.2: Invalid UUID in AssetMap <Id> yields a typed error.
    #[test]
    fn assetmap_invalid_uuid_returns_field_error() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<AssetMap xmlns="http://www.smpte-ra.org/schemas/429-9/2007/AM">
    <Id>not-a-valid-uuid</Id>
    <VolumeCount>1</VolumeCount>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <ChunkList><Chunk><Path>foo.xml</Path></Chunk></ChunkList>
    </Asset></AssetList>
</AssetMap>"#;
        let err = parse_assetmap(xml).unwrap_err();
        assert!(
            matches!(err, AssetMapParseError::Field { field: "Id", .. }),
            "expected Field error for Id, got: {err}"
        );
    }

    // ── PKL ───────────────────────────────────────────────────────────────────

    /// SMPTE ST 2067-2 §9: PKL carries SHA-1 hashes, sizes, and MIME types.
    #[test]
    fn pkl_parses_assets_with_strong_types() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <AnnotationText>MERIDIAN</AnnotationText>
    <IssueDate>2016-10-06T08:35:02-00:00</IssueDate>
    <Issuer>R&amp;S</Issuer>
    <Creator>Clipster 6.1.0.0 Beta (build 111500)</Creator>
    <AssetList>
        <Asset>
            <Id>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</Id>
            <AnnotationText>Meridian UHD 5994P</AnnotationText>
            <Hash>IW0J5IZBsAxLMCCmWtHvfHhjVUw=</Hash>
            <Size>15214</Size>
            <Type>text/xml</Type>
            <OriginalFileName>CPL_0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85.xml</OriginalFileName>
        </Asset>
        <Asset>
            <Id>urn:uuid:61d91654-2650-4abf-abbc-ad2c7f640bf8</Id>
            <Hash>fL7SnTeNskm71I4otXqr/T0D5LQ=</Hash>
            <Size>79486353</Size>
            <Type>application/mxf</Type>
            <OriginalFileName>MERIDIAN_Netflix_Photon_161006_00.mxf</OriginalFileName>
        </Asset>
    </AssetList>
</PackingList>"#;

        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.id,
            ImfUuid::parse("urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c").unwrap()
        );
        assert_eq!(result.annotation_text, Some("MERIDIAN".to_string()));
        assert_eq!(result.issuer, Some("R&S".to_string()));
        assert_eq!(result.asset_list.assets.len(), 2);

        let cpl_asset = &result.asset_list.assets[0];
        assert_eq!(cpl_asset.hash.algorithm, HashAlgorithm::Sha1);
        assert_eq!(cpl_asset.hash.bytes.len(), 20);
        assert_eq!(cpl_asset.hash.to_base64(), "IW0J5IZBsAxLMCCmWtHvfHhjVUw=");
        assert_eq!(cpl_asset.size, 15214);
        assert_eq!(cpl_asset.mime_type, MimeType::TextXml);
        assert!(cpl_asset.mime_type.is_xml());

        let mxf_asset = &result.asset_list.assets[1];
        assert_eq!(mxf_asset.mime_type, MimeType::ApplicationMxf);
        assert!(mxf_asset.mime_type.is_mxf());
    }

    /// SMPTE ST 2067-2 §9: PKL with explicit SHA-1 <HashAlgorithm> element.
    #[test]
    fn pkl_explicit_sha1_hash_algorithm() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
        <HashAlgorithm Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.asset_list.assets[0].hash.algorithm,
            HashAlgorithm::Sha1
        );
    }

    /// SMPTE ST 2067-2 §9: PKL with SHA-256 <HashAlgorithm>.
    #[test]
    fn pkl_sha256_hash_algorithm() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
        <HashAlgorithm Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.asset_list.assets[0].hash.algorithm,
            HashAlgorithm::Sha256
        );
        assert_eq!(result.asset_list.assets[0].hash.bytes.len(), 32);
    }

    /// SMPTE ST 2067-2 §9: PKL without <HashAlgorithm> defaults to SHA-1.
    #[test]
    fn pkl_missing_hash_algorithm_defaults_to_sha1() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.asset_list.assets[0].hash.algorithm,
            HashAlgorithm::Sha1
        );
    }

    /// SMPTE ST 2067-2 §9: PKL with <GroupId> for partial deliveries.
    #[test]
    fn pkl_with_group_id() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <GroupId>urn:uuid:aabbccdd-1122-3344-5566-778899aabbcc</GroupId>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.group_id,
            Some(ImfUuid::parse("urn:uuid:aabbccdd-1122-3344-5566-778899aabbcc").unwrap())
        );
    }

    /// SMPTE ST 2067-2 §9: Unrecognised MIME type is preserved as MimeType::Other.
    #[test]
    fn pkl_unknown_mime_type_preserved() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>512</Size>
        <Type>application/octet-stream</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(
            result.asset_list.assets[0].mime_type,
            MimeType::Other("application/octet-stream".to_string())
        );
    }

    // ── Namespace compatibility ──────────────────────────────────────────────

    /// ST 2067-2: PKL namespace versions must all parse identically.
    #[test]
    fn pkl_parses_with_2067_2_2016_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/2067-2/2016">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(result.asset_list.assets.len(), 1);
        assert_eq!(result.namespace, PklNamespace::Smpte2067_2_2016);
        assert_eq!(result.namespace.spec_id(), "ST 2067-2:2016");
    }

    #[test]
    fn pkl_parses_with_2067_2_2020_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/ns/2067-2/2020">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(result.namespace, PklNamespace::Smpte2067_2_2020);
    }

    #[test]
    fn pkl_detects_dci_429_8_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<PackingList xmlns="http://www.smpte-ra.org/schemas/429-8/2007/PKL">
    <Id>urn:uuid:f5e93462-aed2-44ad-a4ba-2adb65823e7c</Id>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList><Asset>
        <Id>urn:uuid:00000000-0000-0000-0000-000000000001</Id>
        <Hash>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</Hash>
        <Size>1024</Size>
        <Type>application/mxf</Type>
    </Asset></AssetList>
</PackingList>"#;
        let result = parse_pkl(xml).unwrap();
        assert_eq!(result.namespace, PklNamespace::Dci429_8);
    }

    #[test]
    fn assetmap_parses_with_2067_9_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<AssetMap xmlns="http://www.smpte-ra.org/schemas/2067-9/2016">
    <Id>urn:uuid:75864667-c65e-4aae-a5b2-fa5ea5fe31b7</Id>
    <VolumeCount>1</VolumeCount>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList>
        <Asset>
            <Id>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</Id>
            <ChunkList><Chunk><Path>test.xml</Path></Chunk></ChunkList>
        </Asset>
    </AssetList>
</AssetMap>"#;
        let result = parse_assetmap(xml).unwrap();
        assert_eq!(result.asset_list.assets.len(), 1);
        assert_eq!(result.namespace, AssetMapNamespace::Smpte2067_9_2016);
    }

    #[test]
    fn assetmap_parses_with_2020_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<AssetMap xmlns="http://www.smpte-ra.org/ns/2067-9/2020">
    <Id>urn:uuid:75864667-c65e-4aae-a5b2-fa5ea5fe31b7</Id>
    <VolumeCount>1</VolumeCount>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList>
        <Asset>
            <Id>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</Id>
            <ChunkList><Chunk><Path>test.xml</Path></Chunk></ChunkList>
        </Asset>
    </AssetList>
</AssetMap>"#;
        let result = parse_assetmap(xml).unwrap();
        assert_eq!(result.namespace, AssetMapNamespace::Smpte2067_9_2020);
    }

    #[test]
    fn assetmap_detects_dci_429_9_namespace() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<AssetMap xmlns="http://www.smpte-ra.org/schemas/429-9/2007/AM">
    <Id>urn:uuid:75864667-c65e-4aae-a5b2-fa5ea5fe31b7</Id>
    <VolumeCount>1</VolumeCount>
    <IssueDate>2024-01-01T00:00:00Z</IssueDate>
    <AssetList>
        <Asset>
            <Id>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</Id>
            <ChunkList><Chunk><Path>test.xml</Path></Chunk></ChunkList>
        </Asset>
    </AssetList>
</AssetMap>"#;
        let result = parse_assetmap(xml).unwrap();
        assert_eq!(result.namespace, AssetMapNamespace::Dci429_9);
    }

    // ── OPL ──────────────────────────────────────────────────────────────────

    /// SMPTE ST 2067-100: OPL metadata fields are parsed correctly.
    #[test]
    fn opl_parses_core_metadata() {
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<OutputProfileList xmlns="http://www.smpte-ra.org/schemas/2067-100/2014">
    <Id>urn:uuid:8cf83c32-4949-4f00-b081-01e12b18932f</Id>
    <Annotation>OPL Example</Annotation>
    <IssueDate>2016-06-14T19:22:37-00:00</IssueDate>
    <Issuer>Clipster</Issuer>
    <Creator>Clipster 5.9.3.7</Creator>
    <CompositionPlaylistId>urn:uuid:0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85</CompositionPlaylistId>
    <AliasList/>
    <MacroList/>
</OutputProfileList>"#;
        let result = parse_opl(xml).unwrap();
        assert_eq!(
            result.id.to_string(),
            "8cf83c32-4949-4f00-b081-01e12b18932f"
        );
        assert_eq!(result.annotation.as_deref(), Some("OPL Example"));
        assert_eq!(result.issuer.as_deref(), Some("Clipster"));
        assert_eq!(result.creator.as_deref(), Some("Clipster 5.9.3.7"));
        assert_eq!(
            result.composition_playlist_id.to_string(),
            "0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85"
        );
    }

    /// SMPTE ST 2067-100: OPL with complex macros parses without error.
    #[test]
    fn opl_parses_real_test_file() {
        let xml = std::fs::read_to_string(test_data(
            "OPL/OPL_8cf83c32-4949-4f00-b081-01e12b18932f.xml",
        ))
        .unwrap();
        let result = parse_opl(&xml).unwrap();
        assert_eq!(
            result.id.to_string(),
            "8cf83c32-4949-4f00-b081-01e12b18932f"
        );
        assert_eq!(
            result.composition_playlist_id.to_string(),
            "0eb3d1b9-b77b-4d3f-bbe5-7c69b15dca85"
        );
    }

    /// SMPTE ST 2067-100: OPL with simple preset macro from ISXD test data.
    #[test]
    fn opl_parses_isxd_test_file() {
        let xml = std::fs::read_to_string(test_data(
            "ISXD/CompleteIMP/OPL_af6b288d-27e8-441f-9a36-2c4ab9025d19.xml",
        ))
        .unwrap();
        let result = parse_opl(&xml).unwrap();
        assert_eq!(
            result.id.to_string(),
            "af6b288d-27e8-441f-9a36-2c4ab9025d19"
        );
        assert_eq!(
            result.composition_playlist_id.to_string(),
            "b2d74f92-1990-41e0-869f-2179a50f7090"
        );
    }
}