mako-as4 0.20.0

BDEW MaKo AS4 profile — AS4/ebMS3 transport for German energy market communication
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
//! BDEW MaKo AS4 P-Mode types and factory functions.
//!
//! In ebMS3, a **P-Mode** specifies the complete protocol configuration for a
//! trading relationship: MEP, security, service/action URIs, payload packaging,
//! and the partner's HTTPS endpoint URL.
//!
//! Use [`bdew_pmode`] to build pre-configured [`PMode`]s with BDEW defaults,
//! then register them with a [`PModeRegistry`]:
//!
//! ```rust
//! use mako_as4::pmode::{bdew_pmode, bdew_pmode_with_endpoint, BdewAction, PModeRegistry};
//!
//! // Without endpoint (endpoint supplied at send time)
//! let mut registry = PModeRegistry::new();
//! registry.register(bdew_pmode(
//!     "pm-utilmd-9900000000001",
//!     "9900000000001",             // counterparty GLN
//!     BdewAction::Utilmd,
//! ));
//! assert_eq!(registry.len(), 1);
//!
//! // With endpoint baked into the P-Mode (recommended for static configurations)
//! let pm = bdew_pmode_with_endpoint(
//!     "pm-aperak-9900000000001",
//!     "9900000000001",
//!     BdewAction::Aperak,
//!     "https://partner.example/as4/inbox",
//! );
//! assert_eq!(pm.endpoint_url.as_deref(), Some("https://partner.example/as4/inbox"));
//! ```

use std::fmt;
use std::str::FromStr;

use crate::constants;

// Re-export `asx_rs` P-Mode types so consumers don't need a direct `asx_rs` dep
// for the common registry / P-Mode operations.
pub use asx_rs::as4::pmode::{MepType, PMode, PModeRegistry, PModeSecurity, PayloadPackagingMode};
pub use asx_rs::crypto::wssec::WsSecOutboundKeyInfoProfile;

// ── BdewAction ────────────────────────────────────────────────────────────────

/// EDIFACT message types used in BDEW German energy market communication.
///
/// Each variant maps to an AS4 `<eb:Action>` URI in the form
/// `{SERVICE}:{EDIFACT_TYPE}` where `SERVICE = "urn:bdew:as4:service"`.
///
/// ## Conversion methods
///
/// | Method | Returns | Example |
/// |---|---|---|
/// | [`as_edifact_type()`] | `&str` — EDIFACT type name | `"UTILMD"` |
/// | [`as_uri()`] | `String` — full AS4 action URI | `"urn:bdew:as4:service:UTILMD"` |
/// | [`fmt::Display`] | EDIFACT type name | `"UTILMD"` |
/// | [`FromStr`] | parse from EDIFACT type string | `"APERAK".parse()` |
///
/// ## Coverage
///
/// All 16 standard BDEW EDIFACT message types (UTILMD, APERAK, CONTRL, MSCONS,
/// INVOIC, REMADV, IFTSTA, ORDRSP, ORDERS, ORDCHG, REQOTE, INSRPT, PRICAT,
/// QUOTES, PARTIN, UTILTS) are covered by named variants. Any other type maps
/// to [`Custom`], which stores the full action URI verbatim.
///
/// [`as_edifact_type()`]: Self::as_edifact_type
/// [`as_uri()`]: Self::as_uri
/// [`Custom`]: Self::Custom
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BdewAction {
    /// UTILMD — market location master data (GPKE, WiM, GeLi Gas, WiM Gas).
    Utilmd,
    /// APERAK — application error acknowledgement (all processes).
    Aperak,
    /// CONTRL — interchange/message syntax acknowledgement (all processes).
    Contrl,
    /// MSCONS — metered service consumption; meter readings / time series data.
    Mscons,
    /// INVOIC — invoice (GPKE NNE, WiM MSB, GeLi Gas AWH, MABIS).
    Invoic,
    /// REMADV — remittance advice; payment confirmation (GPKE, MABIS billing).
    Remadv,
    /// IFTSTA — interchange transport status; GPKE Sperrung / Entsperrung confirmation.
    Iftsta,
    /// ORDRSP — order response (GPKE Konfiguration, WiM Geräteübernahme).
    Ordrsp,
    /// ORDERS — order / commissioning request (WiM Geräteübernahme, GPKE Sperrung).
    Orders,
    /// ORDCHG — order change (WiM Stornierung, GeLi Gas AWH Stornierung).
    Ordchg,
    /// REQOTE — request for quotation (WiM Preisanfrage MSB).
    Reqote,
    /// INSRPT — inspection report (WiM Ablesesteuerung / Gerätebefund).
    Insrpt,
    /// PRICAT — price catalogue (WiM Preisliste MSB).
    Pricat,
    /// QUOTES — quote response (WiM Preisanfrage MSB response).
    Quotes,
    /// PARTIN — party information; BDEW Kommunikationsdaten exchange
    /// (GPKE PIDs 37000–37006, GeLi Gas PIDs 37008–37014).
    Partin,
    /// UTILTS — utility time series; GPKE UTILTS Konfigurationsdaten and
    /// MaBiS Summenzeitreihen exchange (ÜNB ↔ BIKO).
    Utilts,
    /// Custom action URI for non-standard or future EDIFACT message types.
    ///
    /// Stores the **full** AS4 action URI (e.g. `"urn:bdew:as4:service:SLSFCT"`),
    /// not just the type name. Use [`BdewAction::custom`] to build it from a
    /// type name string, or supply the full URI directly.
    Custom(String),
}

impl BdewAction {
    /// Build a [`Custom`] action from an EDIFACT type name string.
    ///
    /// Composes `{SERVICE}:{type_name}` (e.g. `"SLSFCT"` →
    /// `"urn:bdew:as4:service:SLSFCT"`).
    ///
    /// Use when the type name is not covered by a named variant. For known
    /// types prefer the named variant (e.g. [`BdewAction::Utilmd`]).
    ///
    /// [`Custom`]: Self::Custom
    #[must_use]
    pub fn custom(type_name: impl Into<String>) -> Self {
        Self::Custom(format!(
            "{}:{}",
            crate::constants::SERVICE,
            type_name.into()
        ))
    }

    /// Returns all 16 standard (non-[`Custom`]) BDEW action variants.
    ///
    /// Useful for registering bilateral P-Modes for every known BDEW EDIFACT
    /// message type in one call:
    ///
    /// ```rust
    /// use mako_as4::pmode::{bdew_pmode, BdewAction, PModeRegistry};
    ///
    /// let mut registry = PModeRegistry::new();
    /// for action in BdewAction::all_standard() {
    ///     registry.register(bdew_pmode(
    ///         format!("pm-{}-partner-a", action),
    ///         "9900000000001",
    ///         action,
    ///     ));
    /// }
    /// assert_eq!(registry.len(), 16);
    /// ```
    ///
    /// See also [`BdewAs4Profile::register_partner_all_actions`] which wraps
    /// this in a single call.
    ///
    /// [`Custom`]: Self::Custom
    /// [`BdewAs4Profile::register_partner_all_actions`]: crate::profile::BdewAs4Profile::register_partner_all_actions
    #[must_use]
    pub fn all_standard() -> Vec<Self> {
        vec![
            Self::Utilmd,
            Self::Aperak,
            Self::Contrl,
            Self::Mscons,
            Self::Invoic,
            Self::Remadv,
            Self::Iftsta,
            Self::Ordrsp,
            Self::Orders,
            Self::Ordchg,
            Self::Reqote,
            Self::Insrpt,
            Self::Pricat,
            Self::Quotes,
            Self::Partin,
            Self::Utilts,
        ]
    }

    /// Returns the EDIFACT message-type name for standard variants.
    ///
    /// For `Custom` variants the stored URI string is returned unchanged (it
    /// may be a full URI or a bare type name depending on how the value was
    /// constructed).
    ///
    /// This method avoids a heap allocation compared to [`as_uri()`] when only
    /// the type name is needed (e.g. for logging or EDIFACT header construction).
    ///
    /// ```rust
    /// use mako_as4::pmode::BdewAction;
    ///
    /// assert_eq!(BdewAction::Utilmd.as_edifact_type(), "UTILMD");
    /// assert_eq!(BdewAction::Partin.as_edifact_type(), "PARTIN");
    /// assert_eq!(BdewAction::Utilts.as_edifact_type(), "UTILTS");
    /// ```
    ///
    /// [`as_uri()`]: Self::as_uri
    #[must_use]
    pub fn as_edifact_type(&self) -> &str {
        match self {
            Self::Utilmd => "UTILMD",
            Self::Aperak => "APERAK",
            Self::Contrl => "CONTRL",
            Self::Mscons => "MSCONS",
            Self::Invoic => "INVOIC",
            Self::Remadv => "REMADV",
            Self::Iftsta => "IFTSTA",
            Self::Ordrsp => "ORDRSP",
            Self::Orders => "ORDERS",
            Self::Ordchg => "ORDCHG",
            Self::Reqote => "REQOTE",
            Self::Insrpt => "INSRPT",
            Self::Pricat => "PRICAT",
            Self::Quotes => "QUOTES",
            Self::Partin => "PARTIN",
            Self::Utilts => "UTILTS",
            // Custom stores the full URI; return as-is.
            Self::Custom(uri) => uri.as_str(),
        }
    }

    /// Returns the full BDEW AS4 action URI.
    ///
    /// Format: `"urn:bdew:as4:service:{EDIFACT_TYPE}"`
    ///
    /// For `Custom` variants the stored URI is returned as-is.
    ///
    /// ```rust
    /// use mako_as4::pmode::BdewAction;
    ///
    /// assert_eq!(BdewAction::Utilmd.as_uri(), "urn:bdew:as4:service:UTILMD");
    /// assert_eq!(BdewAction::Partin.as_uri(), "urn:bdew:as4:service:PARTIN");
    /// assert_eq!(BdewAction::Utilts.as_uri(), "urn:bdew:as4:service:UTILTS");
    ///
    /// let uri = "urn:custom:action:FOO";
    /// assert_eq!(BdewAction::Custom(uri.to_string()).as_uri(), uri);
    /// ```
    #[must_use]
    pub fn as_uri(&self) -> String {
        match self {
            Self::Custom(uri) => uri.clone(),
            _ => format!("{}:{}", constants::SERVICE, self.as_edifact_type()),
        }
    }
}

// ── fmt::Display ─────────────────────────────────────────────────────────────

/// Formats the EDIFACT type name (e.g. `"UTILMD"`, `"APERAK"`).
///
/// For `Custom` variants the stored URI string is displayed unchanged.
impl fmt::Display for BdewAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_edifact_type())
    }
}

// ── FromStr ───────────────────────────────────────────────────────────────────

/// Parse a [`BdewAction`] from an EDIFACT message-type name string.
///
/// Matches all 16 standard BDEW type names case-sensitively. Any unrecognised
/// string maps to [`BdewAction::custom`] — the parse never fails, so delivery
/// is attempted rather than being rejected at parse time.
///
/// [`ParseBdewActionError`] is an uninhabited enum provided for API completeness.
///
/// ```rust
/// use mako_as4::pmode::BdewAction;
///
/// assert_eq!("UTILMD".parse::<BdewAction>().unwrap(), BdewAction::Utilmd);
/// assert_eq!("PARTIN".parse::<BdewAction>().unwrap(), BdewAction::Partin);
/// assert_eq!("UTILTS".parse::<BdewAction>().unwrap(), BdewAction::Utilts);
///
/// // Unknown type — maps to Custom (never an Err)
/// let action: BdewAction = "SLSFCT".parse().unwrap();
/// assert!(matches!(action, BdewAction::Custom(_)));
/// ```
impl FromStr for BdewAction {
    type Err = ParseBdewActionError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let action = match s {
            "UTILMD" => Self::Utilmd,
            "APERAK" => Self::Aperak,
            "CONTRL" => Self::Contrl,
            "MSCONS" => Self::Mscons,
            "INVOIC" => Self::Invoic,
            "REMADV" => Self::Remadv,
            "IFTSTA" => Self::Iftsta,
            "ORDRSP" => Self::Ordrsp,
            "ORDERS" => Self::Orders,
            "ORDCHG" => Self::Ordchg,
            "REQOTE" => Self::Reqote,
            "INSRPT" => Self::Insrpt,
            "PRICAT" => Self::Pricat,
            "QUOTES" => Self::Quotes,
            "PARTIN" => Self::Partin,
            "UTILTS" => Self::Utilts,
            other => Self::custom(other),
        };
        Ok(action)
    }
}

/// Uninhabited error type for [`BdewAction`]'s [`FromStr`] implementation.
///
/// `BdewAction::from_str` never returns `Err` — unrecognised strings fall
/// through to [`BdewAction::Custom`].  This type exists purely to satisfy the
/// [`FromStr`] associated-type constraint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseBdewActionError {}

impl fmt::Display for ParseBdewActionError {
    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Uninhabited — never reached.
        unreachable!()
    }
}

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

// ── Convenience free function ─────────────────────────────────────────────────

/// Construct a [`BdewAction`] from an EDIFACT message-type name string.
///
/// Equivalent to `message_type.parse::<BdewAction>().unwrap()`.
///
/// Provided as a free function for call sites (e.g. the outbox delivery loop)
/// that need to convert a raw EDIFACT type string into an action without
/// importing the [`FromStr`] trait.
#[must_use]
#[inline]
pub fn bdew_action_from_str(message_type: &str) -> BdewAction {
    // Safety: FromStr for BdewAction is infallible.
    message_type
        .parse::<BdewAction>()
        .unwrap_or_else(|_| BdewAction::custom(message_type))
}

// ── P-Mode factory functions ──────────────────────────────────────────────────

/// Build a BDEW MaKo P-Mode with BDEW AS4-Profil v1.2 compliant security defaults.
///
/// The returned [`PMode`] is pre-configured with:
///
/// | Field | Value | BDEW source |
/// |---|---|---|
/// | `mep` | [`MepType::OneWayPush`] | AS4-Profil §2.2 |
/// | `service` | [`constants::SERVICE`] | AS4-Profil §3.1 |
/// | `service_type` | `""` (omitted) | AS4-Profil §3.1 |
/// | `security.sign` | `true` — ECDSA-SHA256 + BrainpoolP256r1 | §2.2.6.2.1, BSI TR-03116-3 §9.1 |
/// | `security.encrypt` | `true` — ECDH-ES + ConcatKDF + AES-128-GCM | §2.2.6.2.2, BSI TR-03116-3 §9.2 |
/// | `security.outbound_key_info_profile` | [`X509PKIPathv1`] | §2.2.6.2.1 |
/// | `security.compress` | `true` — `application/gzip` | §2.2.3.2, §2.2.3.3 |
/// | `payload_packaging` | [`MimeAttachment`] — SwA, empty SOAP Body | §2.2.3.2 |
/// | `endpoint_url` | `None` | use [`bdew_pmode_with_endpoint`] |
///
/// ## Algorithm auto-detection
///
/// The WS-Security algorithm is selected automatically from the key material
/// supplied to the `asx_rs` `SessionContext`:
/// - EC key (BrainpoolP256r1) → ECDSA-SHA256 (**BDEW-compliant**)
/// - RSA key → RSA-SHA256 (*not* BDEW-compliant — use only for local testing)
///
/// The key-agreement algorithm is selected from the **recipient** certificate
/// at send time:
/// - EC certificate → ECDH-ES + ConcatKDF + AES-128-GCM (**BDEW-compliant**)
/// - RSA certificate → RSA-OAEP (*not* BDEW-compliant)
///
/// Supply BrainpoolP256r1 credentials for production. Use
/// [`crate::testing::BdewTestPki`] to generate ephemeral test keypairs.
///
/// [`X509PKIPathv1`]: WsSecOutboundKeyInfoProfile::X509PKIPathv1
/// [`MimeAttachment`]: PayloadPackagingMode::MimeAttachment
///
/// # Example
///
/// ```rust
/// use mako_as4::pmode::{bdew_pmode, BdewAction, PModeRegistry};
///
/// let mut registry = PModeRegistry::new();
/// registry.register(bdew_pmode(
///     "pm-utilmd-9900000000001",
///     "9900000000001",
///     BdewAction::Utilmd,
/// ));
/// ```
#[must_use]
pub fn bdew_pmode(
    id: impl Into<String>,
    partner_mp_id: impl Into<String>,
    action: BdewAction,
) -> PMode {
    PMode {
        id: id.into(),
        partner_id: partner_mp_id.into(),
        service: constants::SERVICE.to_string(),
        service_type: constants::SERVICE_TYPE.to_string(),
        action: action.as_uri(),
        mep: MepType::OneWayPush,
        security: PModeSecurity {
            sign: true,
            // Mandatory per BDEW AS4-Profil v1.2 §2.2.6.2.2.
            // `recipient_cert_pem` must be set in `As4SendCredentials` at send time.
            encrypt: true,
            encrypt_soap_headers: false,
            // Mandatory per §2.2.3.2/§2.2.3.3: this profile always compresses,
            // which is why the payload is binary in its own MIME part and the
            // SOAP Body is empty. `PayloadService.CompressionType` is
            // `application/gzip`, emitted as a signed PartProperty so the
            // receiver knows to decompress after decrypting.
            compress: true,
            // X509PKIPathv1 BST token type — mandatory per §2.2.6.2.1.
            outbound_key_info_profile: WsSecOutboundKeyInfoProfile::X509PKIPathv1,
        },
        payload_packaging: PayloadPackagingMode::MimeAttachment,
        endpoint_url: None,
    }
}

/// Build a BDEW MaKo P-Mode with the partner's HTTPS AS4 endpoint baked in.
///
/// Identical to [`bdew_pmode`] but sets `endpoint_url` so the P-Mode is
/// self-contained for outbound delivery — no separate `PartnerDirectory`
/// lookup is required.
///
/// Both signing and encryption are enabled per BDEW AS4-Profil v1.2 §2.2.6.
/// Supply `recipient_cert_pem` in [`asx_rs::as4::As4SendCredentials`] at send time.
///
/// # Example
///
/// ```rust
/// use mako_as4::pmode::{bdew_pmode_with_endpoint, BdewAction};
///
/// let pm = bdew_pmode_with_endpoint(
///     "pm-utilmd-9900000000001",
///     "9900000000001",
///     BdewAction::Utilmd,
///     "https://partner.example/as4/inbox",
/// );
/// assert_eq!(pm.endpoint_url.as_deref(), Some("https://partner.example/as4/inbox"));
/// assert!(pm.security.sign);
/// assert!(pm.security.encrypt);
/// ```
#[must_use]
pub fn bdew_pmode_with_endpoint(
    id: impl Into<String>,
    partner_mp_id: impl Into<String>,
    action: BdewAction,
    endpoint_url: impl Into<String>,
) -> PMode {
    PMode {
        endpoint_url: Some(endpoint_url.into()),
        ..bdew_pmode(id, partner_mp_id, action)
    }
}

/// Build a sign-only BDEW MaKo P-Mode (development / testing only).
///
/// Identical to [`bdew_pmode`] but sets `security.encrypt = false`.
///
/// ## ⚠ Non-BDEW-compliant — do not use in production
///
/// BDEW AS4-Profil v1.2 §2.2.6.2.2 **requires** encryption for every production
/// message. Use this variant only in:
///
/// - Local development without WIRK certificates
/// - Tests that mock the AS4 transport entirely
/// - Pre-production bilateral test agreements where both parties opt out
///
/// For production deployments use [`bdew_pmode`] (sign + encrypt).
///
/// # Example
///
/// ```rust
/// use mako_as4::pmode::{bdew_pmode_sign_only, BdewAction};
///
/// let pm = bdew_pmode_sign_only(
///     "pm-dev-9900000000001",
///     "9900000000001",
///     BdewAction::Utilmd,
/// );
/// assert!(pm.security.sign);
/// assert!(!pm.security.encrypt, "sign-only: not BDEW-compliant, dev/test only");
/// ```
#[must_use]
pub fn bdew_pmode_sign_only(
    id: impl Into<String>,
    partner_mp_id: impl Into<String>,
    action: BdewAction,
) -> PMode {
    PMode {
        security: PModeSecurity {
            sign: true,
            encrypt: false,
            encrypt_soap_headers: false,
            // Mandatory per §2.2.3.2/§2.2.3.3: this profile always compresses,
            // which is why the payload is binary in its own MIME part and the
            // SOAP Body is empty. `PayloadService.CompressionType` is
            // `application/gzip`, emitted as a signed PartProperty so the
            // receiver knows to decompress after decrypting.
            compress: true,
            outbound_key_info_profile: WsSecOutboundKeyInfoProfile::X509PKIPathv1,
        },
        ..bdew_pmode(id, partner_mp_id, action)
    }
}

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

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

    // ── BdewAction::as_edifact_type ───────────────────────────────────────────

    #[test]
    fn as_edifact_type_all_standard() {
        assert_eq!(BdewAction::Utilmd.as_edifact_type(), "UTILMD");
        assert_eq!(BdewAction::Aperak.as_edifact_type(), "APERAK");
        assert_eq!(BdewAction::Contrl.as_edifact_type(), "CONTRL");
        assert_eq!(BdewAction::Mscons.as_edifact_type(), "MSCONS");
        assert_eq!(BdewAction::Invoic.as_edifact_type(), "INVOIC");
        assert_eq!(BdewAction::Remadv.as_edifact_type(), "REMADV");
        assert_eq!(BdewAction::Iftsta.as_edifact_type(), "IFTSTA");
        assert_eq!(BdewAction::Ordrsp.as_edifact_type(), "ORDRSP");
        assert_eq!(BdewAction::Orders.as_edifact_type(), "ORDERS");
        assert_eq!(BdewAction::Ordchg.as_edifact_type(), "ORDCHG");
        assert_eq!(BdewAction::Reqote.as_edifact_type(), "REQOTE");
        assert_eq!(BdewAction::Insrpt.as_edifact_type(), "INSRPT");
        assert_eq!(BdewAction::Pricat.as_edifact_type(), "PRICAT");
        assert_eq!(BdewAction::Quotes.as_edifact_type(), "QUOTES");
        assert_eq!(BdewAction::Partin.as_edifact_type(), "PARTIN");
        assert_eq!(BdewAction::Utilts.as_edifact_type(), "UTILTS");
    }

    // ── BdewAction::as_uri ────────────────────────────────────────────────────

    #[test]
    fn as_uri_utilmd() {
        assert_eq!(BdewAction::Utilmd.as_uri(), "urn:bdew:as4:service:UTILMD");
    }

    #[test]
    fn as_uri_aperak() {
        assert_eq!(BdewAction::Aperak.as_uri(), "urn:bdew:as4:service:APERAK");
    }

    #[test]
    fn as_uri_partin() {
        assert_eq!(BdewAction::Partin.as_uri(), "urn:bdew:as4:service:PARTIN");
    }

    #[test]
    fn as_uri_utilts() {
        assert_eq!(BdewAction::Utilts.as_uri(), "urn:bdew:as4:service:UTILTS");
    }

    #[test]
    fn as_uri_custom_passthrough() {
        let uri = "urn:custom:action:FOO";
        assert_eq!(BdewAction::Custom(uri.to_string()).as_uri(), uri);
    }

    // ── BdewAction::all_standard ──────────────────────────────────────────────

    #[test]
    fn all_standard_has_16_variants() {
        assert_eq!(BdewAction::all_standard().len(), 16);
    }

    #[test]
    fn all_standard_no_duplicates() {
        let v = BdewAction::all_standard();
        let uris: std::collections::HashSet<String> = v.iter().map(|a| a.as_uri()).collect();
        assert_eq!(
            uris.len(),
            v.len(),
            "all_standard() must not contain duplicate URIs"
        );
    }

    #[test]
    fn all_standard_contains_partin_and_utilts() {
        let v = BdewAction::all_standard();
        assert!(
            v.contains(&BdewAction::Partin),
            "all_standard must include Partin"
        );
        assert!(
            v.contains(&BdewAction::Utilts),
            "all_standard must include Utilts"
        );
    }

    // ── BdewAction::custom ────────────────────────────────────────────────────

    #[test]
    fn custom_builds_full_uri() {
        let action = BdewAction::custom("SLSFCT");
        assert_eq!(action.as_uri(), "urn:bdew:as4:service:SLSFCT");
    }

    // ── fmt::Display ──────────────────────────────────────────────────────────

    #[test]
    fn display_shows_edifact_type_name() {
        assert_eq!(BdewAction::Utilmd.to_string(), "UTILMD");
        assert_eq!(BdewAction::Partin.to_string(), "PARTIN");
        assert_eq!(BdewAction::Utilts.to_string(), "UTILTS");
    }

    // ── FromStr ───────────────────────────────────────────────────────────────

    #[test]
    fn from_str_all_standard_roundtrip() {
        for action in BdewAction::all_standard() {
            let type_name = action.as_edifact_type();
            let parsed: BdewAction = type_name.parse().unwrap();
            assert_eq!(
                parsed, action,
                "from_str({type_name}) did not round-trip through as_edifact_type"
            );
        }
    }

    #[test]
    fn from_str_partin() {
        let a: BdewAction = "PARTIN".parse().unwrap();
        assert_eq!(a, BdewAction::Partin);
    }

    #[test]
    fn from_str_utilts() {
        let a: BdewAction = "UTILTS".parse().unwrap();
        assert_eq!(a, BdewAction::Utilts);
    }

    #[test]
    fn from_str_unknown_maps_to_custom_not_error() {
        let a: BdewAction = "SLSFCT".parse().unwrap();
        assert!(
            matches!(a, BdewAction::Custom(_)),
            "Unknown type must map to Custom, not Err"
        );
        assert_eq!(a.as_uri(), "urn:bdew:as4:service:SLSFCT");
    }

    // ── bdew_action_from_str free function ────────────────────────────────────

    #[test]
    fn bdew_action_from_str_helper() {
        assert_eq!(bdew_action_from_str("UTILMD"), BdewAction::Utilmd);
        assert_eq!(bdew_action_from_str("PARTIN"), BdewAction::Partin);
        assert_eq!(bdew_action_from_str("UTILTS"), BdewAction::Utilts);
        assert!(matches!(
            bdew_action_from_str("UNKNOWN"),
            BdewAction::Custom(_)
        ));
    }

    // ── bdew_pmode factory ────────────────────────────────────────────────────

    #[test]
    fn bdew_pmode_defaults() {
        let pm = bdew_pmode("pm-1", "9900000000001", BdewAction::Utilmd);
        assert_eq!(pm.partner_id, "9900000000001");
        assert_eq!(pm.service, constants::SERVICE);
        assert_eq!(pm.service_type, "");
        assert_eq!(pm.action, BdewAction::Utilmd.as_uri());
        assert_eq!(pm.mep, MepType::OneWayPush);
        assert!(pm.security.sign);
        assert!(
            pm.security.encrypt,
            "BDEW AS4-Profil v1.2 §2.2.6.2.2 requires encryption"
        );
        assert!(
            pm.security.compress,
            "BDEW AS4-Profil v1.2 §2.2.3.2/§2.2.3.3 make AS4 compression mandatory — \
             it is why the payload is binary in its own part and the SOAP Body is empty"
        );
        assert_eq!(pm.payload_packaging, PayloadPackagingMode::MimeAttachment);
        assert!(
            pm.endpoint_url.is_none(),
            "bdew_pmode leaves endpoint_url unset"
        );
    }

    #[test]
    fn bdew_pmode_with_endpoint_sets_url() {
        let url = "https://partner.example/as4/inbox";
        let pm = bdew_pmode_with_endpoint("pm-1", "9900000000001", BdewAction::Utilmd, url);
        assert_eq!(pm.endpoint_url.as_deref(), Some(url));
        assert!(pm.security.sign);
        assert!(
            pm.security.encrypt,
            "bdew_pmode_with_endpoint inherits encrypt:true from bdew_pmode"
        );
    }

    #[test]
    fn bdew_pmode_sign_only_disables_encrypt() {
        let pm = bdew_pmode_sign_only("pm-dev", "9900000000001", BdewAction::Utilmd);
        assert!(pm.security.sign);
        assert!(!pm.security.encrypt, "sign_only must have encrypt:false");
        assert_eq!(pm.mep, MepType::OneWayPush);
        assert!(pm.endpoint_url.is_none());
    }

    #[test]
    fn bdew_pmode_partin_action() {
        let pm = bdew_pmode("pm-partin", "9900000000001", BdewAction::Partin);
        assert_eq!(pm.action, "urn:bdew:as4:service:PARTIN");
    }

    #[test]
    fn bdew_pmode_utilts_action() {
        let pm = bdew_pmode("pm-utilts", "9900000000001", BdewAction::Utilts);
        assert_eq!(pm.action, "urn:bdew:as4:service:UTILTS");
    }

    // ── PModeRegistry resolution ──────────────────────────────────────────────

    #[test]
    fn pmode_registry_resolves_by_partner_and_action() {
        let mut registry = PModeRegistry::new();
        registry.register(bdew_pmode("pm-u", "9900000000001", BdewAction::Utilmd));
        registry.register(bdew_pmode("pm-a", "9900000000001", BdewAction::Aperak));

        let pm = registry.resolve(
            "9900000000001",
            constants::SERVICE,
            &BdewAction::Utilmd.as_uri(),
        );
        assert!(pm.is_some());
        assert_eq!(pm.unwrap().id, "pm-u");

        // Different partner — not found
        assert!(
            registry
                .resolve(
                    "9900000000002",
                    constants::SERVICE,
                    &BdewAction::Utilmd.as_uri(),
                )
                .is_none()
        );
    }

    #[test]
    fn pmode_registry_for_all_standard_actions() {
        let mut registry = PModeRegistry::new();
        for action in BdewAction::all_standard() {
            let id = format!("pm-{}-partner", action);
            registry.register(bdew_pmode(id, "9900000000001", action.clone()));
        }
        // Spot-check newly added variants
        assert!(
            registry
                .resolve(
                    "9900000000001",
                    constants::SERVICE,
                    &BdewAction::Partin.as_uri()
                )
                .is_some(),
            "PARTIN P-Mode must resolve"
        );
        assert!(
            registry
                .resolve(
                    "9900000000001",
                    constants::SERVICE,
                    &BdewAction::Utilts.as_uri()
                )
                .is_some(),
            "UTILTS P-Mode must resolve"
        );
    }
}