rust-threat-detector 2.0.0

Advanced memory-safe SIEM threat detection with ML-based scoring, automated incident response, and threat hunting capabilities
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
//! Automated Incident Response Module v2.0
//!
//! Provides automated response playbooks, incident tracking, and
//! remediation workflows for detected threats.

use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

use crate::{ThreatAlert, ThreatCategory, ThreatSeverity};

/// Incident status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IncidentStatus {
    New,
    Acknowledged,
    Investigating,
    Containing,
    Eradicating,
    Recovering,
    Resolved,
    Closed,
}

impl IncidentStatus {
    /// Get next expected status
    pub fn next(&self) -> Option<IncidentStatus> {
        match self {
            IncidentStatus::New => Some(IncidentStatus::Acknowledged),
            IncidentStatus::Acknowledged => Some(IncidentStatus::Investigating),
            IncidentStatus::Investigating => Some(IncidentStatus::Containing),
            IncidentStatus::Containing => Some(IncidentStatus::Eradicating),
            IncidentStatus::Eradicating => Some(IncidentStatus::Recovering),
            IncidentStatus::Recovering => Some(IncidentStatus::Resolved),
            IncidentStatus::Resolved => Some(IncidentStatus::Closed),
            IncidentStatus::Closed => None,
        }
    }

    /// Check if incident is active
    pub fn is_active(&self) -> bool {
        !matches!(self, IncidentStatus::Resolved | IncidentStatus::Closed)
    }
}

/// Response action types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ResponseAction {
    BlockIP { ip: String, duration_hours: u32 },
    DisableAccount { account: String },
    IsolateHost { hostname: String },
    TerminateSession { session_id: String },
    NotifyTeam { team: String, message: String },
    CreateTicket { system: String, priority: String },
    CollectEvidence { sources: Vec<String> },
    RunScan { scan_type: String, target: String },
    ResetCredentials { account: String },
    EnableMFA { account: String },
    UpdateFirewall { rule: String },
    EscalateToSOC { priority: String },
    Custom { name: String, parameters: HashMap<String, String> },
}

impl ResponseAction {
    /// Get action name
    pub fn name(&self) -> &str {
        match self {
            ResponseAction::BlockIP { .. } => "Block IP Address",
            ResponseAction::DisableAccount { .. } => "Disable Account",
            ResponseAction::IsolateHost { .. } => "Isolate Host",
            ResponseAction::TerminateSession { .. } => "Terminate Session",
            ResponseAction::NotifyTeam { .. } => "Notify Team",
            ResponseAction::CreateTicket { .. } => "Create Ticket",
            ResponseAction::CollectEvidence { .. } => "Collect Evidence",
            ResponseAction::RunScan { .. } => "Run Security Scan",
            ResponseAction::ResetCredentials { .. } => "Reset Credentials",
            ResponseAction::EnableMFA { .. } => "Enable MFA",
            ResponseAction::UpdateFirewall { .. } => "Update Firewall",
            ResponseAction::EscalateToSOC { .. } => "Escalate to SOC",
            ResponseAction::Custom { name, .. } => name,
        }
    }

    /// Check if action is reversible
    pub fn is_reversible(&self) -> bool {
        matches!(
            self,
            ResponseAction::BlockIP { .. }
            | ResponseAction::DisableAccount { .. }
            | ResponseAction::IsolateHost { .. }
        )
    }
}

/// Response action result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionResult {
    pub action: ResponseAction,
    pub success: bool,
    pub executed_at: DateTime<Utc>,
    pub message: String,
    pub execution_time_ms: u64,
}

/// Response playbook
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Playbook {
    pub id: String,
    pub name: String,
    pub description: String,
    pub threat_categories: Vec<ThreatCategory>,
    pub min_severity: ThreatSeverity,
    pub actions: Vec<PlaybookAction>,
    pub enabled: bool,
    pub requires_approval: bool,
}

/// Playbook action with conditions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaybookAction {
    pub action: ResponseAction,
    pub order: u32,
    pub condition: Option<String>,
    pub timeout_seconds: u32,
    pub on_failure: FailureAction,
}

/// What to do on action failure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FailureAction {
    Continue,
    Stop,
    Retry { max_attempts: u32 },
    Escalate,
}

/// Security incident
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Incident {
    pub id: String,
    pub title: String,
    pub description: String,
    pub status: IncidentStatus,
    pub severity: ThreatSeverity,
    pub category: ThreatCategory,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub acknowledged_at: Option<DateTime<Utc>>,
    pub resolved_at: Option<DateTime<Utc>>,
    pub assigned_to: Option<String>,
    pub related_alerts: Vec<String>,
    pub affected_assets: Vec<String>,
    pub actions_taken: Vec<ActionResult>,
    pub notes: Vec<IncidentNote>,
    pub timeline: Vec<TimelineEntry>,
    pub metrics: IncidentMetrics,
}

/// Incident note
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncidentNote {
    pub id: String,
    pub author: String,
    pub content: String,
    pub created_at: DateTime<Utc>,
}

/// Timeline entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimelineEntry {
    pub timestamp: DateTime<Utc>,
    pub event_type: String,
    pub description: String,
    pub actor: String,
}

/// Incident metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncidentMetrics {
    pub time_to_detect_seconds: i64,
    pub time_to_acknowledge_seconds: Option<i64>,
    pub time_to_contain_seconds: Option<i64>,
    pub time_to_resolve_seconds: Option<i64>,
    pub total_actions: usize,
    pub successful_actions: usize,
}

impl Incident {
    /// Create new incident from alert
    pub fn from_alert(alert: &ThreatAlert) -> Self {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now();

        Self {
            id: id.clone(),
            title: format!("{:?}: {}", alert.category, alert.description),
            description: alert.description.clone(),
            status: IncidentStatus::New,
            severity: alert.severity,
            category: alert.category.clone(),
            created_at: now,
            updated_at: now,
            acknowledged_at: None,
            resolved_at: None,
            assigned_to: None,
            related_alerts: vec![alert.alert_id.clone()],
            affected_assets: alert.indicators.clone(),
            actions_taken: Vec::new(),
            notes: Vec::new(),
            timeline: vec![TimelineEntry {
                timestamp: now,
                event_type: "Created".to_string(),
                description: "Incident created from alert".to_string(),
                actor: "System".to_string(),
            }],
            metrics: IncidentMetrics {
                time_to_detect_seconds: 0,
                time_to_acknowledge_seconds: None,
                time_to_contain_seconds: None,
                time_to_resolve_seconds: None,
                total_actions: 0,
                successful_actions: 0,
            },
        }
    }

    /// Update incident status
    pub fn update_status(&mut self, new_status: IncidentStatus, actor: &str) {
        let old_status = self.status;
        self.status = new_status;
        self.updated_at = Utc::now();

        self.timeline.push(TimelineEntry {
            timestamp: Utc::now(),
            event_type: "Status Change".to_string(),
            description: format!("{:?} -> {:?}", old_status, new_status),
            actor: actor.to_string(),
        });

        // Update metrics
        match new_status {
            IncidentStatus::Acknowledged => {
                self.acknowledged_at = Some(Utc::now());
                self.metrics.time_to_acknowledge_seconds = Some(
                    (Utc::now() - self.created_at).num_seconds()
                );
            }
            IncidentStatus::Containing => {
                self.metrics.time_to_contain_seconds = Some(
                    (Utc::now() - self.created_at).num_seconds()
                );
            }
            IncidentStatus::Resolved => {
                self.resolved_at = Some(Utc::now());
                self.metrics.time_to_resolve_seconds = Some(
                    (Utc::now() - self.created_at).num_seconds()
                );
            }
            _ => {}
        }
    }

    /// Add action result
    pub fn add_action_result(&mut self, result: ActionResult) {
        self.metrics.total_actions += 1;
        if result.success {
            self.metrics.successful_actions += 1;
        }

        self.timeline.push(TimelineEntry {
            timestamp: result.executed_at,
            event_type: "Action Executed".to_string(),
            description: format!("{}: {}", result.action.name(), result.message),
            actor: "System".to_string(),
        });

        self.actions_taken.push(result);
        self.updated_at = Utc::now();
    }

    /// Add note to incident
    pub fn add_note(&mut self, author: &str, content: &str) {
        self.notes.push(IncidentNote {
            id: Uuid::new_v4().to_string(),
            author: author.to_string(),
            content: content.to_string(),
            created_at: Utc::now(),
        });
        self.updated_at = Utc::now();
    }

    /// Calculate incident duration
    pub fn duration(&self) -> Duration {
        let end_time = self.resolved_at.unwrap_or_else(Utc::now);
        end_time - self.created_at
    }

    /// Check if incident is overdue
    pub fn is_overdue(&self, sla_hours: i64) -> bool {
        if !self.status.is_active() {
            return false;
        }
        let elapsed = Utc::now() - self.created_at;
        elapsed.num_hours() > sla_hours
    }
}

/// Incident response manager
pub struct IncidentResponseManager {
    incidents: HashMap<String, Incident>,
    playbooks: Vec<Playbook>,
    auto_response_enabled: bool,
}

impl IncidentResponseManager {
    /// Create new incident response manager
    pub fn new() -> Self {
        let mut manager = Self {
            incidents: HashMap::new(),
            playbooks: Vec::new(),
            auto_response_enabled: true,
        };
        manager.load_default_playbooks();
        manager
    }

    /// Load default response playbooks
    fn load_default_playbooks(&mut self) {
        // Brute Force Response Playbook
        self.playbooks.push(Playbook {
            id: "PB-001".to_string(),
            name: "Brute Force Attack Response".to_string(),
            description: "Automated response to brute force attacks".to_string(),
            threat_categories: vec![ThreatCategory::BruteForce],
            min_severity: ThreatSeverity::Medium,
            actions: vec![
                PlaybookAction {
                    action: ResponseAction::BlockIP {
                        ip: "{{source_ip}}".to_string(),
                        duration_hours: 24,
                    },
                    order: 1,
                    condition: None,
                    timeout_seconds: 30,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::NotifyTeam {
                        team: "Security".to_string(),
                        message: "Brute force attack detected".to_string(),
                    },
                    order: 2,
                    condition: None,
                    timeout_seconds: 10,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::CollectEvidence {
                        sources: vec!["auth_logs".to_string(), "firewall_logs".to_string()],
                    },
                    order: 3,
                    condition: None,
                    timeout_seconds: 60,
                    on_failure: FailureAction::Continue,
                },
            ],
            enabled: true,
            requires_approval: false,
        });

        // Malware Response Playbook
        self.playbooks.push(Playbook {
            id: "PB-002".to_string(),
            name: "Malware Detection Response".to_string(),
            description: "Critical response to malware detection".to_string(),
            threat_categories: vec![ThreatCategory::MalwareDetection],
            min_severity: ThreatSeverity::High,
            actions: vec![
                PlaybookAction {
                    action: ResponseAction::IsolateHost {
                        hostname: "{{hostname}}".to_string(),
                    },
                    order: 1,
                    condition: None,
                    timeout_seconds: 60,
                    on_failure: FailureAction::Escalate,
                },
                PlaybookAction {
                    action: ResponseAction::EscalateToSOC {
                        priority: "Critical".to_string(),
                    },
                    order: 2,
                    condition: None,
                    timeout_seconds: 10,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::RunScan {
                        scan_type: "full_antivirus".to_string(),
                        target: "{{hostname}}".to_string(),
                    },
                    order: 3,
                    condition: None,
                    timeout_seconds: 300,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::CollectEvidence {
                        sources: vec!["memory_dump".to_string(), "process_list".to_string(), "network_connections".to_string()],
                    },
                    order: 4,
                    condition: None,
                    timeout_seconds: 120,
                    on_failure: FailureAction::Continue,
                },
            ],
            enabled: true,
            requires_approval: false,
        });

        // Data Exfiltration Response Playbook
        self.playbooks.push(Playbook {
            id: "PB-003".to_string(),
            name: "Data Exfiltration Response".to_string(),
            description: "Response to potential data theft".to_string(),
            threat_categories: vec![ThreatCategory::DataExfiltration],
            min_severity: ThreatSeverity::High,
            actions: vec![
                PlaybookAction {
                    action: ResponseAction::BlockIP {
                        ip: "{{destination_ip}}".to_string(),
                        duration_hours: 168, // 1 week
                    },
                    order: 1,
                    condition: None,
                    timeout_seconds: 30,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::TerminateSession {
                        session_id: "{{session_id}}".to_string(),
                    },
                    order: 2,
                    condition: None,
                    timeout_seconds: 10,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::DisableAccount {
                        account: "{{username}}".to_string(),
                    },
                    order: 3,
                    condition: Some("severity >= High".to_string()),
                    timeout_seconds: 10,
                    on_failure: FailureAction::Escalate,
                },
                PlaybookAction {
                    action: ResponseAction::CreateTicket {
                        system: "ServiceNow".to_string(),
                        priority: "P1".to_string(),
                    },
                    order: 4,
                    condition: None,
                    timeout_seconds: 30,
                    on_failure: FailureAction::Continue,
                },
            ],
            enabled: true,
            requires_approval: true,
        });

        // Unauthorized Access Response Playbook
        self.playbooks.push(Playbook {
            id: "PB-004".to_string(),
            name: "Unauthorized Access Response".to_string(),
            description: "Response to privilege escalation and unauthorized access".to_string(),
            threat_categories: vec![ThreatCategory::UnauthorizedAccess],
            min_severity: ThreatSeverity::High,
            actions: vec![
                PlaybookAction {
                    action: ResponseAction::DisableAccount {
                        account: "{{username}}".to_string(),
                    },
                    order: 1,
                    condition: None,
                    timeout_seconds: 10,
                    on_failure: FailureAction::Escalate,
                },
                PlaybookAction {
                    action: ResponseAction::ResetCredentials {
                        account: "{{username}}".to_string(),
                    },
                    order: 2,
                    condition: None,
                    timeout_seconds: 30,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::EnableMFA {
                        account: "{{username}}".to_string(),
                    },
                    order: 3,
                    condition: None,
                    timeout_seconds: 30,
                    on_failure: FailureAction::Continue,
                },
                PlaybookAction {
                    action: ResponseAction::EscalateToSOC {
                        priority: "High".to_string(),
                    },
                    order: 4,
                    condition: None,
                    timeout_seconds: 10,
                    on_failure: FailureAction::Continue,
                },
            ],
            enabled: true,
            requires_approval: false,
        });
    }

    /// Create incident from alert
    pub fn create_incident(&mut self, alert: &ThreatAlert) -> String {
        let incident = Incident::from_alert(alert);
        let id = incident.id.clone();
        self.incidents.insert(id.clone(), incident);
        id
    }

    /// Get incident by ID
    pub fn get_incident(&self, id: &str) -> Option<&Incident> {
        self.incidents.get(id)
    }

    /// Get mutable incident
    pub fn get_incident_mut(&mut self, id: &str) -> Option<&mut Incident> {
        self.incidents.get_mut(id)
    }

    /// Find applicable playbooks for an alert
    pub fn find_playbooks(&self, alert: &ThreatAlert) -> Vec<&Playbook> {
        self.playbooks
            .iter()
            .filter(|pb| {
                pb.enabled
                    && pb.threat_categories.contains(&alert.category)
                    && alert.severity >= pb.min_severity
            })
            .collect()
    }

    /// Execute playbook actions (simulated)
    pub fn execute_playbook(&mut self, incident_id: &str, playbook: &Playbook, context: &HashMap<String, String>) -> Vec<ActionResult> {
        let mut results = Vec::new();

        for pb_action in &playbook.actions {
            let action = self.substitute_variables(&pb_action.action, context);

            // Simulate action execution
            let result = ActionResult {
                action,
                success: true, // In real implementation, this would be actual execution
                executed_at: Utc::now(),
                message: "Action executed successfully".to_string(),
                execution_time_ms: 100,
            };

            results.push(result.clone());

            // Update incident if it exists
            if let Some(incident) = self.incidents.get_mut(incident_id) {
                incident.add_action_result(result);
            }
        }

        results
    }

    /// Substitute variables in action
    fn substitute_variables(&self, action: &ResponseAction, context: &HashMap<String, String>) -> ResponseAction {
        match action {
            ResponseAction::BlockIP { ip, duration_hours } => {
                ResponseAction::BlockIP {
                    ip: self.substitute_var(ip, context),
                    duration_hours: *duration_hours,
                }
            }
            ResponseAction::DisableAccount { account } => {
                ResponseAction::DisableAccount {
                    account: self.substitute_var(account, context),
                }
            }
            ResponseAction::IsolateHost { hostname } => {
                ResponseAction::IsolateHost {
                    hostname: self.substitute_var(hostname, context),
                }
            }
            ResponseAction::TerminateSession { session_id } => {
                ResponseAction::TerminateSession {
                    session_id: self.substitute_var(session_id, context),
                }
            }
            ResponseAction::RunScan { scan_type, target } => {
                ResponseAction::RunScan {
                    scan_type: scan_type.clone(),
                    target: self.substitute_var(target, context),
                }
            }
            ResponseAction::ResetCredentials { account } => {
                ResponseAction::ResetCredentials {
                    account: self.substitute_var(account, context),
                }
            }
            ResponseAction::EnableMFA { account } => {
                ResponseAction::EnableMFA {
                    account: self.substitute_var(account, context),
                }
            }
            _ => action.clone(),
        }
    }

    /// Substitute variable placeholders
    fn substitute_var(&self, template: &str, context: &HashMap<String, String>) -> String {
        let mut result = template.to_string();
        for (key, value) in context {
            result = result.replace(&format!("{{{{{}}}}}", key), value);
        }
        result
    }

    /// Get active incidents
    pub fn get_active_incidents(&self) -> Vec<&Incident> {
        self.incidents
            .values()
            .filter(|i| i.status.is_active())
            .collect()
    }

    /// Get incidents by severity
    pub fn get_incidents_by_severity(&self, min_severity: ThreatSeverity) -> Vec<&Incident> {
        self.incidents
            .values()
            .filter(|i| i.severity >= min_severity)
            .collect()
    }

    /// Get overdue incidents
    pub fn get_overdue_incidents(&self, sla_hours: i64) -> Vec<&Incident> {
        self.incidents
            .values()
            .filter(|i| i.is_overdue(sla_hours))
            .collect()
    }

    /// Get incident statistics
    pub fn get_statistics(&self) -> IncidentStatistics {
        let total = self.incidents.len();
        let active = self.incidents.values().filter(|i| i.status.is_active()).count();
        let resolved = self.incidents.values().filter(|i| matches!(i.status, IncidentStatus::Resolved | IncidentStatus::Closed)).count();

        let mut by_severity: HashMap<ThreatSeverity, usize> = HashMap::new();
        let mut by_category: HashMap<ThreatCategory, usize> = HashMap::new();

        for incident in self.incidents.values() {
            *by_severity.entry(incident.severity).or_insert(0) += 1;
            *by_category.entry(incident.category.clone()).or_insert(0) += 1;
        }

        let avg_resolution_time = self.calculate_avg_resolution_time();

        IncidentStatistics {
            total_incidents: total,
            active_incidents: active,
            resolved_incidents: resolved,
            by_severity,
            by_category,
            average_resolution_time_hours: avg_resolution_time,
        }
    }

    /// Calculate average resolution time
    fn calculate_avg_resolution_time(&self) -> f64 {
        let resolved: Vec<&Incident> = self.incidents
            .values()
            .filter(|i| i.metrics.time_to_resolve_seconds.is_some())
            .collect();

        if resolved.is_empty() {
            return 0.0;
        }

        let total_seconds: i64 = resolved
            .iter()
            .filter_map(|i| i.metrics.time_to_resolve_seconds)
            .sum();

        (total_seconds as f64 / resolved.len() as f64) / 3600.0
    }

    /// Enable/disable auto response
    pub fn set_auto_response(&mut self, enabled: bool) {
        self.auto_response_enabled = enabled;
    }

    /// Add custom playbook
    pub fn add_playbook(&mut self, playbook: Playbook) {
        self.playbooks.push(playbook);
    }

    /// Get all playbooks
    pub fn get_playbooks(&self) -> &[Playbook] {
        &self.playbooks
    }
}

impl Default for IncidentResponseManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Incident statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncidentStatistics {
    pub total_incidents: usize,
    pub active_incidents: usize,
    pub resolved_incidents: usize,
    pub by_severity: HashMap<ThreatSeverity, usize>,
    pub by_category: HashMap<ThreatCategory, usize>,
    pub average_resolution_time_hours: f64,
}

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

    fn create_test_alert() -> ThreatAlert {
        ThreatAlert {
            alert_id: "TEST-001".to_string(),
            timestamp: Utc::now(),
            severity: ThreatSeverity::High,
            category: ThreatCategory::BruteForce,
            description: "Test brute force alert".to_string(),
            source_log: "Test log".to_string(),
            indicators: vec!["192.168.1.100".to_string()],
            recommended_action: "Block IP".to_string(),
            threat_score: 80,
            correlated_alerts: vec![],
        }
    }

    #[test]
    fn test_incident_creation() {
        let alert = create_test_alert();
        let incident = Incident::from_alert(&alert);

        assert_eq!(incident.status, IncidentStatus::New);
        assert_eq!(incident.severity, ThreatSeverity::High);
        assert_eq!(incident.related_alerts.len(), 1);
    }

    #[test]
    fn test_incident_status_progression() {
        let alert = create_test_alert();
        let mut incident = Incident::from_alert(&alert);

        incident.update_status(IncidentStatus::Acknowledged, "analyst");
        assert!(incident.acknowledged_at.is_some());
        assert!(incident.metrics.time_to_acknowledge_seconds.is_some());

        incident.update_status(IncidentStatus::Resolved, "analyst");
        assert!(incident.resolved_at.is_some());
        assert!(incident.metrics.time_to_resolve_seconds.is_some());
    }

    #[test]
    fn test_incident_notes() {
        let alert = create_test_alert();
        let mut incident = Incident::from_alert(&alert);

        incident.add_note("analyst", "Initial investigation started");
        assert_eq!(incident.notes.len(), 1);
        assert_eq!(incident.notes[0].author, "analyst");
    }

    #[test]
    fn test_playbook_finding() {
        let manager = IncidentResponseManager::new();
        let alert = create_test_alert();

        let playbooks = manager.find_playbooks(&alert);
        assert!(!playbooks.is_empty());

        // Should find brute force playbook
        assert!(playbooks.iter().any(|pb| pb.name.contains("Brute Force")));
    }

    #[test]
    fn test_playbook_execution() {
        let mut manager = IncidentResponseManager::new();
        let alert = create_test_alert();

        let incident_id = manager.create_incident(&alert);

        // Clone the playbook to avoid borrow checker issues
        let playbook: Option<Playbook> = {
            manager.find_playbooks(&alert).first().map(|&p| p.clone())
        };

        let mut context = HashMap::new();
        context.insert("source_ip".to_string(), "192.168.1.100".to_string());

        if let Some(playbook) = playbook {
            let results = manager.execute_playbook(&incident_id, &playbook, &context);
            assert!(!results.is_empty());
            assert!(results[0].success);
        }
    }

    #[test]
    fn test_variable_substitution() {
        let manager = IncidentResponseManager::new();
        let mut context = HashMap::new();
        context.insert("source_ip".to_string(), "10.0.0.1".to_string());

        let action = ResponseAction::BlockIP {
            ip: "{{source_ip}}".to_string(),
            duration_hours: 24,
        };

        let substituted = manager.substitute_variables(&action, &context);
        if let ResponseAction::BlockIP { ip, .. } = substituted {
            assert_eq!(ip, "10.0.0.1");
        }
    }

    #[test]
    fn test_active_incidents() {
        let mut manager = IncidentResponseManager::new();

        let alert1 = create_test_alert();
        let id1 = manager.create_incident(&alert1);

        let alert2 = create_test_alert();
        let id2 = manager.create_incident(&alert2);

        // Resolve one incident
        if let Some(incident) = manager.get_incident_mut(&id1) {
            incident.update_status(IncidentStatus::Resolved, "analyst");
        }

        let active = manager.get_active_incidents();
        assert_eq!(active.len(), 1);
    }

    #[test]
    fn test_incident_statistics() {
        let mut manager = IncidentResponseManager::new();

        for _ in 0..5 {
            let alert = create_test_alert();
            manager.create_incident(&alert);
        }

        let stats = manager.get_statistics();
        assert_eq!(stats.total_incidents, 5);
        assert_eq!(stats.active_incidents, 5);
    }

    #[test]
    fn test_incident_overdue() {
        let alert = create_test_alert();
        let mut incident = Incident::from_alert(&alert);

        // New incident should not be overdue with reasonable SLA
        assert!(!incident.is_overdue(24));

        // Resolved incident should not be overdue
        incident.update_status(IncidentStatus::Resolved, "analyst");
        assert!(!incident.is_overdue(1));
    }

    #[test]
    fn test_action_reversibility() {
        let block_action = ResponseAction::BlockIP {
            ip: "10.0.0.1".to_string(),
            duration_hours: 24,
        };
        assert!(block_action.is_reversible());

        let notify_action = ResponseAction::NotifyTeam {
            team: "Security".to_string(),
            message: "Test".to_string(),
        };
        assert!(!notify_action.is_reversible());
    }

    #[test]
    fn test_status_next() {
        assert_eq!(IncidentStatus::New.next(), Some(IncidentStatus::Acknowledged));
        assert_eq!(IncidentStatus::Resolved.next(), Some(IncidentStatus::Closed));
        assert_eq!(IncidentStatus::Closed.next(), None);
    }
}