cmtraceopen-parser 0.1.1

Parse ConfigMgr/SCCM, Intune, CBS, DISM, Panther, MSI, and PSADT logs with format auto-detection, severity classification, and Windows error-code lookup. Pure Rust, no I/O, native + wasm32.
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
use std::collections::HashMap;

use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};

use super::guid_registry::GuidRegistryEntry;

/// Artifact count summary retained from an evidence bundle manifest.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EvidenceBundleArtifactCounts {
    pub collected: u64,
    pub missing: u64,
    pub failed: u64,
    pub skipped: u64,
}

/// Metadata retained when Intune analysis is sourced from a collected evidence bundle.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EvidenceBundleMetadata {
    pub manifest_path: String,
    pub notes_path: Option<String>,
    pub evidence_root: Option<String>,
    #[serde(default)]
    pub primary_entry_points: Vec<String>,
    #[serde(default)]
    pub available_primary_entry_points: Vec<String>,
    pub bundle_id: Option<String>,
    pub bundle_label: Option<String>,
    pub created_utc: Option<String>,
    pub case_reference: Option<String>,
    pub summary: Option<String>,
    pub collector_profile: Option<String>,
    pub collector_version: Option<String>,
    pub collected_utc: Option<String>,
    pub device_name: Option<String>,
    pub primary_user: Option<String>,
    pub platform: Option<String>,
    pub os_version: Option<String>,
    pub tenant: Option<String>,
    pub artifact_counts: Option<EvidenceBundleArtifactCounts>,
}

/// Severity level for generated diagnostic guidance.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneDiagnosticSeverity {
    Info,
    Warning,
    Error,
}

/// Diagnostic category used to group related remediation guidance.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneDiagnosticCategory {
    Download,
    Install,
    Timeout,
    Script,
    Policy,
    State,
    General,
}

/// Priority used to order remediation guidance.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneRemediationPriority {
    Monitor,
    Medium,
    High,
    Immediate,
}

/// Deterministic diagnostic guidance derived from Intune analysis results.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticInsight {
    pub id: String,
    pub severity: IntuneDiagnosticSeverity,
    pub category: IntuneDiagnosticCategory,
    pub remediation_priority: IntuneRemediationPriority,
    pub title: String,
    pub summary: String,
    pub likely_cause: Option<String>,
    pub evidence: Vec<String>,
    pub next_checks: Vec<String>,
    #[serde(default)]
    pub suggested_fixes: Vec<String>,
    #[serde(default)]
    pub focus_areas: Vec<String>,
    #[serde(default)]
    pub affected_source_files: Vec<String>,
    #[serde(default)]
    pub related_error_codes: Vec<String>,
}

/// Type of Intune event detected from log analysis.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum IntuneEventType {
    Win32App,
    WinGetApp,
    PowerShellScript,
    Remediation,
    Esp,
    SyncSession,
    PolicyEvaluation,
    ContentDownload,
    Other,
}

/// Status of an Intune operation.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum IntuneStatus {
    Success,
    Failed,
    InProgress,
    Pending,
    Timeout,
    Unknown,
}

/// A single Intune event extracted from log analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneEvent {
    /// Unique identifier for this event
    pub id: u64,
    /// Event type category
    pub event_type: IntuneEventType,
    /// Display name (resolved from GUID or extracted from context)
    pub name: String,
    /// GUID identifier if available
    pub guid: Option<String>,
    /// Status of the operation
    pub status: IntuneStatus,
    /// Timestamp of event start (ISO 8601 string)
    pub start_time: Option<String>,
    /// Timestamp of event end (ISO 8601 string)
    pub end_time: Option<String>,
    /// Duration in seconds
    pub duration_secs: Option<f64>,
    /// Error code if failed
    pub error_code: Option<String>,
    /// Additional detail message
    pub detail: String,
    /// Source log file path
    pub source_file: String,
    /// Line number in source file
    pub line_number: u32,
    /// Start time as milliseconds since Unix epoch (pre-computed for fast frontend sorting)
    pub start_time_epoch: Option<i64>,
    /// End time as milliseconds since Unix epoch (pre-computed for fast frontend sorting)
    pub end_time_epoch: Option<i64>,
    /// Decoded PowerShell script body (for script detection events)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script_body: Option<String>,
    /// GUID of the parent Win32App this script detection belongs to
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_app_guid: Option<String>,
}

impl IntuneEvent {
    /// Is this event in a failed state?
    pub fn status_is_failed(&self) -> bool {
        matches!(self.status, IntuneStatus::Failed)
    }

    /// Start time as epoch milliseconds, if known.
    pub fn start_time_epoch_ms(&self) -> Option<i64> {
        self.start_time_epoch
    }

    /// GUID anchoring this event (app/policy ID), if known.
    pub fn anchor_guid(&self) -> Option<&str> {
        self.guid.as_deref()
    }

    /// Human-readable display label (app/script name).
    pub fn display_name(&self) -> Option<&str> {
        if self.name.is_empty() {
            None
        } else {
            Some(self.name.as_str())
        }
    }

    /// Short label describing the kind of Intune operation this event represents.
    pub fn event_kind_label(&self) -> Option<&str> {
        Some(match self.event_type {
            IntuneEventType::Win32App => "App Install",
            IntuneEventType::WinGetApp => "WinGet Install",
            IntuneEventType::PowerShellScript => "Script",
            IntuneEventType::Remediation => "Remediation",
            IntuneEventType::Esp => "ESP",
            IntuneEventType::SyncSession => "Sync",
            IntuneEventType::PolicyEvaluation => "Policy",
            IntuneEventType::ContentDownload => "Download",
            IntuneEventType::Other => "Operation",
        })
    }
}

/// Metadata for an app policy extracted from "Get policies" JSON payloads.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AppPolicyMetadata {
    pub id: String,
    pub name: String,
    pub intent: Option<i32>,
    pub target_type: Option<i32>,
    pub install_command_line: Option<String>,
    pub uninstall_command_line: Option<String>,
    pub detection_rules: Vec<DetectionRuleMetadata>,
    pub return_codes: Vec<ReturnCodeEntry>,
    pub requirement_rules: Option<String>,
    pub install_behavior: Option<i32>,
}

/// A single detection rule from app policy metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DetectionRuleMetadata {
    /// 0=Registry, 1=MSI, 2=File, 3=Script
    pub detection_type: i32,
    /// Base64-decoded PowerShell script (type 3 only)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub script_body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enforce_signature_check: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_as_32_bit: Option<bool>,
}

/// A return code mapping from app policy metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReturnCodeEntry {
    pub return_code: i32,
    /// 0=Failed, 1=Success, 2=SoftReboot, 3=HardReboot, 4=Retry
    #[serde(rename = "type")]
    pub code_type: i32,
}

/// Download statistics for a content download event.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DownloadStat {
    /// Name or ID of the content
    pub content_id: String,
    /// Display name
    pub name: String,
    /// Total size in bytes
    pub size_bytes: u64,
    /// Download speed in bytes per second
    pub speed_bps: f64,
    /// Percentage downloaded via Delivery Optimization
    pub do_percentage: f64,
    /// Download duration in seconds
    pub duration_secs: f64,
    /// Whether download succeeded
    pub success: bool,
    /// Timestamp
    pub timestamp: Option<String>,
    /// Timestamp as milliseconds since Unix epoch (pre-computed for fast frontend sorting)
    pub timestamp_epoch: Option<i64>,
}

/// Inclusive timestamp bounds captured from analyzed content.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneTimestampBounds {
    pub first_timestamp: Option<String>,
    pub last_timestamp: Option<String>,
}

/// Coverage metrics for a single analyzed source file.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsFileCoverage {
    pub file_path: String,
    pub event_count: u32,
    pub download_count: u32,
    pub timestamp_bounds: Option<IntuneTimestampBounds>,
    #[serde(default)]
    pub is_rotated_segment: bool,
    pub rotation_group: Option<String>,
}

/// Summary of which files and time ranges contributed to diagnostics.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsCoverage {
    #[serde(default)]
    pub files: Vec<IntuneDiagnosticsFileCoverage>,
    pub timestamp_bounds: Option<IntuneTimestampBounds>,
    #[serde(default)]
    pub has_rotated_logs: bool,
    pub dominant_source: Option<IntuneDominantSource>,
}

/// Primary source contributing to the current diagnostics result.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDominantSource {
    pub file_path: String,
    pub event_count: u32,
    pub event_share: Option<f64>,
}

/// Confidence bucket for the generated diagnostics result.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum IntuneDiagnosticsConfidenceLevel {
    #[default]
    Unknown,
    Low,
    Medium,
    High,
}

/// Confidence metadata for the generated diagnostics result.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsConfidence {
    pub level: IntuneDiagnosticsConfidenceLevel,
    pub score: Option<f64>,
    #[serde(default)]
    pub reasons: Vec<String>,
}

/// Grouped repeated failures for follow-on diagnostics phases.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneRepeatedFailureGroup {
    pub id: String,
    pub name: String,
    pub event_type: IntuneEventType,
    pub error_code: Option<String>,
    pub occurrences: u32,
    pub timestamp_bounds: Option<IntuneTimestampBounds>,
    #[serde(default)]
    pub source_files: Vec<String>,
    #[serde(default)]
    pub sample_event_ids: Vec<u64>,
}

// ---------------------------------------------------------------------------
// Windows Event Log (EVTX) models
// ---------------------------------------------------------------------------

/// Severity level from a Windows Event Log record (`System.Level`).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum EventLogSeverity {
    Critical,
    Error,
    Warning,
    Information,
    Verbose,
    Unknown,
}

impl EventLogSeverity {
    /// Map the numeric `Level` value from an EVTX record to a severity.
    pub fn from_level(level: u8) -> Self {
        match level {
            1 => Self::Critical,
            2 => Self::Error,
            3 => Self::Warning,
            4 => Self::Information,
            5 => Self::Verbose,
            _ => Self::Unknown,
        }
    }

    pub fn is_error_or_warning(&self) -> bool {
        matches!(self, Self::Critical | Self::Error | Self::Warning)
    }
}

/// Typed Windows Event Log channel corresponding to the curated channels
/// collected by the evidence profile.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum EventLogChannel {
    DeviceManagementAdmin,
    DeviceManagementOperational,
    Autopilot,
    AadOperational,
    DeliveryOptimizationOperational,
    ManagementService,
    ProvisioningDiagnosticsAdmin,
    ShellCoreOperational,
    TimeServiceOperational,
    UserDeviceRegistrationAdmin,
    CryptoDpapiOperational,
    KerberosOperational,
    SystemLog,
    Other(String),
}

impl EventLogChannel {
    /// Map the full channel name string from an EVTX record to a typed variant.
    pub fn from_channel_string(raw: &str) -> Self {
        let lower = raw.to_ascii_lowercase();
        if lower.contains("devicemanagement")
            && lower.contains("/admin")
            && !lower.contains("/operational")
        {
            Self::DeviceManagementAdmin
        } else if lower.contains("devicemanagement") && lower.contains("/operational") {
            Self::DeviceManagementOperational
        } else if lower.contains("moderndeployment") && lower.contains("autopilot") {
            Self::Autopilot
        } else if lower.contains("-aad/operational") {
            Self::AadOperational
        } else if lower.contains("deliveryoptimization") && lower.contains("/operational") {
            Self::DeliveryOptimizationOperational
        } else if lower.contains("moderndeployment") && lower.contains("managementservice") {
            Self::ManagementService
        } else if lower.contains("provisioning-diagnostics") && lower.contains("/admin") {
            Self::ProvisioningDiagnosticsAdmin
        } else if lower.contains("shell-core") && lower.contains("/operational") {
            Self::ShellCoreOperational
        } else if lower.contains("time-service") && lower.contains("/operational") {
            Self::TimeServiceOperational
        } else if lower.contains("user device registration") && lower.contains("/admin") {
            Self::UserDeviceRegistrationAdmin
        } else if lower.contains("crypto-dpapi") && lower.contains("/operational") {
            Self::CryptoDpapiOperational
        } else if lower.contains("kerberos") && lower.contains("/operational") {
            Self::KerberosOperational
        } else if lower == "system" || lower.contains("system log") {
            Self::SystemLog
        } else if raw.is_empty() {
            Self::Other("Unknown".to_string())
        } else {
            Self::Other(raw.to_string())
        }
    }

    /// Short display name for UI badges and labels.
    pub fn display_name(&self) -> &str {
        match self {
            Self::DeviceManagementAdmin => "MDM Admin",
            Self::DeviceManagementOperational => "MDM Operational",
            Self::Autopilot => "Autopilot",
            Self::AadOperational => "AAD Operational",
            Self::DeliveryOptimizationOperational => "DO Operational",
            Self::ManagementService => "Management Service",
            Self::ProvisioningDiagnosticsAdmin => "Provisioning Admin",
            Self::ShellCoreOperational => "Shell Core",
            Self::TimeServiceOperational => "Time Service",
            Self::UserDeviceRegistrationAdmin => "User Device Reg",
            Self::CryptoDpapiOperational => "Crypto DPAPI",
            Self::KerberosOperational => "Kerberos",
            Self::SystemLog => "System",
            Self::Other(name) => name.as_str(),
        }
    }
}

/// A single parsed record from a Windows Event Log (.evtx) file.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogEntry {
    /// Sequential ID across all parsed entries (chronological after sorting).
    pub id: u64,
    /// Typed channel enum.
    pub channel: EventLogChannel,
    /// Human-readable channel display name.
    pub channel_display: String,
    /// Provider name from `System.Provider`.
    pub provider: String,
    /// Windows Event ID.
    pub event_id: u32,
    /// Severity mapped from `System.Level`.
    pub severity: EventLogSeverity,
    /// ISO 8601 UTC timestamp.
    pub timestamp: String,
    /// Computer name from `System.Computer`.
    pub computer: Option<String>,
    /// Extracted message from EventData/UserData.
    pub message: String,
    /// Correlation activity ID from `System.Correlation`.
    pub correlation_activity_id: Option<String>,
    /// Path to the .evtx source file.
    pub source_file: String,
}

/// Per-channel summary rollup for the UI.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogChannelSummary {
    pub channel: EventLogChannel,
    pub channel_display: String,
    pub entry_count: u32,
    pub error_count: u32,
    pub warning_count: u32,
    pub timestamp_bounds: Option<IntuneTimestampBounds>,
    pub source_file: String,
}

/// The kind of deterministic correlation between an event log entry
/// and an IME-derived Intune event or diagnostic.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum EventLogCorrelationKind {
    /// Temporal proximity within a contextually relevant channel.
    TimeWindowChannelMatch,
    /// Error code from an IME event appears in the event log message.
    ErrorCodeMatch,
    /// Enrollment/registration activity near ESP or sync events.
    EnrollmentContextMatch,
}

/// A deterministic link between an event log entry and an IME event or diagnostic.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogCorrelationLink {
    /// Points to `EventLogEntry.id`.
    pub event_log_entry_id: u64,
    /// Points to `IntuneEvent.id` when this is an event-level link.
    pub linked_intune_event_id: Option<u64>,
    /// Points to `IntuneDiagnosticInsight.id` when this is a diagnostic-level link.
    pub linked_diagnostic_id: Option<String>,
    /// Which correlation strategy produced this link.
    pub correlation_kind: EventLogCorrelationKind,
    /// Time gap in seconds between the linked items (for display/sorting).
    pub time_delta_secs: Option<f64>,
}

/// Source category for the current event log analysis payload.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum EventLogAnalysisSource {
    #[default]
    Bundle,
    Live,
}

/// Per-channel outcome status for a live Windows Event Log query.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum EventLogLiveQueryStatus {
    #[default]
    Success,
    Empty,
    Failed,
}

/// Per-channel metadata retained for a live Windows Event Log query attempt.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogLiveQueryChannelResult {
    pub channel: EventLogChannel,
    pub channel_display: String,
    pub channel_path: String,
    pub source_file: String,
    pub status: EventLogLiveQueryStatus,
    pub entry_count: u32,
    pub error_message: Option<String>,
}

/// Live Windows Event Log query summary for UI status and empty-state behavior.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EventLogLiveQueryMetadata {
    pub attempted_channel_count: u32,
    pub successful_channel_count: u32,
    pub channels_with_results_count: u32,
    pub failed_channel_count: u32,
    pub per_channel_entry_limit: u32,
    #[serde(default)]
    pub channels: Vec<EventLogLiveQueryChannelResult>,
}

/// Top-level container for all parsed and correlated Windows Event Log data.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EventLogAnalysis {
    pub source_kind: EventLogAnalysisSource,
    pub entries: Vec<EventLogEntry>,
    pub channel_summaries: Vec<EventLogChannelSummary>,
    pub correlation_links: Vec<EventLogCorrelationLink>,
    pub parsed_file_count: u32,
    pub total_entry_count: u32,
    pub error_entry_count: u32,
    pub warning_entry_count: u32,
    pub timestamp_bounds: Option<IntuneTimestampBounds>,
    pub live_query: Option<EventLogLiveQueryMetadata>,
}

// ---------------------------------------------------------------------------
// Intune analysis result
// ---------------------------------------------------------------------------

/// Complete result of Intune log analysis.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneAnalysisResult {
    /// All detected events
    pub events: Vec<IntuneEvent>,
    /// Download statistics
    pub downloads: Vec<DownloadStat>,
    /// Summary counts
    pub summary: IntuneSummary,
    /// Deterministic diagnostic guidance.
    #[serde(default)]
    pub diagnostics: Vec<IntuneDiagnosticInsight>,
    /// Source path analyzed (file path or directory path)
    pub source_file: String,
    /// Expanded source files included in this result.
    #[serde(default)]
    pub source_files: Vec<String>,
    /// Coverage metadata behind the diagnostics contract.
    #[serde(default)]
    pub diagnostics_coverage: IntuneDiagnosticsCoverage,
    /// Confidence metadata behind the diagnostics contract.
    #[serde(default)]
    pub diagnostics_confidence: IntuneDiagnosticsConfidence,
    /// Grouped repeated failures behind the diagnostics contract.
    #[serde(default)]
    pub repeated_failures: Vec<IntuneRepeatedFailureGroup>,
    /// Bundle metadata retained when the analyzed path is an evidence bundle root.
    #[serde(default)]
    pub evidence_bundle: Option<EvidenceBundleMetadata>,
    /// Parsed and correlated Windows Event Log data from evidence bundle .evtx files.
    #[serde(default)]
    pub event_log_analysis: Option<EventLogAnalysis>,
    /// Global GUID→app-name registry for frontend display (tooltips, lookup panel).
    #[serde(default)]
    pub guid_registry: HashMap<String, GuidRegistryEntry>,
    /// Per-app policy metadata extracted from "Get policies" JSON payloads.
    #[serde(default)]
    pub policy_metadata: HashMap<String, AppPolicyMetadata>,
}

impl Serialize for IntuneAnalysisResult {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("IntuneAnalysisResult", 13)?;
        state.serialize_field("events", &self.events)?;
        state.serialize_field("downloads", &self.downloads)?;
        state.serialize_field("summary", &self.summary)?;
        state.serialize_field("diagnostics", &self.diagnostics)?;
        state.serialize_field("sourceFile", &self.source_file)?;
        state.serialize_field("sourceFiles", &self.source_files)?;
        state.serialize_field("diagnosticsCoverage", &self.diagnostics_coverage)?;
        state.serialize_field("diagnosticsConfidence", &self.diagnostics_confidence)?;
        state.serialize_field("repeatedFailures", &self.repeated_failures)?;
        state.serialize_field("evidenceBundle", &self.evidence_bundle)?;
        state.serialize_field("eventLogAnalysis", &self.event_log_analysis)?;
        state.serialize_field("guidRegistry", &self.guid_registry)?;
        state.serialize_field("policyMetadata", &self.policy_metadata)?;
        state.end()
    }
}

/// Summary statistics from Intune analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneSummary {
    pub total_events: u32,
    pub win32_apps: u32,
    pub winget_apps: u32,
    pub scripts: u32,
    pub remediations: u32,
    pub succeeded: u32,
    pub failed: u32,
    pub in_progress: u32,
    pub pending: u32,
    pub timed_out: u32,
    pub total_downloads: u32,
    pub successful_downloads: u32,
    pub failed_downloads: u32,
    pub failed_scripts: u32,
    pub log_time_span: Option<String>,
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::{
        EvidenceBundleArtifactCounts, EvidenceBundleMetadata, IntuneAnalysisResult,
        IntuneDiagnosticsConfidence, IntuneDiagnosticsCoverage, IntuneSummary,
    };

    #[test]
    fn intune_analysis_result_serializes_evidence_bundle_metadata() {
        let result = IntuneAnalysisResult {
            events: Vec::new(),
            downloads: Vec::new(),
            summary: IntuneSummary {
                total_events: 0,
                win32_apps: 0,
                winget_apps: 0,
                scripts: 0,
                remediations: 0,
                succeeded: 0,
                failed: 0,
                in_progress: 0,
                pending: 0,
                timed_out: 0,
                total_downloads: 0,
                successful_downloads: 0,
                failed_downloads: 0,
                failed_scripts: 0,
                log_time_span: None,
            },
            diagnostics: Vec::new(),
            source_file: "bundle-root".to_string(),
            source_files: Vec::new(),
            diagnostics_coverage: IntuneDiagnosticsCoverage::default(),
            diagnostics_confidence: IntuneDiagnosticsConfidence::default(),
            repeated_failures: Vec::new(),
            event_log_analysis: None,
            guid_registry: HashMap::new(),
            policy_metadata: HashMap::new(),
            evidence_bundle: Some(EvidenceBundleMetadata {
                manifest_path: "bundle-root/manifest.json".to_string(),
                notes_path: Some("bundle-root/notes.md".to_string()),
                evidence_root: Some("bundle-root/evidence".to_string()),
                primary_entry_points: vec!["bundle-root/evidence/logs".to_string()],
                available_primary_entry_points: vec!["bundle-root/evidence/logs".to_string()],
                bundle_id: Some("CMTRACE-123".to_string()),
                bundle_label: Some("intune-endpoint-evidence".to_string()),
                created_utc: Some("2026-03-12T16:00:54Z".to_string()),
                case_reference: Some("case-123".to_string()),
                summary: Some("Collected bundle".to_string()),
                collector_profile: Some("intune-windows-endpoint-v1".to_string()),
                collector_version: Some("1.1.0".to_string()),
                collected_utc: Some("2026-03-12T16:00:54Z".to_string()),
                device_name: Some("GELL-VM-5879648".to_string()),
                primary_user: Some("AzureAD\\AdamGell".to_string()),
                platform: Some("Windows".to_string()),
                os_version: Some("Windows 11".to_string()),
                tenant: Some("CDWWorkspaceLab".to_string()),
                artifact_counts: Some(EvidenceBundleArtifactCounts {
                    collected: 55,
                    missing: 7,
                    failed: 2,
                    skipped: 0,
                }),
            }),
        };

        let value = serde_json::to_value(&result).expect("serialize result");

        assert_eq!(
            value["evidenceBundle"]["bundleId"].as_str(),
            Some("CMTRACE-123")
        );
        assert_eq!(
            value["evidenceBundle"]["artifactCounts"]["collected"].as_u64(),
            Some(55)
        );
    }
}