metering 0.25.0

German energy metering quantities: DST-correct Europe/Berlin and 06:00-Gastag calendar arithmetic, exact decimal values, and the market's own code lists. Zero I/O, no async, no clock.
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
//! § 42c EnWG Energy Sharing: whether a delivery point can take part,
//! assessed from master data and from the series it delivers.
//!
//! § 42c EnWG is in force since 22 December 2025 (BGBl. 2025 I Nr. 347); Abs. 4
//! obliges every Verteilernetzbetreiber to enable sharing from 1 June 2026
//! *"innerhalb des Bilanzierungsgebietes eines
//! Elektrizitätsverteilernetzbetreibers"* and from 1 June 2028 also into a
//! directly adjacent Bilanzierungsgebiet in the same Regelzone.
//!
//! Abs. 1 admits a point only when consumption *and* generation are measured
//! by:
//!
//! > „Zählerstandsgangmessung nach § 2 Satz 1 Nummer 27 des
//! > Messstellenbetriebsgesetzes **oder** durch eine viertelstündliche
//! > registrierende Leistungsmessung"
//!
//! Two independent bases ([`EligibilityBasis`]): a conventional RLM meter
//! qualifies without an iMSys, and an iMSys not configured for
//! Zählerstandsgangmessung delivers no quarter-hour series.
//!
//! | Step | Question | Input |
//! |---|---|---|
//! | [`assess_capability`] | Can the point produce quarter-hour values? | device master data |
//! | [`assess_delivery`] | Is it producing them? | observed series |
//! | [`combine_readiness`] | What must the operator do? | both verdicts |
//!
//! Definitions (§ 2 Satz 1 MsbG):
//!
//! - **Nr. 27 Zählerstandsgangmessung**: „die Messung einer
//!   Reihe viertelstündig ermittelter Zählerstände von elektrischer Arbeit und
//!   stündlich ermittelter Zählerstände von Gasmengen". § 42c is Strom-only.
//! - **Nr. 7 intelligentes Messsystem (iMSys)**: a moderne Messeinrichtung or
//!   an RLM meter bound into a communication network via a Smart-Meter-Gateway.
//! - **Nr. 15 moderne Messeinrichtung (mME)**: no gateway, no interval series;
//!   not sufficient on its own.

use crate::series::Series;
use crate::time::calendar::Period;
use crate::time::resolution::Resolution;

// ── Qualifying basis ──────────────────────────────────────────────────────────

/// The statutory basis on which a delivery point qualifies under § 42c Abs. 1.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EligibilityBasis {
    /// Zählerstandsgangmessung per § 2 Satz 1 Nr. 27 MsbG — the iMSys route.
    Zaehlerstandsgangmessung,
    /// Viertelstündliche registrierende Leistungsmessung — the RLM route; needs
    /// no Smart-Meter-Gateway.
    RegistrierendeLeistungsmessung,
}

impl EligibilityBasis {
    /// Every basis, in declaration order.
    pub const ALL: [Self; 2] = [
        Self::Zaehlerstandsgangmessung,
        Self::RegistrierendeLeistungsmessung,
    ];

    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Zaehlerstandsgangmessung => "ZAEHLERSTANDSGANGMESSUNG",
            Self::RegistrierendeLeistungsmessung => "REGISTRIERENDE_LEISTUNGSMESSUNG",
        }
    }

    /// The statutory citation this basis rests on.
    #[must_use]
    pub const fn legal_basis(self) -> &'static str {
        match self {
            Self::Zaehlerstandsgangmessung => "§2 Satz 1 Nr. 27 MsbG",
            Self::RegistrierendeLeistungsmessung => "§42c Abs. 1 EnWG",
        }
    }
}

// ── Findings ──────────────────────────────────────────────────────────────────

/// Why an assessment reached the verdict it did — a code to match on;
/// [`description_de`](Self::description_de) renders it.
///
/// Exhaustive on purpose: each finding routes to an action, so a new one
/// breaks a consumer's `match`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Finding {
    /// The meter is flagged as not remotely readable, so no series of
    /// viertelstündig ermittelte Zählerstände can be transmitted.
    NotRemotelyReadable,
    /// The Smart-Meter-Gateway is not operational, so the iMSys transmits
    /// nothing.
    GatewayNotOperational,
    /// A moderne Messeinrichtung has no gateway and no interval series
    /// (§ 2 Satz 1 Nr. 15 MsbG).
    ModerneMesseinrichtungWithoutGateway,
    /// The Bilanzierungsmethode yields no quarter-hour values.
    BalancingMethodHasNoQuarterHourValues,
    /// The meter type is neither an iMSys nor covered by RLM.
    MeterTypeQualifiesForNeitherLimb,
    /// No Zählertyp in the master data.
    ZaehlertypMissing,
    /// No Bilanzierungsmethode on the Marktlokation.
    BilanzierungsmethodeMissing,
    /// No readings at all in the observation window.
    NoReadings,
    /// Readings arrived, but not at quarter-hour resolution.
    NotQuarterHourResolution,
    /// Coverage is below the configured threshold.
    CoverageBelowThreshold,
}

impl Finding {
    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NotRemotelyReadable => "NOT_REMOTELY_READABLE",
            Self::GatewayNotOperational => "GATEWAY_NOT_OPERATIONAL",
            Self::ModerneMesseinrichtungWithoutGateway => "MODERNE_MESSEINRICHTUNG_WITHOUT_GATEWAY",
            Self::BalancingMethodHasNoQuarterHourValues => {
                "BALANCING_METHOD_HAS_NO_QUARTER_HOUR_VALUES"
            }
            Self::MeterTypeQualifiesForNeitherLimb => "METER_TYPE_QUALIFIES_FOR_NEITHER_LIMB",
            Self::ZaehlertypMissing => "ZAEHLERTYP_MISSING",
            Self::BilanzierungsmethodeMissing => "BILANZIERUNGSMETHODE_MISSING",
            Self::NoReadings => "NO_READINGS",
            Self::NotQuarterHourResolution => "NOT_QUARTER_HOUR_RESOLUTION",
            Self::CoverageBelowThreshold => "COVERAGE_BELOW_THRESHOLD",
        }
    }

    /// Every finding, in declaration order.
    pub const ALL: [Self; 10] = [
        Self::NotRemotelyReadable,
        Self::GatewayNotOperational,
        Self::ModerneMesseinrichtungWithoutGateway,
        Self::BalancingMethodHasNoQuarterHourValues,
        Self::MeterTypeQualifiesForNeitherLimb,
        Self::ZaehlertypMissing,
        Self::BilanzierungsmethodeMissing,
        Self::NoReadings,
        Self::NotQuarterHourResolution,
        Self::CoverageBelowThreshold,
    ];

    /// A German rendering for an operator-facing report.
    #[must_use]
    pub const fn description_de(self) -> &'static str {
        match self {
            Self::NotRemotelyReadable => {
                "Zähler ist als nicht fernauslesbar gekennzeichnet — keine \
                 Zählerstandsgangmessung möglich"
            }
            Self::GatewayNotOperational => {
                "Smart-Meter-Gateway nicht in Betrieb — keine Zählerstandsgangmessung"
            }
            Self::ModerneMesseinrichtungWithoutGateway => {
                "moderne Messeinrichtung ohne Smart-Meter-Gateway \
                 (§ 2 Satz 1 Nr. 15 MsbG) — iMSys-Rollout oder RLM erforderlich"
            }
            Self::BalancingMethodHasNoQuarterHourValues => {
                "Bilanzierungsmethode liefert keine Viertelstundenwerte"
            }
            Self::MeterTypeQualifiesForNeitherLimb => "Zählertyp ist weder iMSys noch RLM",
            Self::ZaehlertypMissing => "kein Zählertyp im Stammdatensatz hinterlegt",
            Self::BilanzierungsmethodeMissing => "keine Bilanzierungsmethode an der Marktlokation",
            Self::NoReadings => "keine Messwerte im Betrachtungszeitraum",
            Self::NotQuarterHourResolution => "Messwerte liegen nicht viertelstündlich vor",
            Self::CoverageBelowThreshold => "Abdeckung unter der Schwelle",
        }
    }
}

// ── Capability (master data) ──────────────────────────────────────────────────

/// The metering equipment installed at a delivery point: the BO4E
/// `Zaehlertyp` value set narrowed to the distinctions § 42c turns on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Zaehlertyp {
    /// Intelligentes Messsystem — a modern meter behind a Smart-Meter-Gateway
    /// (§ 2 Satz 1 Nr. 7 MsbG).
    IntelligentesMesssystem,
    /// Moderne Messeinrichtung — no gateway, no interval series (§ 2 Satz 1
    /// Nr. 15 MsbG); not sufficient for § 42c on its own.
    ModerneMesseinrichtung,
    /// Any conventional meter: Drehstrom-, Wechselstrom-, Ferrariszähler.
    Conventional,
}

impl Zaehlertyp {
    /// Every variant, in declaration order.
    pub const ALL: [Self; 3] = [
        Self::IntelligentesMesssystem,
        Self::ModerneMesseinrichtung,
        Self::Conventional,
    ];

    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::IntelligentesMesssystem => "INTELLIGENTES_MESSSYSTEM",
            Self::ModerneMesseinrichtung => "MODERNE_MESSEINRICHTUNG",
            Self::Conventional => "CONVENTIONAL",
        }
    }
}

/// How the Marktlokation is balanced (`Marktlokation.bilanzierungsmethode`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Bilanzierungsmethode {
    /// Registrierende Leistungsmessung — the second § 42c limb, sufficient on
    /// its own and needing no gateway.
    Rlm,
    /// Standardlastprofil — no quarter-hour values.
    Slp,
    /// Zählerstandsgangmessung at an intelligentes Messsystem.
    Ims,
    /// Temperaturabhängiges Lastprofil (Gas); never qualifies.
    Tlp,
    /// Pauschale Abrechnung — no measurement at all.
    Pauschal,
}

impl Bilanzierungsmethode {
    /// Every variant, in declaration order.
    pub const ALL: [Self; 5] = [Self::Rlm, Self::Slp, Self::Ims, Self::Tlp, Self::Pauschal];

    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Rlm => "RLM",
            Self::Slp => "SLP",
            Self::Ims => "IMS",
            Self::Tlp => "TLP",
            Self::Pauschal => "PAUSCHAL",
        }
    }

    /// `true` when the method produces no quarter-hour series and so rules the
    /// point out on its own.
    #[must_use]
    pub const fn precludes_quarter_hour_values(self) -> bool {
        matches!(self, Self::Slp | Self::Tlp | Self::Pauschal)
    }
}

/// Device master data for one delivery point. A missing field yields a
/// finding, never a guess.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MeteringCapabilityInput {
    /// The installed metering equipment.
    pub zaehlertyp: Option<Zaehlertyp>,
    /// BO4E `Zaehler.istFernauslesbar`; `Some(false)` blocks the
    /// Zählerstandsgang limb.
    pub ist_fernauslesbar: Option<bool>,
    /// How the Marktlokation is balanced.
    pub bilanzierungsmethode: Option<Bilanzierungsmethode>,
    /// Whether the Smart-Meter-Gateway is operational; `Some(false)` blocks
    /// the Zählerstandsgang limb.
    pub smgw_operational: Option<bool>,
}

/// Outcome of the master-data capability assessment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "SCREAMING_SNAKE_CASE"))]
pub enum Capability {
    /// Master data supports a § 42c-qualifying measurement.
    Qualified(EligibilityBasis),
    /// Master data positively rules the point out.
    Disqualified,
    /// Master data is insufficient to decide.
    Unknown,
}

impl Capability {
    /// The qualifying basis, when one was established.
    #[must_use]
    pub const fn basis(self) -> Option<EligibilityBasis> {
        match self {
            Self::Qualified(b) => Some(b),
            _ => None,
        }
    }
}

/// Master data that cannot all be true at once; refused rather than assessed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum Contradiction {
    /// A moderne Messeinrichtung balanced as RLM or as iMSys: it has neither a
    /// load-profile register nor a gateway (§ 2 Satz 1 Nr. 15 MsbG).
    #[error("a moderne Messeinrichtung cannot be balanced as {0}")]
    ModerneMesseinrichtungBalancedAs(Bilanzierungsmethode),
    /// A conventional meter balanced as iMSys (Zählerstandsgang).
    #[error("a conventional meter cannot be balanced as IMS")]
    ConventionalBalancedAsIms,
    /// An operational gateway on a meter type that is not an iMSys: a meter
    /// behind a working gateway is one (§ 2 Satz 1 Nr. 7 MsbG).
    #[error("an operational gateway on a {0} meter")]
    GatewayOnNonImsys(Zaehlertyp),
}

/// Assess § 42c capability from device master data.
///
/// `RLM` qualifies on its own limb. `IMS`, an iMSys meter or an operational
/// gateway qualify on the Zählerstandsgang limb unless the point is reported
/// not remotely readable or its gateway down. A moderne Messeinrichtung, or
/// `SLP`/`TLP`/`Pauschal`, is disqualified; missing fields give
/// [`Capability::Unknown`].
///
/// # Errors
///
/// A [`Contradiction`] for master data that cannot all be true.
pub fn assess_capability(
    input: &MeteringCapabilityInput,
) -> Result<(Capability, Vec<Finding>), Contradiction> {
    let methode = input.bilanzierungsmethode;
    let typ = input.zaehlertyp;
    match (typ, methode) {
        (
            Some(Zaehlertyp::ModerneMesseinrichtung),
            Some(m @ (Bilanzierungsmethode::Rlm | Bilanzierungsmethode::Ims)),
        ) => return Err(Contradiction::ModerneMesseinrichtungBalancedAs(m)),
        (Some(Zaehlertyp::Conventional), Some(Bilanzierungsmethode::Ims)) => {
            return Err(Contradiction::ConventionalBalancedAsIms);
        }
        _ => {}
    }
    if input.smgw_operational == Some(true)
        && let Some(t @ (Zaehlertyp::ModerneMesseinrichtung | Zaehlertyp::Conventional)) = typ
    {
        return Err(Contradiction::GatewayOnNonImsys(t));
    }

    // Limb 2 — viertelstündliche registrierende Leistungsmessung.
    if methode == Some(Bilanzierungsmethode::Rlm) {
        return Ok((
            Capability::Qualified(EligibilityBasis::RegistrierendeLeistungsmessung),
            Vec::new(),
        ));
    }

    // Limb 1 — Zählerstandsgangmessung via iMSys.
    let looks_imsys = methode == Some(Bilanzierungsmethode::Ims)
        || typ == Some(Zaehlertyp::IntelligentesMesssystem)
        || input.smgw_operational == Some(true);
    if looks_imsys {
        let mut findings = Vec::new();
        if input.ist_fernauslesbar == Some(false) {
            findings.push(Finding::NotRemotelyReadable);
        }
        if input.smgw_operational == Some(false) {
            findings.push(Finding::GatewayNotOperational);
        }
        if findings.is_empty() {
            return Ok((
                Capability::Qualified(EligibilityBasis::Zaehlerstandsgangmessung),
                findings,
            ));
        }
        return Ok((Capability::Disqualified, findings));
    }

    let mut findings = Vec::new();
    if typ == Some(Zaehlertyp::ModerneMesseinrichtung) {
        findings.push(Finding::ModerneMesseinrichtungWithoutGateway);
        return Ok((Capability::Disqualified, findings));
    }
    if methode.is_some_and(Bilanzierungsmethode::precludes_quarter_hour_values) {
        findings.push(Finding::BalancingMethodHasNoQuarterHourValues);
        return Ok((Capability::Disqualified, findings));
    }

    if typ.is_none() {
        findings.push(Finding::ZaehlertypMissing);
    }
    if methode.is_none() {
        findings.push(Finding::BilanzierungsmethodeMissing);
    }
    if findings.is_empty() {
        findings.push(Finding::MeterTypeQualifiesForNeitherLimb);
        return Ok((Capability::Disqualified, findings));
    }
    Ok((Capability::Unknown, findings))
}

// ── Delivery (observed data) ──────────────────────────────────────────────────

/// Whether the point is in fact delivering a quarter-hour series.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delivery {
    /// Quarter-hour values observed at or above the coverage threshold.
    Delivering,
    /// Values observed, but not at quarter-hour resolution or below threshold.
    Insufficient,
    /// No readings in the observation window.
    Absent,
}

impl Delivery {
    /// Every outcome, in declaration order.
    pub const ALL: [Self; 3] = [Self::Delivering, Self::Insufficient, Self::Absent];

    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Delivering => "DELIVERING",
            Self::Insufficient => "INSUFFICIENT",
            Self::Absent => "ABSENT",
        }
    }
}

/// Minimum share of the window's quarter-hours, in percent, for a point to
/// count as delivering. § 42c fixes none; this is an operational choice.
pub const DEFAULT_COVERAGE_THRESHOLD_PCT: u8 = 95;

/// Assess actual quarter-hour delivery from the observed series over `window`.
///
/// Coverage is the billable intervals of `series` inside `window` against
/// the quarter-hours the window holds (DST-aware), compared in integers.
/// No billable interval in the window is [`Absent`](Delivery::Absent); a
/// non-quarter-hour series is [`Insufficient`](Delivery::Insufficient).
#[must_use]
pub fn assess_delivery(
    series: &Series,
    window: Period,
    coverage_threshold_pct: u8,
) -> (Delivery, Vec<Finding>) {
    let present = series
        .iter()
        .filter(|iv| {
            iv.quality().is_billable() && window.start() <= iv.from() && iv.to() <= window.end()
        })
        .count();
    if present == 0 {
        return (Delivery::Absent, vec![Finding::NoReadings]);
    }
    if series.resolution() != Resolution::QUARTER_HOUR {
        return (
            Delivery::Insufficient,
            vec![Finding::NotQuarterHourResolution],
        );
    }
    let expected = window.count(Resolution::QUARTER_HOUR).unwrap_or(0);
    let covered = u64::try_from(present)
        .unwrap_or(u64::MAX)
        .saturating_mul(100);
    if covered < u64::from(expected) * u64::from(coverage_threshold_pct) {
        return (
            Delivery::Insufficient,
            vec![Finding::CoverageBelowThreshold],
        );
    }
    (Delivery::Delivering, Vec::new())
}

// ── Combined verdict ──────────────────────────────────────────────────────────

/// Overall § 42c readiness for one delivery point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SharingReadiness {
    /// Capable and delivering — can join a sharing community today.
    Ready,
    /// Capable, but no conforming quarter-hour series is arriving: needs a
    /// configuration order, not a meter rollout.
    CapableNotDelivering,
    /// Master data rules the point out — an iMSys rollout or RLM is required.
    NotCapable,
    /// Insufficient master data to decide.
    Unknown,
}

impl SharingReadiness {
    /// Every verdict, in declaration order.
    pub const ALL: [Self; 4] = [
        Self::Ready,
        Self::CapableNotDelivering,
        Self::NotCapable,
        Self::Unknown,
    ];

    /// Stable DB/wire label. Matches the `serde` tag and
    /// [`FromStr`](std::str::FromStr) input.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Ready => "READY",
            Self::CapableNotDelivering => "CAPABLE_NOT_DELIVERING",
            Self::NotCapable => "NOT_CAPABLE",
            Self::Unknown => "UNKNOWN",
        }
    }

    /// The operator action this verdict calls for.
    #[must_use]
    pub const fn required_action(self) -> &'static str {
        match self {
            Self::Ready => "keine",
            Self::CapableNotDelivering => "Zählerstandsgangmessung beauftragen",
            Self::NotCapable => "iMSys-Rollout oder RLM-Umbau beauftragen",
            Self::Unknown => "Stammdaten vervollständigen",
        }
    }
}

/// Combine a capability and a delivery assessment into one verdict.
///
/// Delivery alone never establishes eligibility: § 42c is about the
/// measurement installed, so capability decides first.
#[must_use]
pub const fn combine_readiness(capability: Capability, delivery: Delivery) -> SharingReadiness {
    match (capability, delivery) {
        (Capability::Qualified(_), Delivery::Delivering) => SharingReadiness::Ready,
        (Capability::Qualified(_), _) => SharingReadiness::CapableNotDelivering,
        (Capability::Disqualified, _) => SharingReadiness::NotCapable,
        (Capability::Unknown, _) => SharingReadiness::Unknown,
    }
}

crate::ids::codes::string_codes! {
    EligibilityBasis;
    Finding;
    Zaehlertyp;
    Bilanzierungsmethode;
    Delivery;
    SharingReadiness;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::series::interval::{MeterInterval, QualityFlag};
    use crate::time::calendar::DayBoundary;
    use rust_decimal::dec;
    use time::Duration;
    use time::macros::date;

    fn cap(
        zt: Option<Zaehlertyp>,
        fern: Option<bool>,
        bm: Option<Bilanzierungsmethode>,
        smgw: Option<bool>,
    ) -> Result<(Capability, Vec<Finding>), Contradiction> {
        assess_capability(&MeteringCapabilityInput {
            zaehlertyp: zt,
            ist_fernauslesbar: fern,
            bilanzierungsmethode: bm,
            smgw_operational: smgw,
        })
    }

    #[test]
    fn rlm_qualifies_without_a_gateway() {
        let (c, _) = cap(
            Some(Zaehlertyp::Conventional),
            None,
            Some(Bilanzierungsmethode::Rlm),
            None,
        )
        .unwrap();
        assert_eq!(
            c,
            Capability::Qualified(EligibilityBasis::RegistrierendeLeistungsmessung)
        );
    }

    #[test]
    fn imsys_qualifies_on_the_zsg_limb() {
        let (c, _) = cap(
            Some(Zaehlertyp::IntelligentesMesssystem),
            Some(true),
            None,
            None,
        )
        .unwrap();
        assert_eq!(
            c,
            Capability::Qualified(EligibilityBasis::Zaehlerstandsgangmessung)
        );
    }

    #[test]
    fn an_imsys_whose_gateway_is_down_is_disqualified() {
        let (c, findings) = cap(
            Some(Zaehlertyp::IntelligentesMesssystem),
            Some(true),
            Some(Bilanzierungsmethode::Ims),
            Some(false),
        )
        .unwrap();
        assert_eq!(c, Capability::Disqualified);
        assert_eq!(findings, vec![Finding::GatewayNotOperational]);
    }

    #[test]
    fn imsys_that_cannot_be_read_remotely_is_disqualified() {
        let (c, findings) = cap(
            Some(Zaehlertyp::IntelligentesMesssystem),
            Some(false),
            Some(Bilanzierungsmethode::Ims),
            None,
        )
        .unwrap();
        assert_eq!(c, Capability::Disqualified);
        assert_eq!(findings, vec![Finding::NotRemotelyReadable]);
    }

    #[test]
    fn contradictory_master_data_is_refused() {
        assert_eq!(
            cap(
                Some(Zaehlertyp::ModerneMesseinrichtung),
                Some(false),
                Some(Bilanzierungsmethode::Rlm),
                None
            ),
            Err(Contradiction::ModerneMesseinrichtungBalancedAs(
                Bilanzierungsmethode::Rlm
            ))
        );
        assert_eq!(
            cap(
                Some(Zaehlertyp::Conventional),
                None,
                Some(Bilanzierungsmethode::Ims),
                None
            ),
            Err(Contradiction::ConventionalBalancedAsIms)
        );
        assert_eq!(
            cap(
                Some(Zaehlertyp::ModerneMesseinrichtung),
                None,
                None,
                Some(true)
            ),
            Err(Contradiction::GatewayOnNonImsys(
                Zaehlertyp::ModerneMesseinrichtung
            ))
        );
    }

    #[test]
    fn moderne_messeinrichtung_and_slp_are_not_sufficient() {
        let (c, f) = cap(Some(Zaehlertyp::ModerneMesseinrichtung), None, None, None).unwrap();
        assert_eq!(
            (c, f),
            (
                Capability::Disqualified,
                vec![Finding::ModerneMesseinrichtungWithoutGateway]
            )
        );
        let (c, _) = cap(
            Some(Zaehlertyp::Conventional),
            None,
            Some(Bilanzierungsmethode::Slp),
            None,
        )
        .unwrap();
        assert_eq!(c, Capability::Disqualified);
    }

    #[test]
    fn missing_master_data_is_unknown_not_a_guess() {
        let (c, findings) = cap(None, None, None, None).unwrap();
        assert_eq!(c, Capability::Unknown);
        assert_eq!(
            findings,
            vec![
                Finding::ZaehlertypMissing,
                Finding::BilanzierungsmethodeMissing
            ]
        );
        for f in Finding::ALL {
            assert!(!f.description_de().is_empty(), "{f:?}");
        }
    }

    fn day_series(resolution: Resolution, present: usize) -> (Series, Period) {
        let day = DayBoundary::Strom.day(date!(2026 - 06 - 01)).unwrap();
        let step = resolution.fixed_seconds().map(i64::from).unwrap_or(900);
        let ivs = (0..present)
            .map(|i| {
                let from = day.start() + Duration::seconds(step * i as i64);
                MeterInterval::new(
                    from,
                    from + Duration::seconds(step),
                    dec!(1),
                    QualityFlag::Measured,
                )
                .unwrap()
            })
            .collect();
        (
            Series::new(resolution, DayBoundary::Strom, ivs).unwrap(),
            day,
        )
    }

    #[test]
    fn delivery_coverage_is_computed_from_the_series() {
        let (full, day) = day_series(Resolution::QUARTER_HOUR, 96);
        assert_eq!(
            assess_delivery(&full, day, DEFAULT_COVERAGE_THRESHOLD_PCT),
            (Delivery::Delivering, vec![])
        );
        // 91 of 96 is 94.8 %: below 95.
        let (thin, day) = day_series(Resolution::QUARTER_HOUR, 91);
        assert_eq!(
            assess_delivery(&thin, day, DEFAULT_COVERAGE_THRESHOLD_PCT),
            (
                Delivery::Insufficient,
                vec![Finding::CoverageBelowThreshold]
            )
        );
        let (hourly, day) = day_series(Resolution::Hour, 24);
        assert_eq!(
            assess_delivery(&hourly, day, DEFAULT_COVERAGE_THRESHOLD_PCT),
            (
                Delivery::Insufficient,
                vec![Finding::NotQuarterHourResolution]
            )
        );
    }

    /// One old reading outside the window is no delivery at all.
    #[test]
    fn readings_outside_the_window_do_not_count() {
        let (old, _) = day_series(Resolution::QUARTER_HOUR, 1);
        let later = DayBoundary::Strom.day(date!(2026 - 07 - 01)).unwrap();
        assert_eq!(
            assess_delivery(&old, later, DEFAULT_COVERAGE_THRESHOLD_PCT),
            (Delivery::Absent, vec![Finding::NoReadings])
        );
    }

    #[test]
    fn capable_but_silent_is_its_own_verdict() {
        let verdict = combine_readiness(
            Capability::Qualified(EligibilityBasis::Zaehlerstandsgangmessung),
            Delivery::Absent,
        );
        assert_eq!(verdict, SharingReadiness::CapableNotDelivering);
        assert_eq!(
            combine_readiness(Capability::Unknown, Delivery::Delivering),
            SharingReadiness::Unknown
        );
    }
}