stateset-embedded 1.22.0

Embeddable commerce library - the SQLite of commerce operations
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
//! Transactional email notification service.
//!
//! Maps [`CommerceEvent`]s to structured email payloads and delivers them via
//! configurable backends. The default backend POSTs a JSON payload to a
//! webhook URL, making it trivial to integrate with any email provider
//! (`SendGrid`, Mailgun, Postmark, etc.) through a thin relay.
//!
//! # Architecture
//!
//! ```text
//! CommerceEvent ──► NotificationService ──► EmailBackend ──► Webhook / Log
//!                    (event→email map)       (delivery)
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::notifications::{NotificationConfig, NotificationService, WebhookEmailBackend};
//!
//! let backend = WebhookEmailBackend::new("https://relay.example.com/email", Some("hmac-secret"))
//!     .with_outbound_allowlist(["relay.example.com"]);
//! let config = NotificationConfig {
//!     from_name: "Acme Store".into(),
//!     from_email: "orders@acme.com".into(),
//!     ..Default::default()
//! };
//! let service = NotificationService::new(config, Box::new(backend));
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use stateset_core::{CommerceEvent, validate_email};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

#[cfg(feature = "events")]
use crate::events::validate_outbound_webhook_target_url;

/// A function that resolves a commerce event to a recipient email address.
pub type RecipientResolver = Arc<dyn Fn(&CommerceEvent) -> Option<String> + Send + Sync>;

// ---------------------------------------------------------------------------
// Email template
// ---------------------------------------------------------------------------

/// Typed email template identifiers for commerce transactional emails.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum EmailTemplate {
    /// Confirmation sent when an order is placed.
    OrderConfirmation,
    /// Notification when an order ships.
    ShippingNotification,
    /// Update when a return request changes status.
    ReturnStatusUpdate,
    /// Notification when an order is cancelled.
    OrderCancellation,
    /// Notification when a refund is issued.
    RefundConfirmation,
    /// Alert when inventory drops below reorder point.
    LowStockAlert,
    /// Welcome email when a customer account is created.
    CustomerWelcome,
}

impl fmt::Display for EmailTemplate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::OrderConfirmation => "order_confirmation",
            Self::ShippingNotification => "shipping_notification",
            Self::ReturnStatusUpdate => "return_status_update",
            Self::OrderCancellation => "order_cancellation",
            Self::RefundConfirmation => "refund_confirmation",
            Self::LowStockAlert => "low_stock_alert",
            Self::CustomerWelcome => "customer_welcome",
        };
        f.write_str(s)
    }
}

// ---------------------------------------------------------------------------
// Transactional email payload
// ---------------------------------------------------------------------------

/// A structured transactional email payload ready for delivery.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionalEmail {
    /// Recipient email address.
    pub to: String,
    /// Sender display name.
    pub from_name: String,
    /// Sender email address.
    pub from_email: String,
    /// Email subject line.
    pub subject: String,
    /// Template identifier.
    pub template: EmailTemplate,
    /// Template variables (key-value pairs injected into the template).
    pub template_data: HashMap<String, serde_json::Value>,
    /// The source commerce event type that triggered this email.
    pub event_type: String,
    /// Timestamp of the originating event.
    pub event_timestamp: DateTime<Utc>,
    /// Unique message ID for idempotency / deduplication.
    pub message_id: uuid::Uuid,
}

// ---------------------------------------------------------------------------
// Delivery backend trait
// ---------------------------------------------------------------------------

/// Trait for email delivery backends.
///
/// Implementations receive a fully-formed [`TransactionalEmail`] and are
/// responsible for delivering it (or enqueuing it for delivery).
pub trait EmailBackend: Send + Sync + fmt::Debug {
    /// Deliver a transactional email.
    ///
    /// Returns `Ok(())` on successful delivery/enqueue, or an error message.
    fn deliver(&self, email: &TransactionalEmail) -> Result<(), String>;
}

// ---------------------------------------------------------------------------
// Webhook backend
// ---------------------------------------------------------------------------

/// Delivers email payloads by posting JSON to a webhook URL.
///
/// The payload is signed with HMAC-SHA256 when a secret is configured,
/// using the `X-Signature-256` header (same scheme as the event webhook
/// system).
#[derive(Clone)]
pub struct WebhookEmailBackend {
    url: String,
    secret: Option<String>,
    outbound_allowlist: Vec<String>,
    #[cfg(feature = "events")]
    client: reqwest::blocking::Client,
}

impl fmt::Debug for WebhookEmailBackend {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WebhookEmailBackend")
            .field("url", &self.url)
            .field("has_secret", &self.secret.is_some())
            .field("outbound_allowlist", &self.outbound_allowlist)
            .finish()
    }
}

impl WebhookEmailBackend {
    /// Create a new webhook email backend.
    #[must_use]
    pub fn new(url: impl Into<String>, secret: Option<String>) -> Self {
        #[cfg(feature = "events")]
        let client = reqwest::blocking::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .unwrap_or_else(|_| reqwest::blocking::Client::new());

        Self {
            url: url.into(),
            secret,
            outbound_allowlist: Vec::new(),
            #[cfg(feature = "events")]
            client,
        }
    }

    /// Restrict outbound webhook delivery to a normalized host allowlist.
    ///
    /// Supported entries:
    /// - `example.com`
    /// - `*.example.com`
    #[must_use]
    pub fn with_outbound_allowlist<I, S>(mut self, entries: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.outbound_allowlist = entries
            .into_iter()
            .filter_map(|entry| {
                let normalized = entry.as_ref().trim().trim_end_matches('.').to_ascii_lowercase();
                if normalized.is_empty() { None } else { Some(normalized) }
            })
            .collect();
        self
    }

    /// Compute HMAC-SHA256 signature for a payload.
    #[cfg(feature = "events")]
    fn sign(&self, payload: &[u8]) -> Option<String> {
        use hmac::{Hmac, Mac};
        use sha2::Sha256;

        let secret = self.secret.as_deref()?;
        let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).ok()?;
        mac.update(payload);
        let result = mac.finalize();
        Some(format!("sha256={}", hex::encode(result.into_bytes())))
    }
}

impl EmailBackend for WebhookEmailBackend {
    fn deliver(&self, email: &TransactionalEmail) -> Result<(), String> {
        validate_transactional_email(email)?;
        let payload =
            serde_json::to_vec(email).map_err(|e| format!("Failed to serialize email: {e}"))?;

        #[cfg(feature = "events")]
        {
            validate_outbound_webhook_target_url(&self.url, &self.outbound_allowlist)
                .map_err(|err| format!("Unsafe webhook delivery URL: {err}"))?;

            let mut request = self
                .client
                .post(&self.url)
                .header("Content-Type", "application/json")
                .header("User-Agent", "stateset-notifications/1.0");

            if let Some(sig) = self.sign(&payload) {
                request = request.header("X-Signature-256", sig);
            }

            let response = request
                .body(payload)
                .send()
                .map_err(|e| format!("Webhook delivery failed: {e}"))?;

            if response.status().is_success() {
                Ok(())
            } else {
                Err(format!("Webhook returned HTTP {}", response.status().as_u16()))
            }
        }

        #[cfg(not(feature = "events"))]
        {
            let _ = payload;
            Err("Webhook backend requires the `events` feature".to_string())
        }
    }
}

// ---------------------------------------------------------------------------
// Log backend (for testing / development)
// ---------------------------------------------------------------------------

/// A backend that logs emails via `tracing` instead of delivering them.
///
/// Useful for development, testing, and dry-run scenarios.
#[derive(Debug, Clone, Default)]
pub struct LogEmailBackend {
    /// Collected emails (for test assertions).
    emails: Arc<std::sync::Mutex<Vec<TransactionalEmail>>>,
}

impl LogEmailBackend {
    /// Create a new log backend.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Get all emails that have been "delivered" (logged).
    #[must_use]
    pub fn emails(&self) -> Vec<TransactionalEmail> {
        self.emails.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone()
    }
}

impl EmailBackend for LogEmailBackend {
    fn deliver(&self, email: &TransactionalEmail) -> Result<(), String> {
        validate_transactional_email(email)?;
        tracing::info!(
            template = %email.template,
            to = %email.to,
            subject = %email.subject,
            event_type = %email.event_type,
            message_id = %email.message_id,
            "Notification email (log backend)"
        );
        self.emails.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(email.clone());
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------

/// Configuration for the notification service.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotificationConfig {
    /// Default sender display name.
    pub from_name: String,
    /// Default sender email address.
    pub from_email: String,
    /// Whether notifications are enabled.
    pub enabled: bool,
    /// Email templates that are enabled. Empty means all templates are enabled.
    pub enabled_templates: Vec<EmailTemplate>,
}

impl Default for NotificationConfig {
    fn default() -> Self {
        Self {
            from_name: "StateSet Commerce".into(),
            from_email: "noreply@stateset.io".into(),
            enabled: true,
            enabled_templates: Vec::new(),
        }
    }
}

impl NotificationConfig {
    /// Check if a template is enabled.
    fn is_template_enabled(&self, template: EmailTemplate) -> bool {
        if !self.enabled {
            return false;
        }
        if self.enabled_templates.is_empty() {
            return true;
        }
        self.enabled_templates.contains(&template)
    }
}

// ---------------------------------------------------------------------------
// Notification service
// ---------------------------------------------------------------------------

/// Maps commerce events to transactional emails and delivers them via a
/// configurable backend.
pub struct NotificationService {
    config: NotificationConfig,
    backend: Box<dyn EmailBackend>,
    /// Optional recipient resolver: given an event, returns the email address.
    /// When `None`, only events that carry an email directly are deliverable.
    recipient_resolver: Option<RecipientResolver>,
}

impl fmt::Debug for NotificationService {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NotificationService")
            .field("config", &self.config)
            .field("backend", &self.backend)
            .field("has_resolver", &self.recipient_resolver.is_some())
            .finish()
    }
}

impl NotificationService {
    /// Create a new notification service.
    #[must_use]
    pub fn new(config: NotificationConfig, backend: Box<dyn EmailBackend>) -> Self {
        Self { config, backend, recipient_resolver: None }
    }

    /// Set a custom recipient resolver function.
    ///
    /// The resolver receives a `CommerceEvent` and should return the
    /// recipient email address, or `None` to skip sending.
    #[must_use]
    pub fn with_recipient_resolver(mut self, resolver: RecipientResolver) -> Self {
        self.recipient_resolver = Some(resolver);
        self
    }

    /// Get the service configuration.
    #[must_use]
    pub const fn config(&self) -> &NotificationConfig {
        &self.config
    }

    /// Process a commerce event, potentially sending an email notification.
    ///
    /// Returns `Ok(Some(message_id))` if an email was sent, `Ok(None)` if
    /// the event does not map to an email template or the template is
    /// disabled, and `Err` if delivery failed.
    pub fn process_event(&self, event: &CommerceEvent) -> Result<Option<uuid::Uuid>, String> {
        if !self.config.enabled {
            return Ok(None);
        }

        let Some(email) = self.event_to_email(event) else {
            return Ok(None);
        };

        validate_transactional_email(&email)?;
        self.backend.deliver(&email)?;

        tracing::debug!(
            template = %email.template,
            to = %email.to,
            message_id = %email.message_id,
            "Notification delivered"
        );

        Ok(Some(email.message_id))
    }

    /// Map a commerce event to a transactional email.
    ///
    /// Returns `None` if the event does not map to any email template, if
    /// the template is disabled, or if no recipient can be resolved.
    #[must_use]
    fn event_to_email(&self, event: &CommerceEvent) -> Option<TransactionalEmail> {
        let (template, subject, data) = self.extract_template_info(event)?;

        if !self.config.is_template_enabled(template) {
            return None;
        }

        let to = self.resolve_recipient(event)?;

        Some(TransactionalEmail {
            to,
            from_name: self.config.from_name.clone(),
            from_email: self.config.from_email.clone(),
            subject,
            template,
            template_data: data,
            event_type: event.event_type().to_string(),
            event_timestamp: event.timestamp(),
            message_id: uuid::Uuid::new_v4(),
        })
    }

    /// Extract template info from a commerce event.
    fn extract_template_info(
        &self,
        event: &CommerceEvent,
    ) -> Option<(EmailTemplate, String, HashMap<String, serde_json::Value>)> {
        match event {
            CommerceEvent::OrderCreated { order_id, total_amount, item_count, .. } => {
                let mut data = HashMap::new();
                data.insert("order_id".into(), serde_json::json!(order_id.to_string()));
                data.insert("total_amount".into(), serde_json::json!(total_amount.to_string()));
                data.insert("item_count".into(), serde_json::json!(item_count));
                Some((
                    EmailTemplate::OrderConfirmation,
                    format!("Order Confirmation — {order_id}"),
                    data,
                ))
            }
            CommerceEvent::OrderCancelled { order_id, reason, .. } => {
                let mut data = HashMap::new();
                data.insert("order_id".into(), serde_json::json!(order_id.to_string()));
                if let Some(reason) = reason {
                    data.insert("reason".into(), serde_json::json!(reason));
                }
                Some((
                    EmailTemplate::OrderCancellation,
                    format!("Order Cancelled — {order_id}"),
                    data,
                ))
            }
            CommerceEvent::OrderFulfillmentStatusChanged { order_id, to_status, .. } => {
                use stateset_core::FulfillmentStatus;
                if *to_status != FulfillmentStatus::Shipped {
                    return None;
                }
                let mut data = HashMap::new();
                data.insert("order_id".into(), serde_json::json!(order_id.to_string()));
                data.insert("status".into(), serde_json::json!(to_status.to_string()));
                Some((
                    EmailTemplate::ShippingNotification,
                    format!("Your Order Has Shipped — {order_id}"),
                    data,
                ))
            }
            CommerceEvent::ReturnStatusChanged { return_id, from_status, to_status, .. } => {
                let mut data = HashMap::new();
                data.insert("return_id".into(), serde_json::json!(return_id.to_string()));
                data.insert("from_status".into(), serde_json::json!(from_status.to_string()));
                data.insert("to_status".into(), serde_json::json!(to_status.to_string()));
                Some((
                    EmailTemplate::ReturnStatusUpdate,
                    format!("Return Update — {return_id}"),
                    data,
                ))
            }
            CommerceEvent::ReturnApproved { return_id, order_id, .. } => {
                let mut data = HashMap::new();
                data.insert("return_id".into(), serde_json::json!(return_id.to_string()));
                data.insert("order_id".into(), serde_json::json!(order_id.to_string()));
                Some((
                    EmailTemplate::ReturnStatusUpdate,
                    format!("Return Approved — {return_id}"),
                    data,
                ))
            }
            CommerceEvent::RefundIssued { return_id, order_id, amount, method, .. } => {
                let mut data = HashMap::new();
                data.insert("return_id".into(), serde_json::json!(return_id.to_string()));
                data.insert("order_id".into(), serde_json::json!(order_id.to_string()));
                data.insert("amount".into(), serde_json::json!(amount.to_string()));
                data.insert("method".into(), serde_json::json!(method));
                Some((
                    EmailTemplate::RefundConfirmation,
                    format!("Refund Issued — {order_id}"),
                    data,
                ))
            }
            CommerceEvent::LowStockAlert { sku, current_quantity, reorder_point, .. } => {
                let mut data = HashMap::new();
                data.insert("sku".into(), serde_json::json!(sku));
                data.insert(
                    "current_quantity".into(),
                    serde_json::json!(current_quantity.to_string()),
                );
                data.insert("reorder_point".into(), serde_json::json!(reorder_point.to_string()));
                Some((EmailTemplate::LowStockAlert, format!("Low Stock Alert — {sku}"), data))
            }
            CommerceEvent::CustomerCreated { customer_id, email, .. } => {
                let mut data = HashMap::new();
                data.insert("customer_id".into(), serde_json::json!(customer_id.to_string()));
                data.insert("email".into(), serde_json::json!(email));
                Some((EmailTemplate::CustomerWelcome, "Welcome to Our Store!".into(), data))
            }
            _ => None,
        }
    }

    /// Resolve the recipient email for an event.
    fn resolve_recipient(&self, event: &CommerceEvent) -> Option<String> {
        // First try the custom resolver
        if let Some(ref resolver) = self.recipient_resolver {
            if let Some(email) = resolver(event) {
                return Some(email);
            }
        }

        // Fall back to extracting email directly from event data
        match event {
            CommerceEvent::CustomerCreated { email, .. } => Some(email.clone()),
            // Other events don't carry emails directly — resolver is needed
            _ => None,
        }
    }
}

fn validate_transactional_email(email: &TransactionalEmail) -> Result<(), String> {
    validate_email(&email.to)
        .map_err(|err| format!("Invalid recipient email `{}`: {err}", email.to))?;
    validate_email(&email.from_email)
        .map_err(|err| format!("Invalid sender email `{}`: {err}", email.from_email))?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use rust_decimal::Decimal;
    use stateset_core::{
        CustomerId, FulfillmentStatus, OrderId, ProductId, ReturnId, ReturnReason, ReturnStatus,
    };

    fn default_service() -> (NotificationService, Arc<std::sync::Mutex<Vec<TransactionalEmail>>>) {
        let backend = LogEmailBackend::new();
        let emails = backend.emails.clone();
        let config = NotificationConfig {
            from_name: "Test Store".into(),
            from_email: "test@example.com".into(),
            ..Default::default()
        };
        let service = NotificationService::new(config, Box::new(backend));
        (service, emails)
    }

    fn service_with_resolver()
    -> (NotificationService, Arc<std::sync::Mutex<Vec<TransactionalEmail>>>) {
        let (service, emails) = default_service();
        let service =
            service.with_recipient_resolver(Arc::new(|_| Some("customer@example.com".into())));
        (service, emails)
    }

    #[test]
    fn order_created_generates_confirmation() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(9999, 2),
            item_count: 3,
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.is_ok());
        assert!(result.as_ref().ok().unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent.len(), 1);
        assert_eq!(sent[0].template, EmailTemplate::OrderConfirmation);
        assert_eq!(sent[0].to, "customer@example.com");
        assert_eq!(sent[0].from_name, "Test Store");
        assert!(sent[0].subject.contains("Order Confirmation"));
        assert!(sent[0].template_data.contains_key("order_id"));
        assert!(sent[0].template_data.contains_key("total_amount"));
        assert!(sent[0].template_data.contains_key("item_count"));
    }

    #[test]
    fn order_cancelled_generates_cancellation() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::OrderCancelled {
            order_id: OrderId::new(),
            reason: Some("Customer request".into()),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::OrderCancellation);
        assert_eq!(sent[0].template_data["reason"], "Customer request");
    }

    #[test]
    fn shipped_generates_shipping_notification() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::OrderFulfillmentStatusChanged {
            order_id: OrderId::new(),
            from_status: FulfillmentStatus::Unfulfilled,
            to_status: FulfillmentStatus::Shipped,
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::ShippingNotification);
        assert!(sent[0].subject.contains("Shipped"));
    }

    #[test]
    fn non_shipped_fulfillment_skipped() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::OrderFulfillmentStatusChanged {
            order_id: OrderId::new(),
            from_status: FulfillmentStatus::Unfulfilled,
            to_status: FulfillmentStatus::Fulfilled,
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_none());
        assert!(emails.lock().unwrap().is_empty());
    }

    #[test]
    fn return_status_change_generates_update() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::ReturnStatusChanged {
            return_id: ReturnId::new(),
            from_status: ReturnStatus::Requested,
            to_status: ReturnStatus::Approved,
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::ReturnStatusUpdate);
    }

    #[test]
    fn return_approved_generates_update() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::ReturnApproved {
            return_id: ReturnId::new(),
            order_id: OrderId::new(),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::ReturnStatusUpdate);
        assert!(sent[0].subject.contains("Approved"));
    }

    #[test]
    fn refund_issued_generates_confirmation() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::RefundIssued {
            return_id: ReturnId::new(),
            order_id: OrderId::new(),
            amount: Decimal::new(2500, 2),
            method: "credit_card".into(),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::RefundConfirmation);
        assert_eq!(sent[0].template_data["method"], "credit_card");
    }

    #[test]
    fn low_stock_alert_generates_email() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::LowStockAlert {
            sku: "WIDGET-001".into(),
            location_id: 1,
            current_quantity: Decimal::new(5, 0),
            reorder_point: Decimal::new(10, 0),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::LowStockAlert);
        assert!(sent[0].subject.contains("WIDGET-001"));
    }

    #[test]
    fn customer_created_generates_welcome() {
        let (service, emails) = default_service();
        // CustomerCreated carries email directly — no resolver needed
        let event = CommerceEvent::CustomerCreated {
            customer_id: CustomerId::new(),
            email: "alice@example.com".into(),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_some());

        let sent = emails.lock().unwrap();
        assert_eq!(sent[0].template, EmailTemplate::CustomerWelcome);
        assert_eq!(sent[0].to, "alice@example.com");
        assert!(sent[0].subject.contains("Welcome"));
    }

    #[test]
    fn unhandled_event_returns_none() {
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::ProductCreated {
            product_id: ProductId::new(),
            name: "Widget".into(),
            slug: "widget".into(),
            timestamp: Utc::now(),
        };

        let result = service.process_event(&event);
        assert!(result.unwrap().is_none());
        assert!(emails.lock().unwrap().is_empty());
    }

    #[test]
    fn disabled_service_skips_all() {
        let backend = LogEmailBackend::new();
        let emails = backend.emails.clone();
        let config = NotificationConfig { enabled: false, ..Default::default() };
        let service = NotificationService::new(config, Box::new(backend))
            .with_recipient_resolver(Arc::new(|_| Some("a@b.com".into())));

        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(100, 0),
            item_count: 1,
            timestamp: Utc::now(),
        };

        assert!(service.process_event(&event).unwrap().is_none());
        assert!(emails.lock().unwrap().is_empty());
    }

    #[test]
    fn template_filtering_works() {
        let backend = LogEmailBackend::new();
        let emails = backend.emails.clone();
        let config = NotificationConfig {
            enabled_templates: vec![EmailTemplate::LowStockAlert],
            ..Default::default()
        };
        let service = NotificationService::new(config, Box::new(backend))
            .with_recipient_resolver(Arc::new(|_| Some("a@b.com".into())));

        // OrderCreated is not in enabled list → skipped
        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(100, 0),
            item_count: 1,
            timestamp: Utc::now(),
        };
        assert!(service.process_event(&event).unwrap().is_none());

        // LowStockAlert IS in enabled list → sent
        let event = CommerceEvent::LowStockAlert {
            sku: "SKU-001".into(),
            location_id: 1,
            current_quantity: Decimal::new(2, 0),
            reorder_point: Decimal::new(10, 0),
            timestamp: Utc::now(),
        };
        assert!(service.process_event(&event).unwrap().is_some());
        assert_eq!(emails.lock().unwrap().len(), 1);
    }

    #[test]
    fn no_resolver_no_email_for_order_events() {
        let (service, emails) = default_service();
        // No resolver set — OrderCreated has no email in the event itself
        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(100, 0),
            item_count: 1,
            timestamp: Utc::now(),
        };

        assert!(service.process_event(&event).unwrap().is_none());
        assert!(emails.lock().unwrap().is_empty());
    }

    #[test]
    fn email_template_display() {
        assert_eq!(EmailTemplate::OrderConfirmation.to_string(), "order_confirmation");
        assert_eq!(EmailTemplate::ShippingNotification.to_string(), "shipping_notification");
        assert_eq!(EmailTemplate::ReturnStatusUpdate.to_string(), "return_status_update");
        assert_eq!(EmailTemplate::OrderCancellation.to_string(), "order_cancellation");
        assert_eq!(EmailTemplate::RefundConfirmation.to_string(), "refund_confirmation");
        assert_eq!(EmailTemplate::LowStockAlert.to_string(), "low_stock_alert");
        assert_eq!(EmailTemplate::CustomerWelcome.to_string(), "customer_welcome");
    }

    #[test]
    fn transactional_email_serializes() {
        let email = TransactionalEmail {
            to: "test@example.com".into(),
            from_name: "Store".into(),
            from_email: "noreply@store.com".into(),
            subject: "Test".into(),
            template: EmailTemplate::OrderConfirmation,
            template_data: HashMap::new(),
            event_type: "order_created".into(),
            event_timestamp: Utc::now(),
            message_id: uuid::Uuid::new_v4(),
        };

        let json = serde_json::to_string(&email).unwrap();
        assert!(json.contains("order_confirmation"));
        assert!(json.contains("test@example.com"));

        let roundtrip: TransactionalEmail = serde_json::from_str(&json).unwrap();
        assert_eq!(roundtrip.template, EmailTemplate::OrderConfirmation);
        assert_eq!(roundtrip.to, "test@example.com");
    }

    #[test]
    fn notification_config_default() {
        let config = NotificationConfig::default();
        assert!(config.enabled);
        assert!(config.enabled_templates.is_empty());
        assert_eq!(config.from_name, "StateSet Commerce");
        assert_eq!(config.from_email, "noreply@stateset.io");
    }

    #[test]
    fn webhook_backend_debug_hides_secret() {
        let backend =
            WebhookEmailBackend::new("https://example.com/email", Some("my_token_123".into()));
        let debug = format!("{backend:?}");
        assert!(debug.contains("has_secret: true"));
        // The actual secret value must not appear in debug output
        assert!(!debug.contains("my_token_123"));
    }

    #[test]
    fn log_backend_collects_emails() {
        let backend = LogEmailBackend::new();
        let email = TransactionalEmail {
            to: "a@b.com".into(),
            from_name: "S".into(),
            from_email: "n@s.com".into(),
            subject: "Hi".into(),
            template: EmailTemplate::CustomerWelcome,
            template_data: HashMap::new(),
            event_type: "customer_created".into(),
            event_timestamp: Utc::now(),
            message_id: uuid::Uuid::new_v4(),
        };

        backend.deliver(&email).unwrap();
        backend.deliver(&email).unwrap();

        let collected = backend.emails();
        assert_eq!(collected.len(), 2);
    }

    #[test]
    fn notification_service_rejects_invalid_sender_email() {
        let backend = LogEmailBackend::new();
        let config = NotificationConfig {
            from_name: "Test Store".into(),
            from_email: "not-an-email".into(),
            ..Default::default()
        };
        let service = NotificationService::new(config, Box::new(backend))
            .with_recipient_resolver(Arc::new(|_| Some("customer@example.com".into())));

        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(100, 0),
            item_count: 1,
            timestamp: Utc::now(),
        };

        let error = service.process_event(&event).unwrap_err();
        assert!(error.contains("Invalid sender email"));
    }

    #[test]
    fn notification_service_rejects_invalid_recipient_email() {
        let backend = LogEmailBackend::new();
        let service = NotificationService::new(NotificationConfig::default(), Box::new(backend))
            .with_recipient_resolver(Arc::new(|_| Some("not-an-email".into())));

        let event = CommerceEvent::OrderCreated {
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            total_amount: Decimal::new(100, 0),
            item_count: 1,
            timestamp: Utc::now(),
        };

        let error = service.process_event(&event).unwrap_err();
        assert!(error.contains("Invalid recipient email"));
    }

    #[cfg(feature = "events")]
    #[test]
    fn webhook_backend_rejects_unsafe_delivery_url_before_network_io() {
        let backend = WebhookEmailBackend::new("http://127.0.0.1:8080/email", None);
        let email = TransactionalEmail {
            to: "customer@example.com".into(),
            from_name: "Store".into(),
            from_email: "noreply@store.com".into(),
            subject: "Test".into(),
            template: EmailTemplate::CustomerWelcome,
            template_data: HashMap::new(),
            event_type: "customer_created".into(),
            event_timestamp: Utc::now(),
            message_id: uuid::Uuid::new_v4(),
        };

        let error = backend.deliver(&email).unwrap_err();
        assert!(error.contains("Unsafe webhook delivery URL"));
    }

    #[test]
    fn return_requested_not_mapped() {
        // ReturnRequested is not directly mapped (ReturnStatusChanged is)
        let (service, emails) = service_with_resolver();
        let event = CommerceEvent::ReturnRequested {
            return_id: ReturnId::new(),
            order_id: OrderId::new(),
            customer_id: CustomerId::new(),
            reason: ReturnReason::Defective,
            item_count: 1,
            timestamp: Utc::now(),
        };

        assert!(service.process_event(&event).unwrap().is_none());
        assert!(emails.lock().unwrap().is_empty());
    }

    #[test]
    fn message_id_is_unique() {
        let (service, emails) = service_with_resolver();

        for _ in 0..3 {
            let event = CommerceEvent::OrderCreated {
                order_id: OrderId::new(),
                customer_id: CustomerId::new(),
                total_amount: Decimal::new(100, 0),
                item_count: 1,
                timestamp: Utc::now(),
            };
            service.process_event(&event).unwrap();
        }

        let sent = emails.lock().unwrap();
        let ids: Vec<_> = sent.iter().map(|e| e.message_id).collect();
        assert_ne!(ids[0], ids[1]);
        assert_ne!(ids[1], ids[2]);
    }
}