oximedia-analytics 0.1.3

Media engagement analytics — viewer behavior, A/B testing, retention curves, and engagement scoring for OxiMedia
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
//! Geographic and device breakdown analytics for viewer session metrics.
//!
//! Aggregates session-level data across two orthogonal dimensions — geographic
//! region and device type — and computes per-slice metrics such as view count,
//! unique viewer count, total watch time, and average watch time.
//!
//! ## Usage
//!
//! ```rust
//! use oximedia_analytics::geo_device::{SessionRecord, BreakdownAnalyzer, DeviceType, Region};
//!
//! let mut analyzer = BreakdownAnalyzer::new();
//! analyzer.ingest(SessionRecord {
//!     viewer_id: "v1".into(),
//!     region: Region::NorthAmerica,
//!     device: DeviceType::Desktop,
//!     watch_seconds: 120.0,
//! });
//! analyzer.ingest(SessionRecord {
//!     viewer_id: "v2".into(),
//!     region: Region::NorthAmerica,
//!     device: DeviceType::Mobile,
//!     watch_seconds: 60.0,
//! });
//! let by_region = analyzer.breakdown_by_region();
//! assert_eq!(by_region.len(), 1); // one region: NorthAmerica
//! ```

use crate::error::AnalyticsError;
use std::collections::HashMap;

// ─── DeviceType ──────────────────────────────────────────────────────────────

/// Broad device category inferred from the user-agent or client metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeviceType {
    /// Traditional laptop or desktop computer.
    Desktop,
    /// Smartphone.
    Mobile,
    /// Tablet (iPad, Android tablet, etc.).
    Tablet,
    /// Smart TV, streaming stick, or set-top box.
    SmartTv,
    /// Game console.
    Console,
    /// Device type could not be determined.
    Unknown,
}

impl DeviceType {
    /// Returns a human-readable label for the device type.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Self::Desktop => "desktop",
            Self::Mobile => "mobile",
            Self::Tablet => "tablet",
            Self::SmartTv => "smart_tv",
            Self::Console => "console",
            Self::Unknown => "unknown",
        }
    }
}

// ─── Region ──────────────────────────────────────────────────────────────────

/// Geographic region grouping for broadcast and streaming analytics.
///
/// Regions correspond to broad audience groupings commonly used in media
/// rights and advertising markets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Region {
    /// United States, Canada, Mexico.
    NorthAmerica,
    /// Brazil, Argentina, Colombia, and other LATAM markets.
    LatinAmerica,
    /// UK, France, Germany, and other European markets.
    Europe,
    /// Russia and other CIS/Eastern European markets.
    EasternEurope,
    /// Middle East and Africa.
    Mea,
    /// India, Southeast Asia, Australia, New Zealand.
    AsiaPacific,
    /// Japan, South Korea, China.
    EastAsia,
    /// Region could not be mapped.
    Unknown,
}

impl Region {
    /// Returns a human-readable label for the region.
    #[must_use]
    pub fn label(&self) -> &'static str {
        match self {
            Self::NorthAmerica => "north_america",
            Self::LatinAmerica => "latin_america",
            Self::Europe => "europe",
            Self::EasternEurope => "eastern_europe",
            Self::Mea => "mea",
            Self::AsiaPacific => "asia_pacific",
            Self::EastAsia => "east_asia",
            Self::Unknown => "unknown",
        }
    }
}

// ─── SessionRecord ───────────────────────────────────────────────────────────

/// A single viewer session enriched with geographic and device metadata.
#[derive(Debug, Clone)]
pub struct SessionRecord {
    /// Opaque viewer / user identifier (used for unique viewer counting).
    pub viewer_id: String,
    /// Geographic region of the viewer.
    pub region: Region,
    /// Device category used for playback.
    pub device: DeviceType,
    /// Total watch time for this session in seconds.
    pub watch_seconds: f64,
}

// ─── SliceMetrics ────────────────────────────────────────────────────────────

/// Aggregated metrics for a single dimension slice (region or device).
#[derive(Debug, Clone, PartialEq)]
pub struct SliceMetrics {
    /// Total number of sessions in this slice.
    pub sessions: u64,
    /// Number of distinct `viewer_id` values (approximate unique viewers).
    pub unique_viewers: u64,
    /// Sum of all `watch_seconds` values in this slice.
    pub total_watch_seconds: f64,
    /// Average watch time per session (`total_watch_seconds / sessions`).
    pub avg_watch_seconds: f64,
}

impl SliceMetrics {
    fn new(sessions: u64, unique_viewers: u64, total_watch_seconds: f64) -> Self {
        let avg = if sessions == 0 {
            0.0
        } else {
            total_watch_seconds / sessions as f64
        };
        Self {
            sessions,
            unique_viewers,
            total_watch_seconds,
            avg_watch_seconds: avg,
        }
    }
}

// ─── BreakdownAnalyzer ───────────────────────────────────────────────────────

/// Accumulates session records and computes breakdowns by region and device.
#[derive(Debug, Default)]
pub struct BreakdownAnalyzer {
    records: Vec<SessionRecord>,
}

impl BreakdownAnalyzer {
    /// Creates an empty analyzer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            records: Vec::new(),
        }
    }

    /// Ingests a single session record.
    pub fn ingest(&mut self, record: SessionRecord) {
        self.records.push(record);
    }

    /// Ingests a batch of session records.
    pub fn ingest_batch(&mut self, records: impl IntoIterator<Item = SessionRecord>) {
        self.records.extend(records);
    }

    /// Total number of session records ingested so far.
    #[must_use]
    pub fn session_count(&self) -> usize {
        self.records.len()
    }

    /// Computes per-region aggregates.
    ///
    /// Returns a map from [`Region`] to [`SliceMetrics`].
    /// Regions with no sessions are omitted.
    #[must_use]
    pub fn breakdown_by_region(&self) -> HashMap<Region, SliceMetrics> {
        self.aggregate(|r| r.region)
    }

    /// Computes per-device aggregates.
    ///
    /// Returns a map from [`DeviceType`] to [`SliceMetrics`].
    /// Device types with no sessions are omitted.
    #[must_use]
    pub fn breakdown_by_device(&self) -> HashMap<DeviceType, SliceMetrics> {
        self.aggregate(|r| r.device)
    }

    /// Computes a cross-tab: breakdown by `(region, device)` pair.
    ///
    /// Returns a map from `(Region, DeviceType)` to [`SliceMetrics`].
    #[must_use]
    pub fn breakdown_by_region_and_device(&self) -> HashMap<(Region, DeviceType), SliceMetrics> {
        self.aggregate(|r| (r.region, r.device))
    }

    /// Finds the region with the highest total watch time.
    ///
    /// # Errors
    ///
    /// Returns [`AnalyticsError::InsufficientData`] when no records have been
    /// ingested.
    pub fn top_region_by_watch_time(&self) -> Result<Region, AnalyticsError> {
        if self.records.is_empty() {
            return Err(AnalyticsError::InsufficientData(
                "no sessions ingested".into(),
            ));
        }
        let breakdown = self.breakdown_by_region();
        breakdown
            .into_iter()
            .max_by(|(_, a), (_, b)| {
                a.total_watch_seconds
                    .partial_cmp(&b.total_watch_seconds)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(region, _)| region)
            .ok_or_else(|| AnalyticsError::InsufficientData("breakdown empty".into()))
    }

    /// Finds the device type with the highest session count.
    ///
    /// # Errors
    ///
    /// Returns [`AnalyticsError::InsufficientData`] when no records have been
    /// ingested.
    pub fn top_device_by_sessions(&self) -> Result<DeviceType, AnalyticsError> {
        if self.records.is_empty() {
            return Err(AnalyticsError::InsufficientData(
                "no sessions ingested".into(),
            ));
        }
        let breakdown = self.breakdown_by_device();
        breakdown
            .into_iter()
            .max_by_key(|(_, m)| m.sessions)
            .map(|(device, _)| device)
            .ok_or_else(|| AnalyticsError::InsufficientData("breakdown empty".into()))
    }

    // ── private helpers ──────────────────────────────────────────────────────

    /// Generic aggregation keyed by the output of `key_fn`.
    fn aggregate<K, F>(&self, key_fn: F) -> HashMap<K, SliceMetrics>
    where
        K: Eq + std::hash::Hash,
        F: Fn(&SessionRecord) -> K,
    {
        // Phase 1: accumulate totals.
        struct Acc {
            sessions: u64,
            viewers: std::collections::HashSet<String>,
            total_watch: f64,
        }

        let mut map: HashMap<K, Acc> = HashMap::new();

        for rec in &self.records {
            let key = key_fn(rec);
            let acc = map.entry(key).or_insert_with(|| Acc {
                sessions: 0,
                viewers: std::collections::HashSet::new(),
                total_watch: 0.0,
            });
            acc.sessions += 1;
            acc.viewers.insert(rec.viewer_id.clone());
            acc.total_watch += rec.watch_seconds;
        }

        // Phase 2: finalise into SliceMetrics.
        map.into_iter()
            .map(|(k, acc)| {
                (
                    k,
                    SliceMetrics::new(acc.sessions, acc.viewers.len() as u64, acc.total_watch),
                )
            })
            .collect()
    }
}

// ─── Period Comparison ───────────────────────────────────────────────────────

/// A tagged session record with an epoch timestamp for temporal comparisons.
#[derive(Debug, Clone)]
pub struct TimestampedRecord {
    /// The underlying session record.
    pub record: SessionRecord,
    /// Unix epoch timestamp in seconds when this session occurred.
    pub timestamp_s: i64,
}

impl TimestampedRecord {
    /// Wrap an existing record with a timestamp.
    pub fn new(record: SessionRecord, timestamp_s: i64) -> Self {
        Self {
            record,
            timestamp_s,
        }
    }
}

/// Comparison of a metric across two time periods (baseline vs comparison).
#[derive(Debug, Clone, PartialEq)]
pub struct PeriodDelta {
    /// Metric value in the baseline period.
    pub baseline: f64,
    /// Metric value in the comparison period.
    pub comparison: f64,
    /// Absolute change: `comparison − baseline`.
    pub absolute_change: f64,
    /// Relative (percentage) change: `(comparison − baseline) / baseline * 100`.
    ///
    /// `NaN` when `baseline` is zero.
    pub relative_change_pct: f64,
}

impl PeriodDelta {
    fn new(baseline: f64, comparison: f64) -> Self {
        let absolute_change = comparison - baseline;
        let relative_change_pct = if baseline.abs() < f64::EPSILON {
            f64::NAN
        } else {
            absolute_change / baseline * 100.0
        };
        Self {
            baseline,
            comparison,
            absolute_change,
            relative_change_pct,
        }
    }

    /// Returns `true` when the comparison period shows growth over baseline.
    pub fn is_growing(&self) -> bool {
        self.absolute_change > 0.0
    }
}

/// Period-over-period breakdown comparison for a single region or device slice.
#[derive(Debug, Clone)]
pub struct SliceComparison {
    /// Change in session count.
    pub sessions: PeriodDelta,
    /// Change in unique viewers.
    pub unique_viewers: PeriodDelta,
    /// Change in total watch seconds.
    pub total_watch_seconds: PeriodDelta,
    /// Change in average watch seconds per session.
    pub avg_watch_seconds: PeriodDelta,
}

/// A full geo/device breakdown report with optional period comparison.
#[derive(Debug, Clone)]
pub struct GeoDeviceReport {
    /// Per-region session metrics.
    pub by_region: HashMap<Region, SliceMetrics>,
    /// Per-device session metrics.
    pub by_device: HashMap<DeviceType, SliceMetrics>,
    /// Cross-tab (region × device) metrics.
    pub cross_tab: HashMap<(Region, DeviceType), SliceMetrics>,
    /// Total number of sessions in the report.
    pub total_sessions: u64,
    /// Total unique viewer count across all sessions.
    pub total_unique_viewers: u64,
    /// Total watch time in seconds.
    pub total_watch_seconds: f64,
    /// Average watch time per session across all sessions.
    pub overall_avg_watch_seconds: f64,
}

impl GeoDeviceReport {
    /// Fraction of total sessions that belong to `region` (0.0–1.0).
    ///
    /// Returns `0.0` when no sessions are recorded.
    pub fn region_share(&self, region: Region) -> f64 {
        if self.total_sessions == 0 {
            return 0.0;
        }
        self.by_region
            .get(&region)
            .map(|m| m.sessions as f64 / self.total_sessions as f64)
            .unwrap_or(0.0)
    }

    /// Fraction of total sessions that belong to `device` (0.0–1.0).
    ///
    /// Returns `0.0` when no sessions are recorded.
    pub fn device_share(&self, device: DeviceType) -> f64 {
        if self.total_sessions == 0 {
            return 0.0;
        }
        self.by_device
            .get(&device)
            .map(|m| m.sessions as f64 / self.total_sessions as f64)
            .unwrap_or(0.0)
    }

    /// Returns the region with the highest session count, or `None` when empty.
    pub fn dominant_region_by_sessions(&self) -> Option<Region> {
        self.by_region
            .iter()
            .max_by_key(|(_, m)| m.sessions)
            .map(|(&r, _)| r)
    }

    /// Returns the device type with the highest unique viewer count, or `None`
    /// when empty.
    pub fn dominant_device_by_viewers(&self) -> Option<DeviceType> {
        self.by_device
            .iter()
            .max_by_key(|(_, m)| m.unique_viewers)
            .map(|(&d, _)| d)
    }
}

impl BreakdownAnalyzer {
    /// Generate a full [`GeoDeviceReport`] from the ingested records.
    pub fn build_report(&self) -> GeoDeviceReport {
        let by_region = self.breakdown_by_region();
        let by_device = self.breakdown_by_device();
        let cross_tab = self.breakdown_by_region_and_device();

        let total_sessions = self.records.len() as u64;
        let total_watch_seconds: f64 = self.records.iter().map(|r| r.watch_seconds).sum();
        let total_unique_viewers = {
            let unique: std::collections::HashSet<&str> =
                self.records.iter().map(|r| r.viewer_id.as_str()).collect();
            unique.len() as u64
        };
        let overall_avg_watch_seconds = if total_sessions == 0 {
            0.0
        } else {
            total_watch_seconds / total_sessions as f64
        };

        GeoDeviceReport {
            by_region,
            by_device,
            cross_tab,
            total_sessions,
            total_unique_viewers,
            total_watch_seconds,
            overall_avg_watch_seconds,
        }
    }

    /// Compare metrics for a [`Region`] between two sub-sets of records split
    /// at `split_timestamp_s`.  Records at or before the split are the
    /// *baseline*; records after are the *comparison* period.
    ///
    /// Returns `None` when no records are tagged with timestamps (i.e., the
    /// [`TimestampedRecord`] API has not been used) or when the region is
    /// absent from either period.
    ///
    /// Use [`BreakdownAnalyzer::ingest_timestamped`] to build an analyzer with
    /// timestamp metadata.
    pub fn compare_region_periods(
        &self,
        timestamped: &[TimestampedRecord],
        region: Region,
        split_timestamp_s: i64,
    ) -> Option<SliceComparison> {
        let baseline: Vec<SessionRecord> = timestamped
            .iter()
            .filter(|t| t.timestamp_s <= split_timestamp_s && t.record.region == region)
            .map(|t| t.record.clone())
            .collect();
        let comparison: Vec<SessionRecord> = timestamped
            .iter()
            .filter(|t| t.timestamp_s > split_timestamp_s && t.record.region == region)
            .map(|t| t.record.clone())
            .collect();

        if baseline.is_empty() && comparison.is_empty() {
            return None;
        }

        let base_metrics = Self::compute_slice_metrics(&baseline);
        let comp_metrics = Self::compute_slice_metrics(&comparison);

        Some(SliceComparison {
            sessions: PeriodDelta::new(base_metrics.sessions as f64, comp_metrics.sessions as f64),
            unique_viewers: PeriodDelta::new(
                base_metrics.unique_viewers as f64,
                comp_metrics.unique_viewers as f64,
            ),
            total_watch_seconds: PeriodDelta::new(
                base_metrics.total_watch_seconds,
                comp_metrics.total_watch_seconds,
            ),
            avg_watch_seconds: PeriodDelta::new(
                base_metrics.avg_watch_seconds,
                comp_metrics.avg_watch_seconds,
            ),
        })
    }

    /// Compare metrics for a [`DeviceType`] between two time periods split at
    /// `split_timestamp_s`.
    pub fn compare_device_periods(
        &self,
        timestamped: &[TimestampedRecord],
        device: DeviceType,
        split_timestamp_s: i64,
    ) -> Option<SliceComparison> {
        let baseline: Vec<SessionRecord> = timestamped
            .iter()
            .filter(|t| t.timestamp_s <= split_timestamp_s && t.record.device == device)
            .map(|t| t.record.clone())
            .collect();
        let comparison: Vec<SessionRecord> = timestamped
            .iter()
            .filter(|t| t.timestamp_s > split_timestamp_s && t.record.device == device)
            .map(|t| t.record.clone())
            .collect();

        if baseline.is_empty() && comparison.is_empty() {
            return None;
        }

        let base_metrics = Self::compute_slice_metrics(&baseline);
        let comp_metrics = Self::compute_slice_metrics(&comparison);

        Some(SliceComparison {
            sessions: PeriodDelta::new(base_metrics.sessions as f64, comp_metrics.sessions as f64),
            unique_viewers: PeriodDelta::new(
                base_metrics.unique_viewers as f64,
                comp_metrics.unique_viewers as f64,
            ),
            total_watch_seconds: PeriodDelta::new(
                base_metrics.total_watch_seconds,
                comp_metrics.total_watch_seconds,
            ),
            avg_watch_seconds: PeriodDelta::new(
                base_metrics.avg_watch_seconds,
                comp_metrics.avg_watch_seconds,
            ),
        })
    }

    /// Ingest a collection of [`TimestampedRecord`]s (discards the timestamp
    /// metadata for the core analyzer, but returns the slice for use with
    /// comparison functions).
    pub fn ingest_timestamped(&mut self, records: &[TimestampedRecord]) {
        for tr in records {
            self.records.push(tr.record.clone());
        }
    }

    // ── private ──────────────────────────────────────────────────────────────

    /// Compute `SliceMetrics` directly from an arbitrary slice of records.
    fn compute_slice_metrics(records: &[SessionRecord]) -> SliceMetrics {
        if records.is_empty() {
            return SliceMetrics::new(0, 0, 0.0);
        }
        let sessions = records.len() as u64;
        let total_watch: f64 = records.iter().map(|r| r.watch_seconds).sum();
        let unique: std::collections::HashSet<&str> =
            records.iter().map(|r| r.viewer_id.as_str()).collect();
        SliceMetrics::new(sessions, unique.len() as u64, total_watch)
    }
}

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

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

    fn rec(viewer_id: &str, region: Region, device: DeviceType, watch: f64) -> SessionRecord {
        SessionRecord {
            viewer_id: viewer_id.into(),
            region,
            device,
            watch_seconds: watch,
        }
    }

    #[test]
    fn empty_analyzer_has_zero_sessions() {
        let a = BreakdownAnalyzer::new();
        assert_eq!(a.session_count(), 0);
    }

    #[test]
    fn single_record_breakdown_by_region() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 300.0));
        let bd = a.breakdown_by_region();
        let m = bd.get(&Region::Europe).expect("Europe present");
        assert_eq!(m.sessions, 1);
        assert_eq!(m.unique_viewers, 1);
        assert!((m.total_watch_seconds - 300.0).abs() < 1e-9);
        assert!((m.avg_watch_seconds - 300.0).abs() < 1e-9);
    }

    #[test]
    fn multiple_regions_counted_separately() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v2", Region::AsiaPacific, DeviceType::Mobile, 200.0));
        a.ingest(rec("v3", Region::Europe, DeviceType::Tablet, 150.0));

        let bd = a.breakdown_by_region();
        let eu = bd.get(&Region::Europe).expect("EU");
        let ap = bd.get(&Region::AsiaPacific).expect("AP");

        assert_eq!(eu.sessions, 2);
        assert_eq!(ap.sessions, 1);
        assert!((eu.total_watch_seconds - 250.0).abs() < 1e-9);
    }

    #[test]
    fn unique_viewers_deduplicated() {
        let mut a = BreakdownAnalyzer::new();
        // Same viewer, three sessions in Europe.
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 60.0));
        a.ingest(rec("v1", Region::Europe, DeviceType::Mobile, 45.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Desktop, 90.0));

        let bd = a.breakdown_by_region();
        let eu = bd.get(&Region::Europe).expect("EU");
        assert_eq!(eu.sessions, 3);
        assert_eq!(eu.unique_viewers, 2); // v1 and v2
    }

    #[test]
    fn breakdown_by_device_type() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::NorthAmerica, DeviceType::Mobile, 100.0));
        a.ingest(rec("v2", Region::NorthAmerica, DeviceType::Mobile, 80.0));
        a.ingest(rec("v3", Region::Europe, DeviceType::Desktop, 200.0));

        let bd = a.breakdown_by_device();
        let mob = bd.get(&DeviceType::Mobile).expect("Mobile");
        let desk = bd.get(&DeviceType::Desktop).expect("Desktop");

        assert_eq!(mob.sessions, 2);
        assert_eq!(desk.sessions, 1);
    }

    #[test]
    fn breakdown_cross_tab() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Mobile, 60.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Desktop, 120.0));
        a.ingest(rec("v3", Region::NorthAmerica, DeviceType::Mobile, 90.0));

        let bd = a.breakdown_by_region_and_device();
        assert_eq!(
            bd.get(&(Region::Europe, DeviceType::Mobile))
                .map(|m| m.sessions),
            Some(1)
        );
        assert_eq!(
            bd.get(&(Region::Europe, DeviceType::Desktop))
                .map(|m| m.sessions),
            Some(1)
        );
        assert_eq!(
            bd.get(&(Region::NorthAmerica, DeviceType::Mobile))
                .map(|m| m.sessions),
            Some(1)
        );
    }

    #[test]
    fn top_region_by_watch_time() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v2", Region::AsiaPacific, DeviceType::Mobile, 500.0));
        a.ingest(rec("v3", Region::AsiaPacific, DeviceType::Tablet, 300.0));

        let top = a.top_region_by_watch_time().expect("top region");
        assert_eq!(top, Region::AsiaPacific);
    }

    #[test]
    fn top_device_by_sessions() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Mobile, 60.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Mobile, 60.0));
        a.ingest(rec("v3", Region::Europe, DeviceType::SmartTv, 200.0));

        let top = a.top_device_by_sessions().expect("top device");
        assert_eq!(top, DeviceType::Mobile);
    }

    #[test]
    fn empty_analyzer_top_region_errors() {
        let a = BreakdownAnalyzer::new();
        assert!(a.top_region_by_watch_time().is_err());
    }

    #[test]
    fn empty_analyzer_top_device_errors() {
        let a = BreakdownAnalyzer::new();
        assert!(a.top_device_by_sessions().is_err());
    }

    #[test]
    fn device_type_labels_are_stable() {
        assert_eq!(DeviceType::Desktop.label(), "desktop");
        assert_eq!(DeviceType::Mobile.label(), "mobile");
        assert_eq!(DeviceType::SmartTv.label(), "smart_tv");
        assert_eq!(DeviceType::Unknown.label(), "unknown");
    }

    #[test]
    fn region_labels_are_stable() {
        assert_eq!(Region::NorthAmerica.label(), "north_america");
        assert_eq!(Region::AsiaPacific.label(), "asia_pacific");
        assert_eq!(Region::Unknown.label(), "unknown");
    }

    #[test]
    fn ingest_batch() {
        let mut a = BreakdownAnalyzer::new();
        let records = vec![
            rec("v1", Region::Europe, DeviceType::Desktop, 100.0),
            rec("v2", Region::Europe, DeviceType::Desktop, 200.0),
        ];
        a.ingest_batch(records);
        assert_eq!(a.session_count(), 2);
    }

    // ── GeoDeviceReport ───────────────────────────────────────────────────────

    #[test]
    fn build_report_totals_correct() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Mobile, 200.0));
        a.ingest(rec("v3", Region::AsiaPacific, DeviceType::Mobile, 300.0));

        let report = a.build_report();
        assert_eq!(report.total_sessions, 3);
        assert_eq!(report.total_unique_viewers, 3);
        assert!((report.total_watch_seconds - 600.0).abs() < 1e-9);
        assert!((report.overall_avg_watch_seconds - 200.0).abs() < 1e-9);
    }

    #[test]
    fn build_report_empty_analyzer() {
        let a = BreakdownAnalyzer::new();
        let report = a.build_report();
        assert_eq!(report.total_sessions, 0);
        assert_eq!(report.total_unique_viewers, 0);
        assert_eq!(report.total_watch_seconds, 0.0);
        assert_eq!(report.overall_avg_watch_seconds, 0.0);
        assert!(report.by_region.is_empty());
    }

    #[test]
    fn region_share_sums_to_one() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v3", Region::AsiaPacific, DeviceType::Mobile, 100.0));

        let report = a.build_report();
        let eu_share = report.region_share(Region::Europe);
        let ap_share = report.region_share(Region::AsiaPacific);
        let unknown_share = report.region_share(Region::Unknown);
        let total = eu_share + ap_share + unknown_share;
        assert!((total - (eu_share + ap_share)).abs() < 1e-9); // unknown is 0
        assert!((eu_share + ap_share - 1.0).abs() < 1e-9);
    }

    #[test]
    fn device_share_correct() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Mobile, 100.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Mobile, 100.0));
        a.ingest(rec("v3", Region::Europe, DeviceType::Desktop, 100.0));

        let report = a.build_report();
        let mobile_share = report.device_share(DeviceType::Mobile);
        let desktop_share = report.device_share(DeviceType::Desktop);
        assert!((mobile_share - 2.0 / 3.0).abs() < 1e-9);
        assert!((desktop_share - 1.0 / 3.0).abs() < 1e-9);
    }

    #[test]
    fn dominant_region_by_sessions() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::Desktop, 100.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::Mobile, 100.0));
        a.ingest(rec("v3", Region::AsiaPacific, DeviceType::Mobile, 100.0));

        let report = a.build_report();
        assert_eq!(report.dominant_region_by_sessions(), Some(Region::Europe));
    }

    #[test]
    fn dominant_device_by_viewers() {
        let mut a = BreakdownAnalyzer::new();
        a.ingest(rec("v1", Region::Europe, DeviceType::SmartTv, 100.0));
        a.ingest(rec("v2", Region::Europe, DeviceType::SmartTv, 100.0));
        a.ingest(rec("v3", Region::Europe, DeviceType::Desktop, 100.0));

        let report = a.build_report();
        assert_eq!(
            report.dominant_device_by_viewers(),
            Some(DeviceType::SmartTv)
        );
    }

    // ── Period comparison ─────────────────────────────────────────────────────

    fn ts_rec(
        viewer_id: &str,
        region: Region,
        device: DeviceType,
        watch: f64,
        ts: i64,
    ) -> TimestampedRecord {
        TimestampedRecord::new(
            SessionRecord {
                viewer_id: viewer_id.into(),
                region,
                device,
                watch_seconds: watch,
            },
            ts,
        )
    }

    #[test]
    fn compare_region_periods_sessions_grow() {
        let a = BreakdownAnalyzer::new();
        let records = vec![
            ts_rec("v1", Region::Europe, DeviceType::Desktop, 60.0, 100),
            ts_rec("v2", Region::Europe, DeviceType::Mobile, 90.0, 200),
            ts_rec("v3", Region::Europe, DeviceType::Desktop, 120.0, 201),
            ts_rec("v4", Region::Europe, DeviceType::Mobile, 80.0, 300),
        ];
        // Split at 200: baseline has 2 sessions (v1,v2), comparison has 2 (v3,v4).
        let cmp = a
            .compare_region_periods(&records, Region::Europe, 200)
            .expect("comparison");
        assert_eq!(cmp.sessions.baseline as u64, 2);
        assert_eq!(cmp.sessions.comparison as u64, 2);
        assert!((cmp.sessions.absolute_change).abs() < 1e-9); // no change
    }

    #[test]
    fn compare_region_periods_watch_time_grows() {
        let a = BreakdownAnalyzer::new();
        let records = vec![
            ts_rec("v1", Region::NorthAmerica, DeviceType::Mobile, 60.0, 50),
            ts_rec("v2", Region::NorthAmerica, DeviceType::Mobile, 600.0, 150),
        ];
        // Baseline (≤100): 60s watch; comparison (>100): 600s watch.
        let cmp = a
            .compare_region_periods(&records, Region::NorthAmerica, 100)
            .expect("comparison");
        assert!((cmp.total_watch_seconds.baseline - 60.0).abs() < 1e-9);
        assert!((cmp.total_watch_seconds.comparison - 600.0).abs() < 1e-9);
        assert!(cmp.total_watch_seconds.is_growing());
    }

    #[test]
    fn compare_device_periods_absent_region_returns_none() {
        let a = BreakdownAnalyzer::new();
        let records = vec![ts_rec(
            "v1",
            Region::AsiaPacific,
            DeviceType::Mobile,
            60.0,
            100,
        )];
        // Tablet has no records in either period.
        let cmp = a.compare_device_periods(&records, DeviceType::Tablet, 50);
        assert!(cmp.is_none());
    }

    #[test]
    fn period_delta_relative_change_computed() {
        let delta = PeriodDelta::new(100.0, 150.0);
        assert!((delta.relative_change_pct - 50.0).abs() < 1e-9);
        assert!(delta.is_growing());
    }

    #[test]
    fn period_delta_zero_baseline_gives_nan_relative() {
        let delta = PeriodDelta::new(0.0, 10.0);
        assert!(delta.relative_change_pct.is_nan());
    }

    #[test]
    fn ingest_timestamped_populates_analyzer() {
        let mut a = BreakdownAnalyzer::new();
        let records = vec![
            ts_rec("v1", Region::Europe, DeviceType::Desktop, 100.0, 1),
            ts_rec("v2", Region::Europe, DeviceType::Mobile, 200.0, 2),
        ];
        a.ingest_timestamped(&records);
        assert_eq!(a.session_count(), 2);
    }
}